anchor
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -23,2 +23,5 @@ /** | ||
// TODO: In next major version, remove this: It should definitely not be here. | ||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
// Normalize the value if it looks like a boolean | ||
@@ -32,2 +35,3 @@ if(ruleset[rule] === 'true') { | ||
} | ||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
@@ -34,0 +38,0 @@ // If the value is false, then we shouldn't even run the validation |
128
lib/rules.js
@@ -87,10 +87,23 @@ /** | ||
'isAfter': { | ||
fn: function(x, val) { | ||
if (!_.isNaN(x)) { | ||
x = new Date(x); | ||
fn: function(x, constraint) { | ||
var normalizedX; | ||
if (_.isNumber(x)) { | ||
normalizedX = new Date(x).getTime(); | ||
} else if (_.isDate(x)) { | ||
normalizedX = x.getTime(); | ||
} else { | ||
normalizedX = Date.parse(x); | ||
} | ||
if (!_.isNaN(val)) { | ||
val = new Date(val); | ||
var normalizedConstraint; | ||
if (_.isNumber(constraint)) { | ||
normalizedConstraint = new Date(constraint).getTime(); | ||
} else if (_.isDate(constraint)) { | ||
normalizedConstraint = constraint.getTime(); | ||
} else { | ||
normalizedConstraint = Date.parse(constraint); | ||
} | ||
return validator.isAfter(x, val); | ||
return normalizedX > normalizedConstraint; | ||
}, | ||
@@ -101,6 +114,8 @@ expectedTypes: ['json', 'ref', 'string', 'number'], | ||
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) + '`.'; | ||
var isValidConstraint = (_.isNumber(constraint) || _.isDate(constraint) || (_.isString(constraint) && _.isNull(validator.toDate(constraint)))); | ||
if (!isValidConstraint) { | ||
return 'Validation rule must be specified as a JS timestamp (number of ms since epoch), a natively-parseable date string, or a JavaScript Date instance; instead got `' + util.inspect(constraint) + '`.'; | ||
} else { | ||
return false; | ||
} | ||
return false; | ||
} | ||
@@ -110,10 +125,23 @@ | ||
'isBefore': { | ||
fn: function(x, val) { | ||
if (!_.isNaN(x)) { | ||
x = new Date(x); | ||
fn: function(x, constraint) { | ||
var normalizedX; | ||
if (_.isNumber(x)) { | ||
normalizedX = new Date(x).getTime(); | ||
} else if (_.isDate(x)) { | ||
normalizedX = x.getTime(); | ||
} else { | ||
normalizedX = Date.parse(x); | ||
} | ||
if (!_.isNaN(val)) { | ||
val = new Date(val); | ||
var normalizedConstraint; | ||
if (_.isNumber(constraint)) { | ||
normalizedConstraint = new Date(constraint).getTime(); | ||
} else if (_.isDate(constraint)) { | ||
normalizedConstraint = constraint.getTime(); | ||
} else { | ||
normalizedConstraint = Date.parse(constraint); | ||
} | ||
return validator.isBefore(x, val); | ||
return normalizedX < normalizedConstraint; | ||
}, | ||
@@ -124,10 +152,15 @@ expectedTypes: ['json', 'ref', 'string', 'number'], | ||
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) + '`.'; | ||
var isValidConstraint = (_.isNumber(constraint) || _.isDate(constraint) || (_.isString(constraint) && _.isNull(validator.toDate(constraint)))); | ||
if (!isValidConstraint) { | ||
return 'Validation rule must be specified as a JS timestamp (number of ms since epoch), a natively-parseable date string, or a JavaScript Date instance; instead got `' + util.inspect(constraint) + '`.'; | ||
} else { | ||
return false; | ||
} | ||
return false; | ||
} | ||
}, | ||
'isCreditCard': { | ||
fn: validator.isCreditCard, | ||
fn: function(x) { | ||
if (typeof x !== 'string') { throw new Error ('Value was not a string.'); } | ||
return validator.isCreditCard(x); | ||
}, | ||
expectedTypes: ['json', 'ref', 'string'], | ||
@@ -138,3 +171,6 @@ defaultErrorMessage: 'Value was not a valid credit card.', | ||
'isEmail': { | ||
fn: validator.isEmail, | ||
fn: function(x) { | ||
if (typeof x !== 'string') { throw new Error ('Value was not a string.'); } | ||
return validator.isEmail(x); | ||
}, | ||
expectedTypes: ['json', 'ref', 'string'], | ||
@@ -145,3 +181,6 @@ defaultErrorMessage: 'Value was not a valid email address.', | ||
'isHexColor': { | ||
fn: validator.isHexColor, | ||
fn: function(x) { | ||
if (typeof x !== 'string') { throw new Error ('Value was not a string.'); } | ||
return validator.isHexColor(x); | ||
}, | ||
expectedTypes: ['json', 'ref', 'string'], | ||
@@ -152,3 +191,5 @@ defaultErrorMessage: 'Value was not a valid hex color.', | ||
'isIn': { | ||
fn: validator.isIn, | ||
fn: function(x, constraint) { | ||
return _.contains(constraint, x); | ||
}, | ||
expectedTypes: ['json', 'ref', 'string', 'number'], | ||
@@ -166,3 +207,6 @@ defaultErrorMessage: function(x, val) { return 'Value was not in the configured whitelist (' + val.join(', ') + ')'; }, | ||
'isIP': { | ||
fn: validator.isIP, | ||
fn: function(x) { | ||
if (typeof x !== 'string') { throw new Error ('Value was not a string.'); } | ||
return validator.isIP(x); | ||
}, | ||
expectedTypes: ['json', 'ref', 'string'], | ||
@@ -173,4 +217,4 @@ defaultErrorMessage: 'Value was not a valid IP address.', | ||
'isNotIn': { | ||
fn: function(x, arrayOrString) { | ||
return !validator.isIn(x, arrayOrString); | ||
fn: function(x, constraint) { | ||
return !_.contains(constraint, x); | ||
}, | ||
@@ -189,2 +233,3 @@ expectedTypes: ['json', 'ref', 'string', 'number'], | ||
fn: function(x, opt) { | ||
if (typeof x !== 'string') { throw new Error ('Value was not a string.'); } | ||
return validator.isURL(x, opt === true ? undefined : opt); | ||
@@ -197,3 +242,6 @@ }, | ||
'isUUID': { | ||
fn: validator.isUUID, | ||
fn: function(x) { | ||
if (typeof x !== 'string') { throw new Error ('Value was not a string.'); } | ||
return validator.isUUID(x); | ||
}, | ||
expectedTypes: ['json', 'ref', 'string'], | ||
@@ -207,3 +255,3 @@ defaultErrorMessage: 'Value was not a valid UUID.', | ||
if (typeof x !== 'string') { throw new Error ('Value was not a string.'); } | ||
return validator.isLength(x, min); | ||
return x.length >= min; | ||
}, | ||
@@ -223,3 +271,3 @@ expectedTypes: ['json', 'ref', 'string'], | ||
if (typeof x !== 'string') { throw new Error ('Value was not a string.'); } | ||
return validator.isLength(x, 0, max); | ||
return x.length <= max; | ||
}, | ||
@@ -239,2 +287,3 @@ expectedTypes: ['json', 'ref', 'string'], | ||
fn: function(x, regex) { | ||
if (typeof x !== 'string') { throw new Error ('Value was not a string.'); } | ||
return validator.matches(x, regex); | ||
@@ -253,11 +302,2 @@ }, | ||
}, | ||
// // ┬─┐┌─┐ ┬┌─┐┌─┐┌┬┐ ┌┐┌┬ ┬┬ ┬ | ||
// // ├┬┘├┤ │├┤ │ │ ││││ ││ │ | ||
// // ┴└─└─┘└┘└─┘└─┘ ┴ ┘└┘└─┘┴─┘┴─┘ | ||
// 'isNotNull': { | ||
// fn: function(x) { | ||
// return !_.isNull(x); | ||
// }, | ||
// expectedTypes: ['json', 'ref'] | ||
// }, | ||
@@ -276,4 +316,7 @@ // ┌─┐┬ ┬┌─┐┌┬┐┌─┐┌┬┐ | ||
if (!_.isFunction(constraint)) { | ||
return 'Expected a function as the constraint; instead got `' + util.inspect(constraint) + '`.'; | ||
return 'Expected a function as the constraint; instead got `' + util.inspect(constraint) + '`. Please return `true` to indicate success, or otherwise return `false` or throw to indicate failure'; | ||
} | ||
if (constraint.constructor.name === 'AsyncFunction') { | ||
return 'Custom validation function cannot be an `async function` -- please use synchronous logic and return `true` to indicate success, or otherwise return `false` or throw to indicate failure.'; | ||
} | ||
return false; | ||
@@ -303,3 +346,3 @@ } | ||
// Don't allow empty strings unless we're explicitly ignoring them. | ||
// Allow empty strings if we're explicitly ignoring them. | ||
if (x === '' && rule.ignoreEmptyString) { | ||
@@ -314,3 +357,8 @@ return false; | ||
} catch (e) { | ||
return e.message; | ||
// console.error('ERROR:',e); | ||
if (_.isError(e)) { | ||
return e.message; | ||
} else { | ||
return String(e); | ||
} | ||
} | ||
@@ -317,0 +365,0 @@ |
{ | ||
"name": "anchor", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "High-level validation library for Node.js (used in Waterline)", | ||
@@ -37,8 +37,8 @@ "homepage": "https://sailsjs.com", | ||
"@sailshq/lodash": "^3.10.2", | ||
"validator": "4.4.0" | ||
"validator": "5.7.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "3.19.0", | ||
"eslint": "4.11.0", | ||
"mocha": "3.0.2" | ||
} | ||
} |
@@ -124,3 +124,3 @@ var testRules = require('./util/testRules.js'); | ||
describe('isBefore/isAfter date', function() { | ||
it(' should support "before" rule ', function() { | ||
it(' should support "isBefore" rule when validating Date instances (Date) vs. Date', function() { | ||
return testRules({ | ||
@@ -130,2 +130,43 @@ isBefore: new Date() | ||
}); | ||
it(' should support "isBefore" rule when validating Date instances (Date) vs. number', function() { | ||
return testRules({ | ||
isBefore: Date.now() | ||
}, new Date(Date.now() - 100000), new Date(Date.now() + 1000000)); | ||
}); | ||
it(' should support "isBefore" rule when validating js timestamps (number) vs. number', function() { | ||
return testRules({ | ||
isBefore: Date.now() | ||
}, (new Date(Date.now() - 100000)).getTime(), (new Date(Date.now() + 1000000)).getTime()); | ||
}); | ||
it(' should support "isBefore" rule when validating js timestamps (number) vs. Date', function() { | ||
return testRules({ | ||
isBefore: new Date() | ||
}, (new Date(Date.now() - 100000)).getTime(), (new Date(Date.now() + 1000000)).getTime()); | ||
}); | ||
it(' should support "isBefore" rule when validating JSON timestamps (string) vs. string', function() { | ||
return testRules({ | ||
isBefore: (new Date()).toJSON() | ||
}, (new Date(Date.now() - 100000)).toJSON(), (new Date(Date.now() + 1000000)).toJSON()); | ||
}); | ||
it(' should support "isBefore" rule when validating JSON timestamps (string) vs. number', function() { | ||
return testRules({ | ||
isBefore: Date.now() | ||
}, (new Date(Date.now() - 100000)).toJSON(), (new Date(Date.now() + 1000000)).toJSON()); | ||
}); | ||
it(' should support "isAfter" rule when validating Date instances (Date) vs. Date', function() { | ||
return testRules({ | ||
isAfter: new Date() | ||
}, new Date(Date.now() + 100000), new Date(Date.now() - 1000000)); | ||
}); | ||
it(' should support "isAfter" rule when validating js timestamps (number) vs. number', function() { | ||
return testRules({ | ||
isAfter: new Date() | ||
}, (new Date(Date.now() + 100000)).getTime(), (new Date(Date.now() - 1000000)).getTime()); | ||
}); | ||
it(' should support "isAfter" rule when validating JSON timestamps (string) vs. string', function() { | ||
return testRules({ | ||
isAfter: (new Date()).toJSON() | ||
}, (new Date(Date.now() + 100000)).toJSON(), (new Date(Date.now() - 1000000)).toJSON()); | ||
}); | ||
}); | ||
@@ -132,0 +173,0 @@ |
@@ -0,1 +1,3 @@ | ||
var util = require('util'); | ||
var _ = require('@sailshq/lodash'); | ||
var anchor = require('../../index.js'); | ||
@@ -9,20 +11,13 @@ | ||
// (not a good production usage pattern-- just here for testing) | ||
var exampleOutcome, nonexampleOutcome; | ||
// Should be falsy | ||
exampleOutcome = anchor(example, rules); | ||
var exampleOutcome = anchor(example, rules); | ||
if (exampleOutcome.length > 0) { | ||
throw new Error('Valid input marked with error: '+util.inspect(exampleOutcome,{depth:null})+ '\nExample: '+util.inspect(example,{depth:null})); | ||
} | ||
// Should be an array | ||
nonexampleOutcome = anchor(nonexample, rules); | ||
if (exampleOutcome.length) { | ||
return gotErrors('Valid input marked with error!', exampleOutcome, example); | ||
var nonexampleOutcome = anchor(nonexample, rules); | ||
if (!_.isArray(nonexampleOutcome) || nonexampleOutcome.length === 0) { | ||
throw new Error('Invalid input (' + nonexample + ') allowed through. '+util.inspect(rules,{depth:null})+ '\nNon-example: '+util.inspect(nonexample,{depth:null})); | ||
} | ||
if (!nonexampleOutcome.length) { | ||
return gotErrors('Invalid input (' + nonexample + ') allowed through.', rules, nonexample); | ||
} | ||
function gotErrors (errMsg, err, data) { | ||
throw new Error(errMsg); | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
39717
615
+ Addedvalidator@5.7.0(transitive)
- Removedvalidator@4.4.0(transitive)
Updatedvalidator@5.7.0