Comparing version 1.1.2 to 1.2.0
124
lib/rules.js
@@ -59,3 +59,9 @@ /** | ||
defaultErrorMessage: function(x, val) { return 'Value was greater than the configured maximum (' + val + ')'; }, | ||
expectedTypes: ['json', 'ref', 'number'] | ||
expectedTypes: ['json', 'ref', 'number'], | ||
checkConfig: function(constraint) { | ||
if (typeof constraint !== 'number') { | ||
return 'Maximum must be specified as a number; instead got `' + util.inspect(constraint) + '`.'; | ||
} | ||
return false; | ||
} | ||
}, | ||
@@ -68,3 +74,9 @@ 'min': { | ||
defaultErrorMessage: function(x, val) { return 'Value was less than the configured minimum (' + val + ')'; }, | ||
expectedTypes: ['json', 'ref', 'number'] | ||
expectedTypes: ['json', 'ref', 'number'], | ||
checkConfig: function(constraint) { | ||
if (typeof constraint !== 'number') { | ||
return 'Minimum must be specified as a number; instead got `' + util.inspect(constraint) + '`.'; | ||
} | ||
return false; | ||
} | ||
}, | ||
@@ -77,12 +89,41 @@ | ||
'isAfter': { | ||
fn: validator.isAfter, | ||
fn: function(x, val) { | ||
if (!_.isNaN(x)) { | ||
x = new Date(x); | ||
} | ||
if (!_.isNaN(val)) { | ||
val = new Date(val); | ||
} | ||
return validator.isAfter(x, val); | ||
}, | ||
expectedTypes: ['json', 'ref', 'string', 'number'], | ||
defaultErrorMessage: function(x, val) { return 'Value was before the configured time (' + val + ')'; }, | ||
ignoreEmptyString: true | ||
ignoreEmptyString: true, | ||
checkConfig: function(constraint) { | ||
if (isNaN(constraint) && _.isNull(validator.toDate(constraint))) { | ||
return 'Date must be specified as a timestamp (number), a parsable date string, or a JavaScript date object; instead got `' + util.inspect(constraint) + '`.'; | ||
} | ||
return false; | ||
} | ||
}, | ||
'isBefore': { | ||
fn: validator.isBefore, | ||
fn: function(x, val) { | ||
if (!_.isNaN(x)) { | ||
x = new Date(x); | ||
} | ||
if (!_.isNaN(val)) { | ||
val = new Date(val); | ||
} | ||
return validator.isBefore(x, val); | ||
}, | ||
expectedTypes: ['json', 'ref', 'string', 'number'], | ||
defaultErrorMessage: function(x, val) { return 'Value was after the configured time (' + val + ')'; }, | ||
ignoreEmptyString: true | ||
ignoreEmptyString: true, | ||
checkConfig: function(constraint) { | ||
if (isNaN(constraint) && _.isNull(validator.toDate(constraint))) { | ||
return 'Date must be specified as a timestamp (number), a parsable date string, or a JavaScript date object; instead got `' + util.inspect(constraint) + '`.'; | ||
} | ||
return false; | ||
} | ||
}, | ||
@@ -111,3 +152,10 @@ 'isCreditCard': { | ||
defaultErrorMessage: function(x, val) { return 'Value was not in the configured whitelist (' + val.join(', ') + ')'; }, | ||
ignoreEmptyString: true | ||
ignoreEmptyString: true, | ||
checkConfig: function(constraint) { | ||
if (!_.isArray(constraint)) { | ||
return 'Allowable values must be specified as an array; instead got `' + util.inspect(constraint) + '`.'; | ||
} | ||
return false; | ||
} | ||
}, | ||
@@ -126,3 +174,9 @@ 'isIP': { | ||
defaultErrorMessage: function(x, val) { return 'Value was in the configured blacklist (' + val.join(', ') + ')'; }, | ||
ignoreEmptyString: true | ||
ignoreEmptyString: true, | ||
checkConfig: function(constraint) { | ||
if (!_.isArray(constraint)) { | ||
return 'Blacklisted values must be specified as an array; instead got `' + util.inspect(constraint) + '`.'; | ||
} | ||
return false; | ||
} | ||
}, | ||
@@ -151,3 +205,9 @@ 'isURL': { | ||
defaultErrorMessage: function(x, val) { return 'Value was shorter than the configured minimum length (' + val + ')'; }, | ||
ignoreEmptyString: true | ||
ignoreEmptyString: true, | ||
checkConfig: function(constraint) { | ||
if (typeof constraint !== 'number' && parseInt(constraint) !== constraint) { | ||
return 'Minimum length must be specified as an integer; instead got `' + util.inspect(constraint) + '`.'; | ||
} | ||
return false; | ||
} | ||
}, | ||
@@ -161,3 +221,9 @@ 'maxLength': { | ||
defaultErrorMessage: function(x, val) { return 'Value was longer than the configured maximum length (' + val + ')'; }, | ||
ignoreEmptyString: true | ||
ignoreEmptyString: true, | ||
checkConfig: function(constraint) { | ||
if (typeof constraint !== 'number' && parseInt(constraint) !== constraint) { | ||
return 'Maximum length must be specified as an integer; instead got `' + util.inspect(constraint) + '`.'; | ||
} | ||
return false; | ||
} | ||
}, | ||
@@ -167,5 +233,2 @@ | ||
fn: function(x, regex) { | ||
if (!_.isRegExp(regex)) { | ||
throw new Error('This rule expects a regular expression to be configured, but instead got the ' + typeof regex + ' `' + util.inspect(regex) + '`.'); | ||
} | ||
return validator.matches(x, regex); | ||
@@ -175,3 +238,10 @@ }, | ||
expectedTypes: ['json', 'ref', 'string'], | ||
ignoreEmptyString: true | ||
ignoreEmptyString: true, | ||
checkConfig: function(constraint) { | ||
if (!_.isRegExp(constraint)) { | ||
return 'Expected a regular expression as the constraint; instead got `' + util.inspect(constraint) + '`.'; | ||
} | ||
return false; | ||
} | ||
}, | ||
@@ -198,2 +268,9 @@ // // ┬─┐┌─┐ ┬┌─┐┌─┐┌┬┐ ┌┐┌┬ ┬┬ ┬ | ||
defaultErrorMessage: 'Value failed custom validation.', | ||
checkConfig: function(constraint) { | ||
if (!_.isFunction(constraint)) { | ||
return 'Expected a function as the constraint; instead got `' + util.inspect(constraint) + '`.'; | ||
} | ||
return false; | ||
} | ||
} | ||
@@ -204,3 +281,3 @@ | ||
// Wrap a rule in a function that handles nulls and empty strings as requested, | ||
// and adds an `acceptedTypes` array that users of the rule can check to see | ||
// and adds an `expectedTypes` array that users of the rule can check to see | ||
// if their value is of a type that the rule is designed to handle. Note that | ||
@@ -210,3 +287,3 @@ // this list of types is not necessarily validated in the rule itself; that is, | ||
// will automatically kick out numbers (it might stringify them). It's up to | ||
// you to decide whether to run the validation based on its `acceptedTypes`. | ||
// you to decide whether to run the validation based on its `expectedTypes`. | ||
module.exports = _.reduce(rules, function createRule(memo, rule, ruleName) { | ||
@@ -240,3 +317,16 @@ | ||
// Set the `acceptedTypes` property of the wrapped function. | ||
// If the rule doesn't declare its own config-checker, assume that the constraint is supposed to be `true`. | ||
// This is the case for most of the `is` rules like `isBoolean`, `isCreditCard`, `isEmail`, etc. | ||
if (_.isUndefined(rule.checkConfig)) { | ||
wrappedRule.checkConfig = function (constraint) { | ||
if (constraint !== true) { | ||
return 'This validation only accepts `true` as a constraint. Instead, saw `' + constraint + '`.'; | ||
} | ||
return false; | ||
}; | ||
} else { | ||
wrappedRule.checkConfig = rule.checkConfig; | ||
} | ||
// Set the `expectedTypes` property of the wrapped function. | ||
wrappedRule.expectedTypes = rule.expectedTypes; | ||
@@ -243,0 +333,0 @@ |
{ | ||
"name": "anchor", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "High-level validation library for Node.js (used in Waterline)", | ||
@@ -5,0 +5,0 @@ "homepage": "https://sailsjs.com", |
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
34497
534