├── .travis.yml ├── .jshintrc ├── src ├── error.js └── index.js ├── test ├── keys.js ├── errors.js └── types.js ├── lib ├── error.js └── index.js ├── .gitignore ├── .eslintrc.js ├── LICENSE ├── package.json └── README.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "4.2" 4 | - "5.3" 5 | after_script: 6 | - npm run coverage 7 | - npm run coveralls 8 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "esnext": true, 3 | "undef": true, 4 | "unused": true, 5 | "node": true, 6 | "mocha": true, 7 | "predef": [ ], 8 | "-W030": false 9 | } 10 | -------------------------------------------------------------------------------- /src/error.js: -------------------------------------------------------------------------------- 1 | export default function CheckError(message, key = null) { 2 | this.name = 'CheckError'; 3 | this.key = key; 4 | this.message = message; 5 | this.stack = (new Error()).stack; 6 | } 7 | CheckError.prototype = new Error(); 8 | -------------------------------------------------------------------------------- /test/keys.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import check from '../src/index.js'; 4 | import assert from 'assert'; 5 | 6 | describe('keys', function() { 7 | it('Nested objects', function() { 8 | assert.throws(() => check({ some: { }}, { 9 | some: { 10 | field: String, 11 | }, 12 | }), (error) => { 13 | return (error.key === 'some.field'); 14 | }); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /lib/error.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.default = CheckError; 7 | function CheckError(message) { 8 | var key = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; 9 | 10 | this.name = 'CheckError'; 11 | this.key = key; 12 | this.message = message; 13 | this.stack = new Error().stack; 14 | } 15 | CheckError.prototype = new Error(); -------------------------------------------------------------------------------- /test/errors.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import check from '../src/index.js'; 4 | import assert from 'assert'; 5 | 6 | describe('error pattern types', function() { 7 | it('String', function() { 8 | assert.throws(() => check(null, String), (error) => { 9 | return (error.message === 'Value null is not of type String.'); 10 | }); 11 | }); 12 | 13 | it('[String]', function() { 14 | assert.throws(() => check('strValue', [String]), (error) => { 15 | return (error.message === 'Value "strValue" is not of type [String].'); 16 | }); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Coverage directory used by tools like istanbul 11 | coverage 12 | 13 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 14 | .grunt 15 | 16 | # node-waf configuration 17 | .lock-wscript 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 24 | node_modules 25 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | browser: true, 4 | es6: true, 5 | node: true, 6 | }, 7 | extends: 'eslint:recommended', 8 | parserOptions: { 9 | ecmaFeatures: { 10 | experimentalObjectRestSpread: true, 11 | }, 12 | sourceType: 'module', 13 | }, 14 | plugins: [ 15 | ], 16 | rules: { 17 | indent: [ 18 | 2, 19 | 'tab', 20 | ], 21 | 'linebreak-style': [ 22 | 2, 23 | 'unix', 24 | ], 25 | quotes: [ 26 | 2, 27 | 'single', 28 | ], 29 | semi: [ 30 | 2, 31 | 'always', 32 | ], 33 | 'comma-dangle': [2, 'always-multiline'], 34 | }, 35 | }; 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Martijn de Haan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "type-check-system", 3 | "version": "2.0.0", 4 | "description": "A simple type checker for JavaScript inspired by Meteor's check.", 5 | "main": "lib/index.js", 6 | "jsnext:main": "src/index.js", 7 | "scripts": { 8 | "build": "babel src -d lib", 9 | "test": "NODE_ENV=test mocha --compilers js:babel-register --reporter spec ./test/*.js", 10 | "coverage": "istanbul cover _mocha -- --compilers js:babel-register --recursive ./test/*.js", 11 | "coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/martijndeh/check.git" 16 | }, 17 | "bugs": { 18 | "url": "https://github.com/martijndeh/check/issues" 19 | }, 20 | "author": "Martijn de Haan", 21 | "license": "MIT", 22 | "devDependencies": { 23 | "babel": "^6.5.2", 24 | "babel-cli": "^6.5.1", 25 | "babel-preset-es2015": "^6.5.0", 26 | "babel-register": "^6.7.2", 27 | "coveralls": "^2.11.6", 28 | "istanbul": "^1.0.0-alpha.2", 29 | "mocha": "^2.4.5" 30 | }, 31 | "babel": { 32 | "presets": [ 33 | "es2015" 34 | ] 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Check 2 | [![Build Status](https://travis-ci.org/martijndeh/lego.svg?branch=master)](https://travis-ci.org/martijndeh/check) 3 | [![Coverage Status](https://coveralls.io/repos/github/martijndeh/check/badge.svg?branch=master)](https://coveralls.io/github/martijndeh/check?branch=master) 4 | 5 | A type checker for JavaScript inspired by Meteor's check. 6 | 7 | ``` 8 | npm install type-check-system --save 9 | ``` 10 | 11 | ## Usage 12 | 13 | Check if `value` is a string. 14 | ```js 15 | let value = 'This is a string.'; 16 | check(value, String); 17 | ``` 18 | 19 | Check if `value` is a list of strings. 20 | ```js 21 | let value = ['a', 'b', 'c']; 22 | check(value, [String]); 23 | ``` 24 | 25 | Check if `value` is a string, or null. 26 | ```js 27 | let value = 'string'; 28 | check(value, String, null); 29 | ``` 30 | 31 | Check if `value` is an object with key `id` with type string. 32 | ```js 33 | let value = { 34 | id: '0af390f0-abb6-4ef6-b9af-6287e6aab172' 35 | }; 36 | check(value, {id: String}); 37 | ``` 38 | 39 | Please have a look at the tests to see more examples. 40 | 41 | ## Limitations 42 | 43 | This library does not expose any `Match`-like functions, like Meteor's check library. Instead, this library only uses a check based on patterns to keep it's api simple. If you want `Match` features, have a look at [meteor-check](https://www.npmjs.com/package/meteor-check) instead. 44 | 45 | ## Tests 46 | 47 | To execute the tests, run the following commands: 48 | 49 | ``` 50 | npm run build 51 | npm run test 52 | ``` 53 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import CheckError from './error.js'; 2 | 3 | function getDescription(pattern) { 4 | if (Array.isArray(pattern)) { 5 | return '[' + pattern.map(getDescription).join(', ') + ']'; 6 | } 7 | else if (pattern === void 0) { 8 | return 'undefined'; 9 | } 10 | 11 | const description = JSON.stringify(pattern); 12 | if (description === void 0 && pattern && pattern.name) { 13 | return pattern.name; 14 | } 15 | 16 | return description; 17 | } 18 | 19 | export default function check(value, ...patterns) { 20 | const _check = function(value, pattern, keys = []) { 21 | if (typeof pattern === 'undefined') { 22 | return (typeof value === 'undefined'); 23 | } 24 | else if (pattern === true) { 25 | return (value === true); 26 | } 27 | else if (pattern === false) { 28 | return (value === false); 29 | } 30 | else if (pattern === null) { 31 | return (value === null); 32 | } 33 | else if (pattern === String) { 34 | return (typeof value === 'string'); 35 | } 36 | else if (pattern === Array) { 37 | return Array.isArray(value); 38 | } 39 | else if (pattern === Number) { 40 | return (typeof value === 'number'); 41 | } 42 | else if (pattern === Boolean) { 43 | return (typeof value === 'boolean'); 44 | } 45 | else if (pattern === Function) { 46 | return (typeof value === 'function'); 47 | } 48 | else if (pattern instanceof RegExp) { 49 | return (pattern.test(value) === true); 50 | } 51 | else if (pattern === Object) { 52 | return (value !== null) && (typeof value === 'object'); 53 | } 54 | else if (typeof pattern === 'object') { 55 | return _check(value, Object) && Object.keys(pattern).every((key) => { 56 | const nextKeys = [...keys, key]; 57 | const isValid = _check(value[key], pattern[key], nextKeys); 58 | 59 | if (!isValid) { 60 | throw new CheckError(`Value ${JSON.stringify(value[key])} is not of type ${getDescription(pattern[key])}`, nextKeys.join('.')); 61 | } 62 | 63 | return isValid; 64 | }); 65 | } 66 | 67 | return (value instanceof pattern); 68 | }; 69 | 70 | if (patterns.length === 0) { 71 | throw new Error('No patterns to check.'); 72 | } 73 | else { 74 | const isValid = patterns.some((pattern) => { 75 | if(Array.isArray(pattern)) { 76 | return Array.isArray(value) && value.every((subvalue) => _check(subvalue, pattern[0])); 77 | } 78 | else { 79 | return _check(value, pattern); 80 | } 81 | }); 82 | 83 | if (!isValid) { 84 | throw new CheckError(`Value ${JSON.stringify(value)} is not of type ${patterns.map((pattern) => getDescription(pattern)).join(', ')}.`); 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; 8 | 9 | exports.default = check; 10 | 11 | var _error = require('./error.js'); 12 | 13 | var _error2 = _interopRequireDefault(_error); 14 | 15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 16 | 17 | function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } 18 | 19 | function getDescription(pattern) { 20 | if (Array.isArray(pattern)) { 21 | return '[' + pattern.map(getDescription).join(', ') + ']'; 22 | } else if (pattern === void 0) { 23 | return 'undefined'; 24 | } 25 | 26 | var description = JSON.stringify(pattern); 27 | if (description === void 0 && pattern && pattern.name) { 28 | return pattern.name; 29 | } 30 | 31 | return description; 32 | } 33 | 34 | function check(value) { 35 | var _check = function _check(value, pattern) { 36 | var keys = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; 37 | 38 | if (typeof pattern === 'undefined') { 39 | return typeof value === 'undefined'; 40 | } else if (pattern === true) { 41 | return value === true; 42 | } else if (pattern === false) { 43 | return value === false; 44 | } else if (pattern === null) { 45 | return value === null; 46 | } else if (pattern === String) { 47 | return typeof value === 'string'; 48 | } else if (pattern === Array) { 49 | return Array.isArray(value); 50 | } else if (pattern === Number) { 51 | return typeof value === 'number'; 52 | } else if (pattern === Boolean) { 53 | return typeof value === 'boolean'; 54 | } else if (pattern === Function) { 55 | return typeof value === 'function'; 56 | } else if (pattern instanceof RegExp) { 57 | return pattern.test(value) === true; 58 | } else if (pattern === Object) { 59 | return value !== null && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object'; 60 | } else if ((typeof pattern === 'undefined' ? 'undefined' : _typeof(pattern)) === 'object') { 61 | return _check(value, Object) && Object.keys(pattern).every(function (key) { 62 | var nextKeys = [].concat(_toConsumableArray(keys), [key]); 63 | var isValid = _check(value[key], pattern[key], nextKeys); 64 | 65 | if (!isValid) { 66 | throw new _error2.default('Value ' + JSON.stringify(value[key]) + ' is not of type ' + getDescription(pattern[key]), nextKeys.join('.')); 67 | } 68 | 69 | return isValid; 70 | }); 71 | } 72 | 73 | return value instanceof pattern; 74 | }; 75 | 76 | for (var _len = arguments.length, patterns = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { 77 | patterns[_key - 1] = arguments[_key]; 78 | } 79 | 80 | if (patterns.length === 0) { 81 | throw new Error('No patterns to check.'); 82 | } else { 83 | var isValid = patterns.some(function (pattern) { 84 | if (Array.isArray(pattern)) { 85 | return Array.isArray(value) && value.every(function (subvalue) { 86 | return _check(subvalue, pattern[0]); 87 | }); 88 | } else { 89 | return _check(value, pattern); 90 | } 91 | }); 92 | 93 | if (!isValid) { 94 | throw new _error2.default('Value ' + JSON.stringify(value) + ' is not of type ' + patterns.map(function (pattern) { 95 | return getDescription(pattern); 96 | }).join(', ') + '.'); 97 | } 98 | } 99 | } -------------------------------------------------------------------------------- /test/types.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import check from '../src/index.js'; 4 | import assert from 'assert'; 5 | import CheckError from '../src/error.js'; 6 | 7 | describe('types', function() { 8 | it('undefined', function() { 9 | check(undefined, undefined); 10 | check([undefined], [undefined]); 11 | 12 | assert.throws(() => check([1, 2, 3], [undefined]), CheckError); 13 | assert.throws(() => check([undefined, 2, 3], [undefined]), CheckError); 14 | assert.throws(() => check(1, undefined), CheckError); 15 | assert.throws(() => check('test', undefined), CheckError); 16 | assert.throws(() => check(/$/i, undefined), CheckError); 17 | assert.throws(() => check(null, undefined), CheckError); 18 | assert.throws(() => check(0, undefined), CheckError); 19 | assert.throws(() => check(false, undefined), CheckError); 20 | assert.throws(() => check(Object, undefined), CheckError); 21 | assert.throws(() => check({}, undefined), CheckError); 22 | assert.throws(() => check(function() {}, undefined), CheckError); 23 | assert.throws(() => check({id: 123}, undefined), CheckError); 24 | assert.throws(() => check(new Error(), undefined), CheckError); 25 | assert.throws(() => check(false, undefined), CheckError); 26 | assert.throws(() => check(true, undefined), CheckError); 27 | }); 28 | 29 | it('null', function() { 30 | check(null, null); 31 | check([null], [null]); 32 | 33 | assert.throws(() => check([null, 1, 2], [null]), CheckError); 34 | assert.throws(() => check([1, 2, 3], [null]), CheckError); 35 | assert.throws(() => check(undefined, null), CheckError); 36 | assert.throws(() => check(1, null), CheckError); 37 | assert.throws(() => check('test', null), CheckError); 38 | assert.throws(() => check(/$/i, null), CheckError); 39 | assert.throws(() => check(0, null), CheckError); 40 | assert.throws(() => check(false, null), CheckError); 41 | assert.throws(() => check(Object, null), CheckError); 42 | assert.throws(() => check({}, null), CheckError); 43 | assert.throws(() => check(function() {}, null), CheckError); 44 | assert.throws(() => check({id: 123}, null), CheckError); 45 | assert.throws(() => check(new Error(), null), CheckError); 46 | assert.throws(() => check(false, null), CheckError); 47 | assert.throws(() => check(true, null), CheckError); 48 | }); 49 | 50 | it('String', function() { 51 | check('check', String); 52 | check('check', String, null); 53 | check(null, String, null); 54 | check('', String); 55 | check(['test', 'test2', 'test3'], [String]); 56 | 57 | assert.throws(() => check(['test', 1], [String]), CheckError); 58 | assert.throws(() => check([1, 2], [String]), CheckError); 59 | assert.throws(() => check(undefined, String), CheckError); 60 | assert.throws(() => check(null, Number), CheckError); 61 | assert.throws(() => check(1, String), CheckError); 62 | assert.throws(() => check(/$/i, String), CheckError); 63 | assert.throws(() => check(0, String), CheckError); 64 | assert.throws(() => check(false, String), CheckError); 65 | assert.throws(() => check(Object, String), CheckError); 66 | assert.throws(() => check({}, String), CheckError); 67 | assert.throws(() => check(function() {}, String), CheckError); 68 | assert.throws(() => check({id: 123}, String), CheckError); 69 | assert.throws(() => check(new Error(), String), CheckError); 70 | assert.throws(() => check(false, String), CheckError); 71 | assert.throws(() => check(true, String), CheckError); 72 | }); 73 | 74 | it('String or Number', function() { 75 | check('check', String, Number); 76 | check('check', Number, String); 77 | check(123, String, Number); 78 | check(123, Number, String); 79 | 80 | assert.throws(() => check(['test', 1], [String]), CheckError); 81 | assert.throws(() => check([1, 2], [String]), CheckError); 82 | 83 | assert.throws(() => check(undefined, String, Number), CheckError); 84 | assert.throws(() => check(null, String, Number), CheckError); 85 | assert.throws(() => check(/$/i, String, Number), CheckError); 86 | assert.throws(() => check(false, String, Number), CheckError); 87 | assert.throws(() => check(Object, String, Number), CheckError); 88 | assert.throws(() => check({}, String, Number), CheckError); 89 | assert.throws(() => check(function() {}, String, Number), CheckError); 90 | assert.throws(() => check({id: 123}, String, Number), CheckError); 91 | assert.throws(() => check(new Error(), String, Number), CheckError); 92 | assert.throws(() => check(false, String, Number), CheckError); 93 | assert.throws(() => check(true, String, Number), CheckError); 94 | }); 95 | 96 | it('Number', function() { 97 | check(1, Number); 98 | check(-1, Number); 99 | check(0xFFFF, Number); 100 | check([1, 2, 3], [Number]); 101 | 102 | assert.throws(() => check([1, 2, 'test'], [Number]), CheckError); 103 | assert.throws(() => check(['a', 'b', 'c'], [Number]), CheckError); 104 | assert.throws(() => check(undefined, Number), CheckError); 105 | assert.throws(() => check(null, Number), CheckError); 106 | assert.throws(() => check('test', Number), CheckError); 107 | assert.throws(() => check(/$/i, Number), CheckError); 108 | assert.throws(() => check(false, Number), CheckError); 109 | assert.throws(() => check(Object, Number), CheckError); 110 | assert.throws(() => check({}, Number), CheckError); 111 | assert.throws(() => check(function() {}, Number), CheckError); 112 | assert.throws(() => check({id: 123}, Number), CheckError); 113 | assert.throws(() => check(new Error(), Number), CheckError); 114 | assert.throws(() => check(false, Number), CheckError); 115 | assert.throws(() => check(true, Number), CheckError); 116 | }); 117 | 118 | it('Boolean', function() { 119 | check(true, Boolean); 120 | check(true, true); 121 | check(false, Boolean); 122 | check(false, false); 123 | check(false, false, true); 124 | check(true, false, true); 125 | check([false, true], [Boolean]); 126 | 127 | assert.throws(() => check(undefined, Boolean), CheckError); 128 | assert.throws(() => check(null, Boolean), CheckError); 129 | assert.throws(() => check('test', Boolean), CheckError); 130 | assert.throws(() => check(/$/i, Boolean), CheckError); 131 | assert.throws(() => check(0, Boolean), CheckError); 132 | assert.throws(() => check(1, Boolean), CheckError); 133 | assert.throws(() => check(Object, Boolean), CheckError); 134 | assert.throws(() => check({}, Boolean), CheckError); 135 | assert.throws(() => check(function() {}, Boolean), CheckError); 136 | assert.throws(() => check({id: 123}, Boolean), CheckError); 137 | assert.throws(() => check(new Error(), Boolean), CheckError); 138 | assert.throws(() => check(false, Object), CheckError); 139 | assert.throws(() => check(true, Object), CheckError); 140 | }); 141 | 142 | it('RegExp', function() { 143 | check('test', /^test$/); 144 | check(/$/i, RegExp); 145 | check([/$/i], [RegExp]); 146 | 147 | assert.throws(() => check(undefined, /^test$/), CheckError); 148 | assert.throws(() => check(null, /^test$/), CheckError); 149 | assert.throws(() => check('test2', /^test$/), CheckError); 150 | assert.throws(() => check(/$/i, /^test$/), CheckError); 151 | assert.throws(() => check(0, /^test$/), CheckError); 152 | assert.throws(() => check(1, /^test$/), CheckError); 153 | assert.throws(() => check(Object, /^test$/), CheckError); 154 | assert.throws(() => check({}, /^test$/), CheckError); 155 | assert.throws(() => check(function() {}, /^test$/), CheckError); 156 | assert.throws(() => check({id: 123}, /^test$/), CheckError); 157 | assert.throws(() => check(new Error(), /^test$/), CheckError); 158 | assert.throws(() => check('fail', /^test$/), CheckError); 159 | assert.throws(() => check(false, Object), CheckError); 160 | assert.throws(() => check(true, Object), CheckError); 161 | }); 162 | 163 | it('Object', function() { 164 | check({}, Object); 165 | check(new Error(), Object); 166 | check({id: 123}, Object); 167 | check([{id: 123}], [Object]); 168 | check(/$/i, Object); 169 | 170 | assert.throws(() => check(undefined, Object), CheckError); 171 | assert.throws(() => check(null, Object), CheckError); 172 | assert.throws(() => check('test', Object), CheckError); 173 | assert.throws(() => check(0, Object), CheckError); 174 | assert.throws(() => check(1, Object), CheckError); 175 | assert.throws(() => check(function() {}, Object), CheckError); 176 | assert.throws(() => check('fail', Object), CheckError); 177 | assert.throws(() => check(false, Object), CheckError); 178 | assert.throws(() => check(true, Object), CheckError); 179 | }); 180 | 181 | it('Object keys', function() { 182 | check({id: 'test'}, {id: String}); 183 | check({id: 'test', name: 'test'}, {id: String}); 184 | check({id: 'test', tests: []}, {id: String, tests: Array}); 185 | check([{id: 'test'}], [{id: String}]); 186 | 187 | assert.throws(() => check({id: 123}, {id: String}), CheckError); 188 | assert.throws(() => check({test: 123}, {id: String}), CheckError); 189 | assert.throws(() => check(undefined, {id: String}), CheckError); 190 | assert.throws(() => check(null, {id: String}), CheckError); 191 | assert.throws(() => check('test', {id: String}), CheckError); 192 | assert.throws(() => check(/$/i, {id: String}), CheckError); 193 | assert.throws(() => check(0, {id: String}), CheckError); 194 | assert.throws(() => check(1, {id: String}), CheckError); 195 | assert.throws(() => check(function() {}, {id: String}), CheckError); 196 | assert.throws(() => check('fail', {id: String}), CheckError); 197 | assert.throws(() => check(false, {id: String}), CheckError); 198 | assert.throws(() => check(true, {id: String}), CheckError); 199 | }); 200 | 201 | it('Array', function() { 202 | check([], Array); 203 | check([1, 2, 3], Array); 204 | check([[1, 2, 3]], [Array]); 205 | 206 | assert.throws(() => check(undefined, Array), CheckError); 207 | assert.throws(() => check(null, Array), CheckError); 208 | assert.throws(() => check('test', Array), CheckError); 209 | assert.throws(() => check(/$/i, Array), CheckError); 210 | assert.throws(() => check(0, Array), CheckError); 211 | assert.throws(() => check(1, Array), CheckError); 212 | assert.throws(() => check(function() {}, Array), CheckError); 213 | assert.throws(() => check('fail', Array), CheckError); 214 | assert.throws(() => check({}, Array), CheckError); 215 | assert.throws(() => check({id: 123}, Array), CheckError); 216 | assert.throws(() => check(false, Array), CheckError); 217 | assert.throws(() => check(true, Array), CheckError); 218 | }); 219 | 220 | it('Function', function() { 221 | check(function() {}, Function); 222 | check([function() {}], [Function]); 223 | 224 | assert.throws(() => check(undefined, Function), CheckError); 225 | assert.throws(() => check(null, Function), CheckError); 226 | assert.throws(() => check('test', Function), CheckError); 227 | assert.throws(() => check(/$/i, Function), CheckError); 228 | assert.throws(() => check(0, Function), CheckError); 229 | assert.throws(() => check(1, Function), CheckError); 230 | assert.throws(() => check('fail', Function), CheckError); 231 | assert.throws(() => check({}, Function), CheckError); 232 | assert.throws(() => check({id: 123}, Function), CheckError); 233 | assert.throws(() => check(false, Function), CheckError); 234 | assert.throws(() => check(true, Function), CheckError); 235 | }); 236 | 237 | it('Nested objects', function() { 238 | let data = { 239 | sys: { 240 | id: 'test', 241 | version: 1 242 | }, 243 | fields: [], 244 | name: 'Name', 245 | description: 'Description' 246 | }; 247 | check(data, { 248 | sys: { 249 | id: String, 250 | version: Number 251 | }, 252 | fields: Array, 253 | name: String, 254 | description: String 255 | }); 256 | 257 | assert.throws(() => check(null, { 258 | sys: { 259 | id: String, 260 | version: Number 261 | }, 262 | fields: Array, 263 | name: String, 264 | description: String 265 | }), CheckError); 266 | }); 267 | 268 | it('No patterns', function() { 269 | var value = 123; 270 | assert.throws(() => check(value), Error); 271 | }); 272 | }); 273 | --------------------------------------------------------------------------------