validator
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -192,9 +192,9 @@ var net = require('net'); | ||
var result = pattern.exec(this.str); | ||
if (!result || result.length != 4 ) { | ||
return this.error(this.msg || 'Not a date'); | ||
} | ||
var dt = new Date(this.str); | ||
if ( dt.getFullYear() != parseInt(result[3]) | ||
@@ -204,20 +204,29 @@ || dt.getMonth() + 1 != parseInt(result[1]) | ||
) { | ||
return this.error(this.msg || 'Not a date'); | ||
return this.error(this.msg || 'Not a date'); | ||
} | ||
return this; | ||
}; | ||
} | ||
Validator.prototype.in = function(options) { | ||
//Syntactic sugar for string-in-string | ||
if (options && typeof options.indexOf === 'function') { | ||
if (! ~options.indexOf(this.str)) { | ||
return this.error(this.msg || 'Unexpected value'); | ||
} | ||
return this; | ||
if (!~options.indexOf(this.str)) { | ||
return this.error(this.msg || 'Unexpected value'); | ||
} | ||
return this; | ||
} else { | ||
return this.error(this.msg || 'Invalid in() argument'); | ||
} | ||
else { | ||
return this.error(this.msg || 'Invalid in() argument'); | ||
} | ||
Validator.prototype.notIn = function(options) { | ||
if (options && typeof options.indexOf === 'function') { | ||
if (options.indexOf(this.str) !== -1) { | ||
return this.error(this.msg || 'Unexpected value'); | ||
} | ||
return this; | ||
} else { | ||
return this.error(this.msg || 'Invalid notIn() argument'); | ||
} | ||
}; | ||
} | ||
{ "name" : "validator", | ||
"description" : "Data validation, filtering and sanitization for node.js", | ||
"version" : "0.2.4", | ||
"version" : "0.2.5", | ||
"homepage" : "http://github.com/chriso/node-validator", | ||
@@ -5,0 +5,0 @@ "keywords" : ["validator", "validation", "assert", "params", "sanitization", "xss", "entities", "sanitize", "sanitisation", "input"], |
@@ -75,3 +75,5 @@ **node-validator is a library of string validation, filtering and sanitization methods.** | ||
len(min, max) //max is optional | ||
isDate(date) | ||
in(options) //Accepts an array or string | ||
notIn(options) | ||
@@ -78,0 +80,0 @@ ## List of sanitization / filter methods |
@@ -394,3 +394,3 @@ var node_validator = require('../lib'), | ||
}, | ||
'test #in(options)': function () { | ||
@@ -400,5 +400,5 @@ | ||
assert.ok(Validator.check('foo').in('I love football')); | ||
assert.ok(Validator.check('foo').in(['foo', 'bar', 'baz'])); | ||
assert.throws(function() { | ||
@@ -408,3 +408,3 @@ Validator.check('foo').in(['bar', 'baz']); | ||
); | ||
assert.throws(function() { | ||
@@ -414,3 +414,3 @@ Validator.check('foo').in('bar, baz'); | ||
); | ||
assert.throws(function() { | ||
@@ -420,3 +420,3 @@ Validator.check('foo').in(1234567); | ||
); | ||
assert.throws(function() { | ||
@@ -426,6 +426,35 @@ Validator.check('foo').in({foo:"foo",bar:"bar"}); | ||
); | ||
}, | ||
'test #notIn(options)': function () { | ||
assert.ok(Validator.check('foo').notIn('bar')); | ||
assert.ok(Validator.check('foo').notIn('awesome')); | ||
assert.ok(Validator.check('foo').notIn(['foobar', 'bar', 'baz'])); | ||
assert.throws(function() { | ||
Validator.check('foo').notIn(['foo', 'bar', 'baz']); | ||
}, /unexpected/i | ||
); | ||
assert.throws(function() { | ||
Validator.check('foo').notIn('foobar'); | ||
}, /unexpected/i | ||
); | ||
assert.throws(function() { | ||
Validator.check('foo').notIn(1234567); | ||
}, /invalid/i | ||
); | ||
assert.throws(function() { | ||
Validator.check('foo').notIn({foo:"foo",bar:"bar"}); | ||
}, /invalid/i | ||
); | ||
} | ||
} | ||
@@ -721,2 +721,24 @@ /*! | ||
Validator.prototype.in = function(options) { | ||
if (options && typeof options.indexOf === 'function') { | ||
if (!~options.indexOf(this.str)) { | ||
return this.error(this.msg || 'Unexpected value'); | ||
} | ||
return this; | ||
} else { | ||
return this.error(this.msg || 'Invalid in() argument'); | ||
} | ||
} | ||
Validator.prototype.notIn = function(options) { | ||
if (options && typeof options.indexOf === 'function') { | ||
if (options.indexOf(this.str) !== -1) { | ||
return this.error(this.msg || 'Unexpected value'); | ||
} | ||
return this; | ||
} else { | ||
return this.error(this.msg || 'Invalid notIn() argument'); | ||
} | ||
} | ||
var Filter = exports.Filter = function() {} | ||
@@ -723,0 +745,0 @@ |
98049
2039
170