check-types-mini
Advanced tools
Comparing version 2.0.3 to 2.1.0
@@ -7,2 +7,6 @@ # Change Log | ||
## [2.1.0] - 2017-06-18 | ||
### Added | ||
- ✨ Now, the errors which are caused by misconfiguration of the `check-types-mini` itself will reference it as a source of an error. Once this library is configured correctly, then the errors can be personalised as per `opts.msg`. | ||
## [2.0.0] - 2017-06-12 | ||
@@ -49,1 +53,2 @@ ### Changed | ||
[2.0.0]: https://github.com/codsen/check-types-mini/compare/v1.6.0...v2.0.0 | ||
[2.1.0]: https://github.com/codsen/check-types-mini/compare/v2.0.0...v2.1.0 |
67
index.js
@@ -10,16 +10,16 @@ 'use strict' | ||
const arrayiffyIfString = require('arrayiffy-if-string') | ||
const isObj = require('lodash.isplainobject') | ||
function checkTypes (obj, ref, opts) { | ||
const NAMESFORANYTYPE = ['any', 'anything', 'every', 'everything', 'all', 'whatever', 'whatevs'] | ||
function existy (x) { return x != null } | ||
function isBool (something) { return type(something) === 'boolean' } | ||
function isStr (something) { return type(something) === 'string' } | ||
const NAMESFORANYTYPE = ['any', 'anything', 'every', 'everything', 'all', 'whatever', 'whatevs'] | ||
const isArr = Array.isArray | ||
if (arguments.length === 0) { | ||
throw new Error('check-types-mini/checkTypes(): [THROW_ID_01] missing first two arguments!') | ||
throw new Error(`check-types-mini/checkTypes(): Missing all arguments!`) | ||
} | ||
if (arguments.length === 1) { | ||
throw new Error('check-types-mini/checkTypes(): [THROW_ID_02] missing second argument!') | ||
throw new Error(`check-types-mini/checkTypes(): Missing second argument!`) | ||
} | ||
@@ -33,11 +33,19 @@ | ||
schema: {}, | ||
msg: '', | ||
optsVarName: 'opts.' | ||
msg: 'check-types-mini/checkTypes()', | ||
optsVarName: 'opts' | ||
} | ||
opts = objectAssign(clone(defaults), opts) | ||
if (existy(opts) && isObj(opts)) { | ||
opts = objectAssign(clone(defaults), opts) | ||
} else { | ||
opts = clone(defaults) | ||
} | ||
if (!isStr(opts.msg)) { | ||
throw new Error(`check-types-mini/checkTypes(): [THROW_ID_07] opts.msg must be string! Currently it's: ${type(opts.msg)}, equal to ${JSON.stringify(opts.msg, null, 4)}`) | ||
throw new Error(`check-types-mini/checkTypes(): opts.msg must be string! Currently it's: ${type(opts.msg)}, equal to ${JSON.stringify(opts.msg, null, 4)}`) | ||
} | ||
opts.msg = opts.msg.trim() | ||
if (opts.msg[opts.msg.length - 1] === ':') { | ||
opts.msg = opts.msg.slice(0, opts.msg.length - 1) | ||
} | ||
if (!isStr(opts.optsVarName)) { | ||
throw new Error(`check-types-mini/checkTypes(): [THROW_ID_08] opts.optsVarName must be string! Currently it's: ${type(opts.optsVarName)}, equal to ${JSON.stringify(opts.optsVarName, null, 4)}`) | ||
throw new Error(`check-types-mini/checkTypes(): opts.optsVarName must be string! Currently it's: ${type(opts.optsVarName)}, equal to ${JSON.stringify(opts.optsVarName, null, 4)}`) | ||
} | ||
@@ -49,29 +57,26 @@ | ||
Object.keys(opts.schema).forEach(function (oneKey) { | ||
if (!isArr(opts.schema[oneKey])) { | ||
opts.schema[oneKey] = [opts.schema[oneKey]] | ||
} | ||
// then turn all keys into strings and trim and lowercase them: | ||
opts.schema[oneKey] = opts.schema[oneKey].map(String).map(el => el.toLowerCase()).map(el => el.trim()) | ||
}) | ||
if (!isArr(opts.ignoreKeys)) { | ||
throw new TypeError(`check-types-mini/checkTypes(): [THROW_ID_03] opts.ignoreKeys should be an array, currently it's: ${type(opts.ignoreKeys)}`) | ||
throw new TypeError(`check-types-mini/checkTypes(): opts.ignoreKeys should be an array, currently it's: ${type(opts.ignoreKeys)}`) | ||
} | ||
if (!isBool(opts.acceptArrays)) { | ||
throw new TypeError(`check-types-mini/checkTypes(): [THROW_ID_04] opts.acceptArrays should be a Boolean, currently it's: ${type(opts.acceptArrays)}`) | ||
throw new TypeError(`check-types-mini/checkTypes(): opts.acceptArrays should be a Boolean, currently it's: ${type(opts.acceptArrays)}`) | ||
} | ||
if (!isArr(opts.acceptArraysIgnore)) { | ||
throw new TypeError(`check-types-mini/checkTypes(): [THROW_ID_05] opts.acceptArraysIgnore should be an array, currently it's: ${type(opts.acceptArraysIgnore)}`) | ||
throw new TypeError(`check-types-mini/checkTypes(): opts.acceptArraysIgnore should be an array, currently it's: ${type(opts.acceptArraysIgnore)}`) | ||
} | ||
if (!isBool(opts.enforceStrictKeyset)) { | ||
throw new TypeError(`check-types-mini/checkTypes(): [THROW_ID_06] opts.enforceStrictKeyset should be a Boolean, currently it's: ${type(opts.enforceStrictKeyset)}`) | ||
throw new TypeError(`check-types-mini/checkTypes(): opts.enforceStrictKeyset should be a Boolean, currently it's: ${type(opts.enforceStrictKeyset)}`) | ||
} | ||
Object.keys(opts.schema).forEach(function (oneKey) { | ||
if (!isArr(opts.schema[oneKey])) { | ||
opts.schema[oneKey] = [opts.schema[oneKey]] | ||
} | ||
// then turn all keys into strings and trim and lowercase them: | ||
opts.schema[oneKey] = opts.schema[oneKey].map(String).map(el => el.toLowerCase()).map(el => el.trim()) | ||
}) | ||
if (!existy(ref)) { | ||
ref = {} | ||
} | ||
if (opts.msg.length > 0) { | ||
opts.msg = opts.msg.trim() + ' ' | ||
} | ||
@@ -81,3 +86,3 @@ if (opts.enforceStrictKeyset) { | ||
if (pullAll(Object.keys(obj), clone(Object.keys(ref)).concat(Object.keys(opts.schema))).length !== 0) { | ||
throw new TypeError(`check-types-mini/checkTypes(): [THROW_ID_09] opts.enforceStrictKeyset is on and the following keys are not covered by schema and/or reference objects: ${JSON.stringify(pullAll(Object.keys(obj), clone(Object.keys(ref)).concat(Object.keys(opts.schema))), null, 4)}`) | ||
throw new TypeError(`${opts.msg}: ${opts.optsVarName}.enforceStrictKeyset is on and the following keys are not covered by schema and/or reference objects: ${JSON.stringify(pullAll(Object.keys(obj), clone(Object.keys(ref)).concat(Object.keys(opts.schema))), null, 4)}`) | ||
} | ||
@@ -87,9 +92,9 @@ } else { | ||
if (pullAll(Object.keys(obj), Object.keys(ref)).length !== 0) { | ||
throw new TypeError(`check-types-mini/checkTypes(): [THROW_ID_10] The input object has keys that are not covered by reference object: ${JSON.stringify(pullAll(Object.keys(obj), Object.keys(ref)), null, 4)}`) | ||
throw new TypeError(`${opts.msg}: The input object has keys that are not covered by reference object: ${JSON.stringify(pullAll(Object.keys(obj), Object.keys(ref)), null, 4)}`) | ||
} else if (pullAll(Object.keys(ref), Object.keys(obj)).length !== 0) { | ||
throw new TypeError(`check-types-mini/checkTypes(): [THROW_ID_11] The reference object has keys that are not present in the input object: ${JSON.stringify(pullAll(Object.keys(ref), Object.keys(obj)), null, 4)}`) | ||
throw new TypeError(`${opts.msg}: The reference object has keys that are not present in the input object: ${JSON.stringify(pullAll(Object.keys(ref), Object.keys(obj)), null, 4)}`) | ||
} | ||
} else { | ||
// it's an error because both schema and reference don't exist | ||
throw new TypeError(`check-types-mini/checkTypes(): [THROW_ID_12] Both opts.schema and reference objects are missing! We don't have anything to match the keys as you requested via opts.enforceStrictKeyset!`) | ||
throw new TypeError(`${opts.msg}: Both ${opts.optsVarName}.schema and reference objects are missing! We don't have anything to match the keys as you requested via opts.enforceStrictKeyset!`) | ||
} | ||
@@ -109,3 +114,3 @@ } | ||
if (!includes(opts.schema[key], type(obj[key]).toLowerCase())) { | ||
throw new TypeError(`${opts.msg}${opts.optsVarName}${key} was customised to ${JSON.stringify(obj[key], null, 4)} which is not among the allowed types in schema (${opts.schema[key]}) but ${type(obj[key])}`) | ||
throw new TypeError(`${opts.msg}: ${opts.optsVarName}.${key} was customised to ${JSON.stringify(obj[key], null, 4)} which is not among the allowed types in schema (${opts.schema[key]}) but ${type(obj[key])}`) | ||
} | ||
@@ -125,6 +130,6 @@ } | ||
if (!allMatch) { | ||
throw new TypeError(`${opts.msg}${opts.optsVarName}${key} was customised to be array, but not all of its elements are ${type(ref[key])}-type`) | ||
throw new TypeError(`${opts.msg}: ${opts.optsVarName}.${key} was customised to be array, but not all of its elements are ${type(ref[key])}-type`) | ||
} | ||
} else { | ||
throw new TypeError(`${opts.msg}${opts.optsVarName}${key} was customised to ${JSON.stringify(obj[key], null, 4)} which is not ${type(ref[key])} but ${type(obj[key])}`) | ||
throw new TypeError(`${opts.msg}: ${opts.optsVarName}.${key} was customised to ${JSON.stringify(obj[key], null, 4)} which is not ${type(ref[key])} but ${type(obj[key])}`) | ||
} | ||
@@ -131,0 +136,0 @@ } |
{ | ||
"name": "check-types-mini", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "Check the types of your options object's values after user has customised them", | ||
@@ -42,2 +42,3 @@ "main": "index.js", | ||
"lodash.intersection": "*", | ||
"lodash.isplainobject": "*", | ||
"lodash.pullall": "*", | ||
@@ -57,3 +58,3 @@ "object-assign": "*", | ||
"ava": { | ||
"verbose": true | ||
"verbose": false | ||
}, | ||
@@ -60,0 +61,0 @@ "engines": { |
34
test.js
@@ -6,12 +6,12 @@ 'use strict' | ||
test('01.01 - throws when all/first arg\'s missing', t => { | ||
test('01.01 - throws when all/first args are missing', t => { | ||
t.throws(function () { | ||
checkTypes() | ||
}, 'check-types-mini/checkTypes(): [THROW_ID_01] missing first two arguments!') | ||
}, 'check-types-mini/checkTypes(): Missing all arguments!') | ||
}) | ||
test('01.02 - throws when second arg\'s missing', t => { | ||
test('01.02 - throws when second arg is missing', t => { | ||
t.throws(function () { | ||
checkTypes('zzzz') | ||
}, 'check-types-mini/checkTypes(): [THROW_ID_02] missing second argument!') | ||
}, 'check-types-mini/checkTypes(): Missing second argument!') | ||
}) | ||
@@ -33,3 +33,3 @@ | ||
) | ||
}, 'opts.option2 was customised to "false" which is not boolean but string') | ||
}, 'check-types-mini/checkTypes(): opts.option2 was customised to "false" which is not boolean but string') | ||
}) | ||
@@ -90,7 +90,7 @@ | ||
{ | ||
msg: 'newLibrary/index.js: [THROW_ID_01]' // << no trailing space | ||
msg: 'newLibrary/index.js [THROW_ID_01]' // << no trailing space | ||
} | ||
) | ||
}, | ||
'newLibrary/index.js: [THROW_ID_01] opts.option2 was customised to "false" which is not boolean but string' | ||
'newLibrary/index.js [THROW_ID_01]: opts.option2 was customised to "false" which is not boolean but string' | ||
) | ||
@@ -110,7 +110,7 @@ t.throws(function () { | ||
{ | ||
msg: 'newLibrary/index.js: [THROW_ID_01] ' // << trailing space | ||
msg: 'newLibrary/index.js [THROW_ID_01]: ' // << trailing space | ||
} | ||
) | ||
}, | ||
'newLibrary/index.js: [THROW_ID_01] opts.option2 was customised to "false" which is not boolean but string' | ||
'newLibrary/index.js [THROW_ID_01]: opts.option2 was customised to "false" which is not boolean but string' | ||
) | ||
@@ -130,3 +130,3 @@ t.notThrows(function () { | ||
{ | ||
msg: 'newLibrary/index.js: [THROW_ID_01] ', | ||
msg: 'newLibrary/index.js [THROW_ID_01]: ', | ||
ignoreKeys: ['option2'] | ||
@@ -150,3 +150,15 @@ } | ||
}, | ||
'check-types-mini/checkTypes(): [THROW_ID_03] opts.ignoreKeys should be an array, currently it\'s: boolean') | ||
'check-types-mini/checkTypes(): opts.ignoreKeys should be an array, currently it\'s: boolean') | ||
t.throws(function () { | ||
checkTypes( | ||
{a: 'a'}, | ||
{a: 'b'}, | ||
{ | ||
msg: 1, | ||
optsVarName: 'bbb', | ||
ignoreKeys: false | ||
} | ||
) | ||
}, | ||
'check-types-mini/checkTypes(): opts.msg must be string! Currently it\'s: number, equal to 1') | ||
t.notThrows(function () { | ||
@@ -153,0 +165,0 @@ checkTypes({a: 'a'}, {a: 'b'}, |
Sorry, the diff of this file is not supported yet
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
39251
882
8
7
+ Addedlodash.isplainobject@*
+ Addedlodash.isplainobject@4.0.6(transitive)