Comparing version 0.11.2 to 0.11.3
@@ -23,3 +23,6 @@ /** | ||
module.exports = function errorFactory(value, ruleName, keyName, customMessage) { | ||
// Avoid throwing errors by defaulting the `ruleName` value | ||
if (_.isUndefined(ruleName)) { | ||
ruleName = 'unknown'; | ||
} | ||
// Construct error message | ||
@@ -34,10 +37,56 @@ var errMsg; | ||
else { | ||
// errMsg = 'Validation error: "' + value + '" '; | ||
// errMsg += keyName ? '(' + keyName + ') ' : ''; | ||
// errMsg += 'is not of type "' + ruleName + '"'; | ||
errMsg = util.format( | ||
'`%s` should be a %s (instead of "%s", which is a %s)', | ||
keyName, ruleName, value, typeof value | ||
); | ||
// Get the type of the value that was passed here. | ||
var actualType = typeof value; | ||
// In the case of a NaN, don't bother saying "instead of NaN which is a NaN", | ||
// since it doesn't give you any more information. | ||
if (typeof value === 'number' && isNaN(value)) { | ||
errMsg = util.format( | ||
// Template: | ||
'%s should be a%s %s', | ||
// Token replacements: | ||
// ------------------- | ||
// Default to just saying 'Value' if we don't have the property name of the value. | ||
keyName ? ('`' + keyName + '`') : 'Value', | ||
// Adjust the article ("a" or "an") based on whether the next word starts with a vowel. | ||
_.contains(['a','e','i','o','u'], ruleName[0]) ? 'n' : '', | ||
// Output the name of the expected data type (e.g. "string" or "boolean"). | ||
ruleName | ||
); | ||
} | ||
// In all other cases, make a nicer error message comparing what was expected | ||
// to what was actually received. | ||
else { | ||
// Attempt to stringify the passed value. | ||
var stringifiedValue; | ||
try { | ||
stringifiedValue = JSON.stringify(value); | ||
} | ||
// Fall back to just displaying the `toString()` version of the value. | ||
catch(e) { | ||
stringifiedValue = value; | ||
} | ||
errMsg = util.format( | ||
// Template: | ||
'%s should be a%s %s (instead of %s, which is a%s %s)', | ||
// Token replacements: | ||
// ------------------- | ||
// Default to just saying 'Value' if we don't have the property name of the value. | ||
keyName ? ('`' + keyName + '`') : 'Value', | ||
// Adjust the article ("a" or "an") based on whether the next word starts with a vowel. | ||
_.contains(['a','e','i','o','u'], ruleName[0]) ? 'n' : '', | ||
// Output the name of the expected data type (e.g. "string" or "boolean"). | ||
ruleName, | ||
// Output the actual value that was passed in here. | ||
stringifiedValue, | ||
// Adjust the article ("a" or "an") based on whether the next word starts with a vowel. | ||
_.contains(['a','e','i','o','u'], actualType[0]) ? 'n' : '', | ||
// Output the type of the value that was passed in. | ||
actualType | ||
); | ||
} | ||
} | ||
@@ -44,0 +93,0 @@ |
@@ -24,2 +24,10 @@ /** | ||
return !validator.isNull(x); | ||
// Note that this (^^) is probably not the expected behavior! | ||
// | ||
// Because: | ||
// ``` | ||
// require('validator').isNull([]) >> true | ||
// ``` | ||
// | ||
// This will change in a future release of anchor. | ||
}, | ||
@@ -26,0 +34,0 @@ |
{ | ||
"name": "anchor", | ||
"version": "0.11.2", | ||
"version": "0.11.3", | ||
"description": "Recursive validation library with support for objects and lists", | ||
@@ -5,0 +5,0 @@ "homepage": "http://sailsjs.org", |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
25651
658
0