Comparing version 5.2.0 to 6.0.0
@@ -29,7 +29,7 @@ /** | ||
// Strip out "E_INVALID_TYPE" errors- they are ok if we're just coercing. | ||
_.remove(errors, {code: 'E_INVALID_TYPE'}); | ||
// Strip out "E_NOT_STRICTLY_VALID" errors- they are ok if we're just coercing. | ||
_.remove(errors, {code: 'E_NOT_STRICTLY_VALID'}); | ||
// Strip out "E_COERCION" errors- they are ok if we're just coercing. | ||
_.remove(errors, {code: 'E_COERCION'}); | ||
// Strip out "E_NOT_EVEN_CLOSE" errors- they are ok if we're just coercing. | ||
_.remove(errors, {code: 'E_NOT_EVEN_CLOSE'}); | ||
@@ -36,0 +36,0 @@ // TODO: |
@@ -16,8 +16,42 @@ /** | ||
module.exports = function consolidateErrors (errors, msgSuffix) { | ||
// If there are still errors, coallesce the remaining list of errors into a single | ||
// Error object we can throw. | ||
if (errors.length === 0) return; | ||
var err = new Error(util.format('%d error(s) %s:\n', errors.length, msgSuffix||'', errors)); | ||
err.code = errors[0].code; | ||
// If any of the errors are not "minor", then this is not a "minor" error. | ||
// If there are errors, coallesce them into a single Error object we can throw. | ||
if (errors.length === 0) { | ||
return; | ||
} | ||
// Remove duplicate E_NOT_EVEN_CLOSE / E_NOT_STRICTLY_VALID errors. | ||
var uniqueErrors = _.uniq(errors, function disregardValidationErrCode(err){ | ||
var hash = ''; | ||
if (!err.code) { | ||
hash += '?'; | ||
} | ||
else if (err.code !== 'E_NOT_EVEN_CLOSE' && err.code !== 'E_NOT_STRICTLY_VALID') { | ||
hash += err.code; | ||
} | ||
hash += err.expected; | ||
if (err.keyName) { | ||
hash += err.keyName; | ||
} | ||
return hash; | ||
}); | ||
var errMsg = util.format( | ||
'%d error%s%s:', | ||
uniqueErrors.length, (uniqueErrors.length!==1?'s':''), (msgSuffix?(' '+msgSuffix):''), | ||
( | ||
'\n • '+ _.pluck(uniqueErrors, 'message').join('\n • ') | ||
) | ||
); | ||
var err = new Error(errMsg); | ||
// Determine the appropriate top-level error code. | ||
if (_.any(uniqueErrors, { code: 'E_UNKNOWN_TYPE' })) { | ||
err.code = 'E_UNKNOWN_TYPE'; | ||
} | ||
else { | ||
err.code = 'E_INVALID'; | ||
} | ||
// If any of the original errors are not "minor", then this is not a "minor" error. | ||
err.minor = _.reduce(errors, function(memo, subError) { | ||
@@ -29,4 +63,8 @@ if (!memo || !subError.minor) { | ||
}, true); | ||
// Don't include `minor` property if it's falsy. | ||
if (!err.minor) delete err.minor; | ||
err.errors = errors; | ||
// Expose duplicate-free list of errors as `err.errors` | ||
err.errors = uniqueErrors; | ||
return err; | ||
@@ -33,0 +71,0 @@ }; |
@@ -10,2 +10,4 @@ /** | ||
var rebuildSanitized = require('./sanitize'); | ||
var compile = require('../compile'); | ||
var getDisplayType = require('../get-display-type'); | ||
@@ -104,8 +106,12 @@ | ||
// Build an E_INVALID_TYPE error | ||
// Build an E_NOT_STRICTLY_VALID error | ||
var newErr = (function (){ | ||
var msg = util.format( | ||
'An invalid value was specified: \n' + util.inspect(actual, false, null) + '\n\n' + | ||
'This doesn\'t match the specified type: \n' + util.inspect(expected, false, null) | ||
); | ||
var msg = ''; | ||
if (_.isUndefined(actual)) { | ||
msg += 'Expecting ' + compile(expected) + ' (but got `undefined`)'; | ||
} | ||
else { | ||
msg += 'Specified value (a ' + getDisplayType(actual) + ': '+compile(actual)+') '; | ||
msg += 'doesn\'t match the expected '+(_.isObject(expected)?getDisplayType(expected)+' ':'')+'type'+(_.isObject(expected)?' schema ':'')+ ': ' + compile(expected) + ''; | ||
} | ||
if (meta && meta.keyName) { | ||
@@ -116,7 +122,7 @@ msg = 'For key `'+meta.keyName+'`: ' + msg; | ||
if (meta && meta.keyName) { | ||
err.inputKey = meta.keyName; | ||
err.keyName = meta.keyName; | ||
} | ||
err.actual = util.inspect(actual, false, null); | ||
err.expected = util.inspect(expected, false, null); | ||
err.code = 'E_INVALID_TYPE'; | ||
err.actual = actual; | ||
err.expected = expected; | ||
err.code = 'E_NOT_STRICTLY_VALID'; | ||
@@ -150,6 +156,11 @@ // This is considered a "minor" error if it can be coerced without | ||
errors.push((function (){ | ||
var msg = util.format( | ||
'An invalid value was specified: \n' + util.inspect(actual, false, null) + '\n\n' + | ||
'This cannot be coerced into the specified type: \n' + util.inspect(expected, false, null) | ||
); | ||
var msg = ''; | ||
if (_.isUndefined(actual)) { | ||
msg += 'Expecting ' + compile(expected) + ' (but got `undefined`)'; | ||
} | ||
else { | ||
msg += 'Specified value (a ' + getDisplayType(actual) + ': '+compile(actual)+ ') '; | ||
msg += 'doesn\'t match the expected '+(_.isObject(expected)?getDisplayType(expected)+' ':'')+'type'+(_.isObject(expected)?' schema ':'')+ ': ' + compile(expected) + ''; | ||
} | ||
msg += ' Attempted to coerce the specified value to match, but it wasn\'t similar enough to the expected value.'; | ||
if (meta && meta.keyName) { | ||
@@ -160,7 +171,7 @@ msg = 'For key `'+meta.keyName+'`: ' + msg; | ||
if (meta && meta.keyName) { | ||
err.inputKey = meta.keyName; | ||
err.keyName = meta.keyName; | ||
} | ||
err.actual = util.inspect(actual, false, null); | ||
err.expected = util.inspect(expected, false, null); | ||
err.code = 'E_COERCION'; | ||
err.actual = actual; | ||
err.expected = expected; | ||
err.code = 'E_NOT_EVEN_CLOSE'; | ||
return err; | ||
@@ -170,5 +181,2 @@ })()); | ||
} | ||
// else { | ||
// console.log('actual:',actual,' IS EXPECTED TYPE.'); | ||
// } | ||
@@ -175,0 +183,0 @@ // Build partial result |
{ | ||
"name": "rttc", | ||
"version": "5.2.0", | ||
"version": "6.0.0", | ||
"description": "Runtime type-checking for JavaScript.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -518,7 +518,7 @@ # rttc | ||
If value cannot be properly coerced, throws error with its `.code` property set to `E_INVALID_TYPE`: | ||
If value cannot be properly coerced, throws error with its `.code` property set to `E_INVALID`: | ||
```javascript | ||
rttc.validate('number', 'asdf'); | ||
// throws E_INVALID_TYPE | ||
// throws E_INVALID | ||
``` | ||
@@ -525,0 +525,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
165277
3364