Comparing version 0.1.2 to 0.1.3
33
index.js
@@ -0,12 +1,33 @@ | ||
/// <reference path="lib/node.d.ts" /> | ||
/// <reference path="lib/enforce.d.ts" /> | ||
/// <reference path="lib/enforcements/common.ts" /> | ||
/// <reference path="lib/enforcements/lists.ts" /> | ||
/// <reference path="lib/enforcements/ranges.ts" /> | ||
/// <reference path="lib/enforcements/patterns.ts" /> | ||
/// <reference path="lib/enforcements/security.ts" /> | ||
var common = require("./lib/enforcements/common"); | ||
var Enforce = require("./lib/enforce"); | ||
exports.Enforce = Enforce; | ||
var Validator = require("./lib/validator"); | ||
exports.Validator = Validator; | ||
var lists = require("./lib/enforcements/lists"); | ||
exports.lists = lists; | ||
var ranges = require("./lib/enforcements/ranges"); | ||
exports.ranges = ranges; | ||
var patterns = require("./lib/enforcements/patterns"); | ||
exports.patterns = patterns; | ||
var security = require("./lib/enforcements/security"); | ||
exports.security = security; | ||
exports.security = require("./lib/enforcements/security"); | ||
exports.patterns = require("./lib/enforcements/patterns"); | ||
exports.ranges = require("./lib/enforcements/ranges"); | ||
exports.lists = require("./lib/enforcements/lists"); | ||
//Force TypeScript compiler to output our inputs (otherwise it optimizes them away) | ||
exports.Enforce.hasOwnProperty('x'); | ||
exports.Validator.hasOwnProperty('x'); | ||
exports.lists.hasOwnProperty('x'); | ||
exports.ranges.hasOwnProperty('x'); | ||
exports.patterns.hasOwnProperty('x'); | ||
exports.security.hasOwnProperty('x'); | ||
for (var k in common) { | ||
exports[k] = common[k]; | ||
exports[k] = common[k]; | ||
} | ||
exports.Enforce = require("./lib/Enforce").Enforce; |
@@ -1,22 +0,39 @@ | ||
var ranges = require("./ranges"); | ||
/// <reference path="../node.d.ts" /> | ||
/// <reference path="../enforce.d.ts" /> | ||
/// <reference path="../validator.ts" /> | ||
/// <reference path="ranges.ts" /> | ||
var Validator = require('../validator'); | ||
var Ranges = require('./ranges'); | ||
/** | ||
* Make sure the property isn't `NULL` or `undefined`. | ||
* Note: 0 and '' will be considered valid. | ||
**/ | ||
exports.required = function (msg) { | ||
return function (v, next) { | ||
if(v === null || v === undefined) return next(msg || 'required') | ||
else return next(); | ||
}; | ||
}; | ||
function required(message) { | ||
if (typeof message === "undefined") { message = 'required'; } | ||
/** | ||
* Check if a string has zero length. Sometimes | ||
* you might want to have a property on your | ||
* model that is not required but on a specific | ||
* form it can be. | ||
**/ | ||
exports.notEmptyString = function (msg) { | ||
return ranges.length(1, undefined, msg || "empty-string"); | ||
}; | ||
return new Validator(function (value, next) { | ||
if (value === null || value === undefined) { | ||
return next(message); | ||
} | ||
return next(); | ||
}); | ||
} | ||
exports.required = required; | ||
function notEmptyString(message) { | ||
if (typeof message === "undefined") { message = 'empty-string'; } | ||
return Ranges.length(1, undefined, message); | ||
} | ||
exports.notEmptyString = notEmptyString; | ||
function sameAs(property, message) { | ||
if (typeof message === "undefined") { message = 'not-same-as'; } | ||
return new Validator(function (value, next) { | ||
if (value !== this[property]) { | ||
return next(message); | ||
} | ||
return next(); | ||
}); | ||
} | ||
exports.sameAs = sameAs; |
@@ -1,21 +0,25 @@ | ||
/** | ||
* Check if a value (number or string) is | ||
* in a list of values. | ||
**/ | ||
exports.inside = function (list, msg) { | ||
return function (v, next) { | ||
if (list.indexOf(v) >= 0) return next(); | ||
return next(msg || 'outside-list'); | ||
}; | ||
}; | ||
/// <reference path="../node.d.ts" /> | ||
/// <reference path="../enforce.d.ts" /> | ||
/// <reference path="../validator.ts" /> | ||
var Validator = require('../validator'); | ||
/** | ||
* Check if a value (number or string) is | ||
* not in a list of values. | ||
**/ | ||
exports.outside = function (list, msg) { | ||
return function (v, next) { | ||
if (list.indexOf(v) == -1) return next(); | ||
return next(msg || 'inside-list'); | ||
}; | ||
}; | ||
function inside(list, message) { | ||
if (typeof message === "undefined") { message = 'outside-list'; } | ||
return new Validator(function (value, next) { | ||
if (list.indexOf(value) >= 0) | ||
return next(); | ||
return next(message); | ||
}); | ||
} | ||
exports.inside = inside; | ||
function outside(list, message) { | ||
if (typeof message === "undefined") { message = 'inside-list'; } | ||
return new Validator(function (value, next) { | ||
if (list.indexOf(value) === -1) | ||
return next(); | ||
return next(message); | ||
}); | ||
} | ||
exports.outside = outside; | ||
@@ -0,40 +1,56 @@ | ||
/// <reference path="../node.d.ts" /> | ||
/// <reference path="../enforce.d.ts" /> | ||
/// <reference path="../validator.ts" /> | ||
var Validator = require('../validator'); | ||
function match(pattern, modifiers, message) { | ||
if (typeof modifiers === "undefined") { modifiers = 'i'; } | ||
if (typeof message === "undefined") { message = 'no-pattern-match'; } | ||
if (arguments.length == 2) { | ||
message = modifiers; | ||
modifiers = 'i'; | ||
} | ||
if (typeof pattern === 'string') { | ||
pattern = new RegExp(pattern, modifiers); | ||
} | ||
return new Validator(function (value, next) { | ||
if (typeof value === 'string' && value.match(pattern)) | ||
return next(); | ||
return next(message); | ||
}); | ||
} | ||
exports.match = match; | ||
/** | ||
* Check if a value matches a given pattern. | ||
* You can define a pattern string and regex | ||
* modifiers or just send the RegExp object | ||
* as 1st argument. | ||
**/ | ||
exports.match = function (pattern, modifiers, msg) { | ||
if (typeof pattern == "string") { | ||
pattern = new RegExp(pattern, modifiers); | ||
} | ||
return function (v, next) { | ||
if (typeof v == "string" && v.match(pattern)) return next(); | ||
return next(msg || 'no-pattern-match'); | ||
}; | ||
}; | ||
* Check if a value is an hexadecimal string | ||
* (letters from A to F and numbers). | ||
**/ | ||
function hexString(message) { | ||
if (typeof message === "undefined") { message = 'not-hex-string'; } | ||
return exports.match("^[a-f0-9]+$", "i", message); | ||
} | ||
exports.hexString = hexString; | ||
/** | ||
* Check if a value is an hexadecimal string | ||
* (letters from A to F and numbers). | ||
**/ | ||
exports.hexString = function (msg) { | ||
return exports.match("^[a-f0-9]+$", "i", msg || "not-hex-string"); | ||
}; | ||
* Check if a value is an e-mail address | ||
* (simple checking, works 99%). | ||
**/ | ||
function email(message) { | ||
if (typeof message === "undefined") { message = 'not-valid-email'; } | ||
return exports.match("^[a-z0-9\\._%\\+\\-]+@[a-z0-9\\.\\-]+\\.[a-z]{2,6}$", "i", message); | ||
} | ||
exports.email = email; | ||
/** | ||
* Check if a value is an e-mail address | ||
* (simple checking, works 99%). | ||
**/ | ||
exports.email = function (msg) { | ||
return exports.match("^[a-z0-9\\._%\\+\\-]+@[a-z0-9\\.\\-]+\\.[a-z]{2,6}$", "i", msg || "not-valid-email"); | ||
}; | ||
* Check if it's a valid IPv4 address. | ||
**/ | ||
function ipv4(message) { | ||
if (typeof message === "undefined") { message = 'not-valid-ipv4'; } | ||
var p1 = "([1-9]|1[0-9][0-9]?|2[0-4][0-9]|25[0-4])"; | ||
var p2 = "([0-9]|1[0-9][0-9]?|2[0-4][0-9]|25[0-4])"; | ||
return exports.match("^" + [p1, p2, p2, p1].join("\\.") + "$", "", message); | ||
} | ||
exports.ipv4 = ipv4; | ||
/** | ||
* Check if it's a valid IPv4 address. | ||
**/ | ||
exports.ipv4 = function (msg) { | ||
var p1 = "([1-9]|1[0-9][0-9]?|2[0-4][0-9]|25[0-4])"; | ||
var p2 = "([0-9]|1[0-9][0-9]?|2[0-4][0-9]|25[0-4])"; | ||
return exports.match("^" + [ p1, p2, p2, p1 ].join("\\.") + "$", "", msg || "not-valid-ipv4"); | ||
}; |
@@ -1,29 +0,38 @@ | ||
/** | ||
* Check if a number is between a minimum and | ||
* a maximum number. One of this constraints | ||
* can be omitted. | ||
**/ | ||
exports.number = function (min, max, msg) { | ||
return function (n, next) { | ||
if (n === undefined || n === null) return next('undefined'); | ||
if (min === undefined && n <= max) return next(); | ||
if (max === undefined && n >= min) return next(); | ||
if (n >= min && n <= max) return next(); | ||
return next(msg || 'out-of-range-number'); | ||
}; | ||
}; | ||
/// <reference path="../node.d.ts" /> | ||
/// <reference path="../node.d.ts" /> | ||
/// <reference path="../enforce.d.ts" /> | ||
/// <reference path="../validator.ts" /> | ||
var Validator = require('../validator'); | ||
/** | ||
* Check if a string length is between a minimum | ||
* and a maximum number. One of this constraints | ||
* can be omitted. | ||
**/ | ||
exports.length = function (min, max, msg) { | ||
return function (v, next) { | ||
if (v === undefined || v === null) return next('undefined'); | ||
if (min === undefined && v.length <= max) return next(); | ||
if (max === undefined && v.length >= min) return next(); | ||
if (v.length >= min && v.length <= max) return next(); | ||
return next(msg || 'out-of-range-length'); | ||
}; | ||
}; | ||
function number(min, max, message) { | ||
if (typeof message === "undefined") { message = 'out-of-range-number'; } | ||
return new Validator(function (value, next) { | ||
if (value === undefined || value === null) | ||
return next('undefined'); | ||
if (min === undefined && value <= max) | ||
return next(); | ||
if (max === undefined && value >= min) | ||
return next(); | ||
if (value >= min && value <= max) | ||
return next(); | ||
return next(message); | ||
}); | ||
} | ||
exports.number = number; | ||
function length(min, max, message) { | ||
if (typeof message === "undefined") { message = 'out-of-range-length'; } | ||
return new Validator(function (value, next) { | ||
if (value === undefined || value === null) | ||
return next('undefined'); | ||
if (min === undefined && value.length <= max) | ||
return next(); | ||
if (max === undefined && value.length >= min) | ||
return next(); | ||
if (value.length >= min && value.length <= max) | ||
return next(); | ||
return next(message); | ||
}); | ||
} | ||
exports.length = length; | ||
@@ -1,160 +0,168 @@ | ||
var patterns = require("./patterns"); | ||
/// <reference path="../node.d.ts" /> | ||
/// <reference path="../enforce.d.ts" /> | ||
/// <reference path="../validator.ts" /> | ||
/// <reference path="patterns.ts" /> | ||
var patterns = require('./patterns'); | ||
var Validator = require('../validator'); | ||
var creditcard_prefixes = { | ||
amex : [ "34", "37" ], | ||
maestro : [ "5018", "5020", "5038", "5893", "6304", "6759", "6761", "6762", "6763" ], | ||
mastercard : [ "51", "52", "53", "54", "55" ], | ||
discover4 : [ "6011" ], | ||
discover3 : [ "644", "645", "646", "647", "648", "649" ], | ||
discover2 : [ "65" ] | ||
amex: ["34", "37"], | ||
maestro: ["5018", "5020", "5038", "5893", "6304", "6759", "6761", "6762", "6763"], | ||
mastercard: ["51", "52", "53", "54", "55"], | ||
discover4: ["6011"], | ||
discover3: ["644", "645", "646", "647", "648", "649"], | ||
discover2: ["65"] | ||
}; | ||
exports.username = function () { | ||
var opts = { | ||
length : 2, | ||
expr : null | ||
}; | ||
var msg = "invalid-username"; | ||
function username(options, message) { | ||
if (typeof options === "undefined") { options = { length: 2, expr: null }; } | ||
if (typeof message === "undefined") { message = 'invalid-username'; } | ||
if (typeof options === 'string') { | ||
message = options.toString(); | ||
options = { length: 2, expr: null }; | ||
} | ||
for (var i = 0; i < arguments.length; i++) { | ||
switch (typeof arguments[i]) { | ||
case "string": | ||
msg = arguments[i]; | ||
break; | ||
case "object": | ||
for (var k in arguments[i]) { | ||
opts[k] = arguments[i][k]; | ||
} | ||
break; | ||
} | ||
} | ||
if (!options.expr) { | ||
options.expr = new RegExp("^[a-z_][a-z0-9_\\-]{" + (options.length - 1) + ",}$", "i"); | ||
} | ||
if (opts.expr === null) { | ||
opts.expr = new RegExp("^[a-z_][a-z0-9_\\-]{" + (opts.length - 1) + ",}$", "i"); | ||
} | ||
console.info(JSON.stringify({ options: options, message: message })); | ||
return patterns.match(opts.expr, "", msg); | ||
}; | ||
return patterns.match(options.expr, message); | ||
} | ||
exports.username = username; | ||
exports.password = function (checks, msg) { | ||
if (!msg) { | ||
msg = checks; | ||
checks = "luns6"; // (l)owercase, (u)ppercase, (n)umber, (s)pecial characters, (6) min length | ||
} | ||
if (!msg) { | ||
msg = "weak-password"; | ||
} | ||
var m = checks.match(/([0-9]+)/); | ||
var min_len = (m ? parseInt(m[1], 10) : null); | ||
function password(checks, message) { | ||
if (!message) { | ||
message = checks; | ||
checks = "luns6"; | ||
} | ||
return function (v, next) { | ||
if (!v) return next(msg); | ||
if (!message) { | ||
message = "weak-password"; | ||
} | ||
if (checks.indexOf("l") >= 0 && !v.match(/[a-z]/)) return next(msg); | ||
if (checks.indexOf("u") >= 0 && !v.match(/[A-Z]/)) return next(msg); | ||
if (checks.indexOf("n") >= 0 && !v.match(/[0-9]/)) return next(msg); | ||
if (checks.indexOf("s") >= 0 && !v.match(/[^a-zA-Z0-9]/)) return next(msg); | ||
if (min_len !== null && min_len > v.length) return next(msg); | ||
var m = checks.match(/([0-9]+)/); | ||
var min_len = (m ? parseInt(m[1], 10) : null); | ||
return next(); | ||
}; | ||
}; | ||
return new Validator(function (value, next) { | ||
if (!value) | ||
return next(message); | ||
exports.creditcard = function () { | ||
var types = [ "amex", "visa", "maestro", "discover", "mastercard" ]; | ||
var msg = "not-valid-creditcard"; | ||
if (checks.indexOf("l") >= 0 && !value.match(/[a-z]/)) | ||
return next(message); | ||
if (checks.indexOf("u") >= 0 && !value.match(/[A-Z]/)) | ||
return next(message); | ||
if (checks.indexOf("n") >= 0 && !value.match(/[0-9]/)) | ||
return next(message); | ||
if (checks.indexOf("s") >= 0 && !value.match(/[^a-zA-Z0-9]/)) | ||
return next(message); | ||
if (min_len !== null && min_len > value.length) | ||
return next(message); | ||
for (var i = 0; i < arguments.length; i++) { | ||
if (Array.isArray(arguments[i])) { | ||
types = arguments[i]; | ||
} else if (typeof arguments[i] == "string") { | ||
msg = arguments[i]; | ||
} | ||
} | ||
return next(); | ||
}); | ||
} | ||
exports.password = password; | ||
return function (v, next) { | ||
if (!v) return next(msg); | ||
function creditcard(types, message) { | ||
if (typeof types === "undefined") { types = ["amex", "visa", "maestro", "discover", "mastercard"]; } | ||
if (typeof message === "undefined") { message = 'not-valid-creditcard'; } | ||
return new Validator(function (v, next) { | ||
if (!v) | ||
return next(message); | ||
v = "" + v; | ||
v = "" + v; | ||
var ok = false; | ||
var ok = false; | ||
// check right now for the possible types | ||
for (var i = 0; i < types.length; i++) { | ||
switch (types[i]) { | ||
case "amex": | ||
if (v.length != 15) break; | ||
for (var i = 0; i < types.length; i++) { | ||
switch (types[i]) { | ||
case "amex": | ||
if (v.length != 15) | ||
break; | ||
ok = (creditcard_prefixes.amex.indexOf(v.substr(0, 2)) >= 0); | ||
break; | ||
case "visa": | ||
if (v.length < 13) break; | ||
if (v.length > 16) break; | ||
ok = (creditcard_prefixes["amex"].indexOf(v.substr(0, 2)) >= 0); | ||
break; | ||
case "visa": | ||
if (v.length < 13) | ||
break; | ||
if (v.length > 16) | ||
break; | ||
ok = (v[0] == "4"); | ||
break; | ||
case "maestro": | ||
if (v.length < 16) break; | ||
if (v.length > 19) break; | ||
ok = (v[0] == "4"); | ||
break; | ||
case "maestro": | ||
if (v.length < 16) | ||
break; | ||
if (v.length > 19) | ||
break; | ||
ok = (creditcard_prefixes.maestro.indexOf(v.substr(0, 4)) >= 0); | ||
break; | ||
case "mastercard": | ||
if (v.length < 16) break; | ||
if (v.length > 19) break; | ||
ok = (creditcard_prefixes["maestro"].indexOf(v.substr(0, 4)) >= 0); | ||
break; | ||
case "mastercard": | ||
if (v.length < 16) | ||
break; | ||
if (v.length > 19) | ||
break; | ||
ok = (creditcard_prefixes.mastercard.indexOf(v.substr(0, 2)) >= 0); | ||
break; | ||
case "discover": | ||
if (v.length != 16) break; | ||
ok = (creditcard_prefixes["mastercard"].indexOf(v.substr(0, 2)) >= 0); | ||
break; | ||
case "discover": | ||
if (v.length != 16) | ||
break; | ||
ok = (creditcard_prefixes.discover4.indexOf(v.substr(0, 4)) >= 0) || | ||
(creditcard_prefixes.discover3.indexOf(v.substr(0, 3)) >= 0) || | ||
(creditcard_prefixes.discover2.indexOf(v.substr(0, 2)) >= 0); | ||
ok = (creditcard_prefixes["discover4"].indexOf(v.substr(0, 4)) >= 0) || (creditcard_prefixes["discover3"].indexOf(v.substr(0, 3)) >= 0) || (creditcard_prefixes["discover2"].indexOf(v.substr(0, 2)) >= 0); | ||
if (!ok) { | ||
var prefix = +v.substr(0, 6); | ||
if (!ok) { | ||
var prefix = +v.substr(0, 6); | ||
ok = (!isNaN(prefix) && prefix >= 622126 && prefix <= 622925); | ||
} | ||
break; | ||
case "luhn": | ||
// fallback | ||
ok = true; | ||
break; | ||
} | ||
if (ok) break; | ||
} | ||
ok = (!isNaN(prefix) && prefix >= 622126 && prefix <= 622925); | ||
} | ||
break; | ||
case "luhn": | ||
// fallback | ||
ok = true; | ||
break; | ||
} | ||
if (ok) | ||
break; | ||
} | ||
if (!ok) { | ||
return next(msg); | ||
} | ||
if (!ok) { | ||
return next(message); | ||
} | ||
// it's in one of possible types, let's check Luhn | ||
var check = +v[v.length - 1]; | ||
// it's in one of possible types, let's check Luhn | ||
var check = +v[v.length - 1]; | ||
if (isNaN(check)) { | ||
return next(msg); | ||
} | ||
if (isNaN(check)) { | ||
return next(message); | ||
} | ||
var digits = v.slice(0, v.length - 1).split(''); | ||
digits.reverse(); | ||
var digits = v.slice(0, v.length - 1).split(''); | ||
digits.reverse(); | ||
for (var i = 0; i < digits.length; i++) { | ||
digits[i] = +digits[i]; | ||
if (isNaN(digits[i])) { | ||
return next(msg); | ||
} | ||
for (var i = 0; i < digits.length; i++) { | ||
digits[i] = +digits[i]; | ||
if (isNaN(digits[i])) { | ||
return next(message); | ||
} | ||
if (i % 2 === 0) { | ||
digits[i] *= 2; | ||
if (digits[i] > 9) { | ||
digits[i] -= 9; | ||
} | ||
} | ||
} | ||
if (i % 2 === 0) { | ||
digits[i] *= 2; | ||
if (digits[i] > 9) { | ||
digits[i] -= 9; | ||
} | ||
} | ||
} | ||
check += digits.reduce(function (a, b) { return a + b; }); | ||
check += digits.reduce(function (a, b) { | ||
return a + b; | ||
}); | ||
return next(check % 10 !== 0 ? msg : null); | ||
}; | ||
}; | ||
return next(check % 10 !== 0 ? message : null); | ||
}); | ||
} | ||
exports.creditcard = creditcard; | ||
@@ -5,2 +5,5 @@ { | ||
"description" : "NodeJS data validations", | ||
"contributors": [ | ||
{ "name": "Benjamin Pannell", "email": "contact@sierrasoftworks.com", "website": "https://sierrasoftworks.com" } | ||
], | ||
"keywords" : [ | ||
@@ -11,3 +14,3 @@ "enforce", | ||
], | ||
"version" : "0.1.2", | ||
"version" : "0.1.3", | ||
"license" : "MIT", | ||
@@ -24,5 +27,6 @@ "repository" : "http://github.com/dresende/node-enforce.git", | ||
"devDependencies": { | ||
"mocha" : "1.12.0", | ||
"should" : "1.2.2" | ||
"mocha" : "1.12.1", | ||
"should" : "1.2.2", | ||
"typescript" : ">= 0.9.1" | ||
} | ||
} |
@@ -54,4 +54,6 @@ ## Data Validations [![Build Status](https://secure.travis-ci.org/dresende/node-enforce.png?branch=master)](http://travis-ci.org/dresende/node-enforce) | ||
All validators accept a `msg` argument at the end. These argument is the error message returned if the | ||
validation fails. All validators return a `function` that is called by `Enforce` with the value of the property | ||
in question and a `next` callback. | ||
validation fails. All validators return a `Validator` object that is used by `Enforce` to support chaining | ||
and other functionality. `Validator` objects have a `validate` method which is called by `Enforce` with the | ||
value of the property in question, a `next` callback and an optional global context table which may be used to | ||
pass information between validators. | ||
@@ -161,1 +163,31 @@ ### Required | ||
Checks if a property matches a predefined `RegExp` object accepting valid IPv4 address. | ||
## Chaining | ||
`Enforce` supports chaining operations on all `Validator` objects, these allow you to add additional common | ||
conditions to each validation step. All chain operations return `Validator` objects, allowing you to chain | ||
multiple commands together with ease. | ||
#### ifDefined | ||
`validation.ifDefined()` | ||
Only proceedes to check the `validation` if the property's value is not `null` or `undefined`, passing validation | ||
if it is. | ||
#### ifNotEmptyString | ||
`validation.ifNotEmptyString()` | ||
Only proceedes to check the `validation` if the property's value is a `string` with a length greater than 0. | ||
#### ifType | ||
`validation.ifType(type)` | ||
Only proceedes to check the `validation` if the property's value is of the specified type. Checked with a `typeof value == type` operation. | ||
#### ifNotType | ||
`validation.ifNotType(type)` | ||
Only proceedes to check the `validation` if the property's value is not of the specified type. Checked with a `typeof value != type` operation. |
@@ -1,9 +0,8 @@ | ||
var should = require("should"); | ||
var should = require("should"); | ||
exports.checkValidation = function (done, validation_err) { | ||
return function (err) { | ||
should.equal(err, validation_err); | ||
return done(); | ||
}; | ||
return function (err) { | ||
should.equal(err, validation_err); | ||
return done(); | ||
}; | ||
}; |
@@ -16,2 +16,12 @@ var should = require("should"); | ||
}); | ||
it("should have .sameAs()", function (done) { | ||
enforce.sameAs.should.be.a("function"); | ||
return done(); | ||
}); | ||
it("should have a chainable .ifDefined() function", function (done) { | ||
enforce.required().ifDefined.should.be.a("function"); | ||
return done(); | ||
}); | ||
}); | ||
@@ -23,30 +33,38 @@ | ||
it("should pass 1", function (done) { | ||
validator(1, common.checkValidation(done)); | ||
validator.validate(1, common.checkValidation(done)); | ||
}); | ||
it("should pass 0", function (done) { | ||
validator(0, common.checkValidation(done)); | ||
validator.validate(0, common.checkValidation(done)); | ||
}); | ||
it("should pass ''", function (done) { | ||
validator('', common.checkValidation(done)); | ||
validator.validate('', common.checkValidation(done)); | ||
}); | ||
it("should pass false", function (done) { | ||
validator(false, common.checkValidation(done)); | ||
validator.validate(false, common.checkValidation(done)); | ||
}); | ||
it("should not pass null", function (done) { | ||
validator(null, common.checkValidation(done, 'required')); | ||
validator.validate(null, common.checkValidation(done, 'required')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator(undefined, common.checkValidation(done, 'required')); | ||
validator.validate(undefined, common.checkValidation(done, 'required')); | ||
}); | ||
describe("width custom error", function () { | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.required('custom-error'); | ||
it("should not pass null with 'custom-error'", function (done) { | ||
validator(null, common.checkValidation(done, 'custom-error')); | ||
validator.validate(null, common.checkValidation(done, 'custom-error')); | ||
}); | ||
@@ -60,20 +78,64 @@ }); | ||
it("should pass 'hello'", function (done) { | ||
validator('hello', common.checkValidation(done)); | ||
validator.validate('hello', common.checkValidation(done)); | ||
}); | ||
it("should pass ' '", function (done) { | ||
validator(' ', common.checkValidation(done)); | ||
validator.validate(' ', common.checkValidation(done)); | ||
}); | ||
it("should not pass ''", function (done) { | ||
validator('', common.checkValidation(done, 'empty-string')); | ||
validator.validate('', common.checkValidation(done, 'empty-string')); | ||
}); | ||
describe("width custom error", function () { | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'undefined')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'undefined')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.notEmptyString('custom-error'); | ||
it("should not pass '' with 'custom-error'", function (done) { | ||
validator('', common.checkValidation(done, 'custom-error')); | ||
validator.validate('', common.checkValidation(done, 'custom-error')); | ||
}); | ||
}); | ||
}); | ||
describe("enforce.sameAs()", function () { | ||
var validator = enforce.sameAs('other'); | ||
it("should pass 'hello' === 'hello'", function (done) { | ||
validator.validate('hello', common.checkValidation(done), { other: 'hello' }); | ||
}); | ||
it("should pass '' === ''", function (done) { | ||
validator.validate('', common.checkValidation(done), { other: '' }); | ||
}); | ||
it("should not pass '' === 0", function (done) { | ||
validator.validate('', common.checkValidation(done, 'not-same-as'), { other: 0 }); | ||
}); | ||
it("should not pass '' === undefined", function (done) { | ||
validator.validate('', common.checkValidation(done, 'not-same-as'), { other: undefined }); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.sameAs('other', 'custom-error'); | ||
it("should not pass 'hello' === '' with 'custom-error'", function (done) { | ||
validator.validate('hello', common.checkValidation(done, 'custom-error'), { other: '' }); | ||
}); | ||
}); | ||
}); |
@@ -38,4 +38,9 @@ var should = require("should"); | ||
describe(".add", function () { | ||
var checks = new enforce.Enforce(); | ||
var checks = new enforce.Enforce(); | ||
it("should have the .add() method", function (done) { | ||
checks.add.should.be.a('function'); | ||
done(); | ||
}); | ||
it("should throw if not passing a validator", function (done) { | ||
@@ -51,3 +56,3 @@ (function () { | ||
(function () { | ||
checks.add("prop", function () {}); | ||
checks.add("prop", enforce.required()); | ||
}).should.not.throwError(); | ||
@@ -57,8 +62,34 @@ | ||
}); | ||
it("should allow adding a chained validator", function (done) { | ||
(function () { | ||
checks.add("prop", enforce.notEmptyString().ifDefined()); | ||
}).should.not.throwError(); | ||
return done(); | ||
}); | ||
it("should allow adding legacy validators (<= 0.1.2)", function (done) { | ||
(function () { | ||
checks.add("prop", function (value, next) { }); | ||
}).should.not.throwError(); | ||
(function () { | ||
checks.add("prop", function (value, next, contexts) { }); | ||
}).should.not.throwError(); | ||
return done(); | ||
}); | ||
}); | ||
describe(".clear", function () { | ||
var checks = new enforce.Enforce(); | ||
checks.add("prop", function () {}); | ||
var checks = new enforce.Enforce(); | ||
it("should have a .clear() method", function (done) { | ||
checks.clear.should.be.a("function"); | ||
done(); | ||
}); | ||
checks.add("prop", enforce.required()); | ||
it("should clear all validators", function (done) { | ||
@@ -65,0 +96,0 @@ Object.keys(checks.validations).length.should.be.above(0); |
@@ -22,26 +22,42 @@ var should = require("should"); | ||
it("should pass 1", function (done) { | ||
validator(1, common.checkValidation(done)); | ||
validator.validate(1, common.checkValidation(done)); | ||
}); | ||
it("should pass 3", function (done) { | ||
validator(3, common.checkValidation(done)); | ||
validator.validate(3, common.checkValidation(done)); | ||
}); | ||
it("should not pass -1", function (done) { | ||
validator(-1, common.checkValidation(done, 'outside-list')); | ||
validator.validate(-1, common.checkValidation(done, 'outside-list')); | ||
}); | ||
it("should not pass '1'", function (done) { | ||
validator('1', common.checkValidation(done, 'outside-list')); | ||
validator.validate('1', common.checkValidation(done, 'outside-list')); | ||
}); | ||
it("should not pass []", function (done) { | ||
validator([], common.checkValidation(done, 'outside-list')); | ||
validator.validate([], common.checkValidation(done, 'outside-list')); | ||
}); | ||
describe("width custom error", function () { | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'outside-list')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'outside-list')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.lists.inside([ 1, 2, 3 ], 'custom-error'); | ||
it("should not pass -1 with 'custom-error'", function (done) { | ||
validator(-1, common.checkValidation(done, 'custom-error')); | ||
validator.validate(-1, common.checkValidation(done, 'custom-error')); | ||
}); | ||
@@ -55,28 +71,28 @@ }); | ||
it("should pass 4", function (done) { | ||
validator(4, common.checkValidation(done)); | ||
validator.validate(4, common.checkValidation(done)); | ||
}); | ||
it("should pass -2", function (done) { | ||
validator(-2, common.checkValidation(done)); | ||
validator.validate(-2, common.checkValidation(done)); | ||
}); | ||
it("should pass ''", function (done) { | ||
validator('', common.checkValidation(done)); | ||
validator.validate('', common.checkValidation(done)); | ||
}); | ||
it("should pass null", function (done) { | ||
validator(null, common.checkValidation(done)); | ||
validator.validate(null, common.checkValidation(done)); | ||
}); | ||
it("should not pass 2", function (done) { | ||
validator(2, common.checkValidation(done, 'inside-list')); | ||
validator.validate(2, common.checkValidation(done, 'inside-list')); | ||
}); | ||
describe("width custom error", function () { | ||
describe("with custom error", function () { | ||
var validator = enforce.lists.outside([ 1, 2, 3 ], 'custom-error'); | ||
it("should not pass 2 with 'custom-error'", function (done) { | ||
validator(2, common.checkValidation(done, 'custom-error')); | ||
validator.validate(2, common.checkValidation(done, 'custom-error')); | ||
}); | ||
}); | ||
}); |
@@ -26,2 +26,8 @@ var should = require("should"); | ||
}); | ||
describe("with custom error", function () { | ||
it("should fail 'abc' on /def/ with 'invalid'", function (done) { | ||
enforce.patterns.match(/def/, 'invalid').validate('abc', common.checkValidation(done, 'invalid')); | ||
}); | ||
}); | ||
}); | ||
@@ -33,22 +39,38 @@ | ||
it("should pass 'ABCDEF0123456789'", function (done) { | ||
validator('ABCDEF0123456789', common.checkValidation(done)); | ||
validator.validate('ABCDEF0123456789', common.checkValidation(done)); | ||
}); | ||
it("should pass 'abcdef0123456789'", function (done) { | ||
validator('abcdef0123456789', common.checkValidation(done)); | ||
validator.validate('abcdef0123456789', common.checkValidation(done)); | ||
}); | ||
it("should not pass 'af830g'", function (done) { | ||
validator('af830g', common.checkValidation(done, 'not-hex-string')); | ||
validator.validate('af830g', common.checkValidation(done, 'not-hex-string')); | ||
}); | ||
it("should not pass ''", function (done) { | ||
validator('', common.checkValidation(done, 'not-hex-string')); | ||
validator.validate('', common.checkValidation(done, 'not-hex-string')); | ||
}); | ||
describe("width custom error", function () { | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'not-hex-string')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'not-hex-string')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.patterns.hexString('custom-error'); | ||
it("should not pass 'af830g' with 'custom-error'", function (done) { | ||
validator('af830g', common.checkValidation(done, 'custom-error')); | ||
validator.validate('af830g', common.checkValidation(done, 'custom-error')); | ||
}); | ||
@@ -62,34 +84,50 @@ }); | ||
it("should pass 'niceandsimple@example.com'", function (done) { | ||
validator('niceandsimple@example.com', common.checkValidation(done)); | ||
validator.validate('niceandsimple@example.com', common.checkValidation(done)); | ||
}); | ||
it("should pass 'Very.Common@example.com'", function (done) { | ||
validator('Very.Common@example.com', common.checkValidation(done)); | ||
validator.validate('Very.Common@example.com', common.checkValidation(done)); | ||
}); | ||
it("should pass 'disposable.style.email.with+symbol@example.com'", function (done) { | ||
validator('disposable.style.email.with+symbol@example.com', common.checkValidation(done)); | ||
validator.validate('disposable.style.email.with+symbol@example.com', common.checkValidation(done)); | ||
}); | ||
it("should not pass 'Abc.example.com'", function (done) { | ||
validator('Abc.example.com', common.checkValidation(done, 'not-valid-email')); | ||
validator.validate('Abc.example.com', common.checkValidation(done, 'not-valid-email')); | ||
}); | ||
it("should not pass 'A@b@c@example.com'", function (done) { | ||
validator('A@b@c@example.com', common.checkValidation(done, 'not-valid-email')); | ||
validator.validate('A@b@c@example.com', common.checkValidation(done, 'not-valid-email')); | ||
}); | ||
it("should not pass 'not\\allowed@example.com'", function (done) { | ||
validator('not\\allowed@example.com', common.checkValidation(done, 'not-valid-email')); | ||
validator.validate('not\\allowed@example.com', common.checkValidation(done, 'not-valid-email')); | ||
}); | ||
it("should not pass 'abc@example'", function (done) { | ||
validator('abc@example', common.checkValidation(done, 'not-valid-email')); | ||
validator.validate('abc@example', common.checkValidation(done, 'not-valid-email')); | ||
}); | ||
describe("width custom error", function () { | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'not-valid-email')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'not-valid-email')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.patterns.email('custom-error'); | ||
it("should not pass 'abc@example' with 'custom-error'", function (done) { | ||
validator('abc@example', common.checkValidation(done, 'custom-error')); | ||
validator.validate('abc@example', common.checkValidation(done, 'custom-error')); | ||
}); | ||
@@ -103,32 +141,48 @@ }); | ||
it("should pass '1.2.3.4'", function (done) { | ||
validator('1.2.3.4', common.checkValidation(done)); | ||
validator.validate('1.2.3.4', common.checkValidation(done)); | ||
}); | ||
it("should pass '1.0.0.1'", function (done) { | ||
validator('1.0.0.1', common.checkValidation(done)); | ||
validator.validate('1.0.0.1', common.checkValidation(done)); | ||
}); | ||
it("should pass '1.10.100.254'", function (done) { | ||
validator('1.10.100.254', common.checkValidation(done)); | ||
validator.validate('1.10.100.254', common.checkValidation(done)); | ||
}); | ||
it("should not pass '1.10.100.255'", function (done) { | ||
validator('1.10.100.255', common.checkValidation(done, 'not-valid-ipv4')); | ||
validator.validate('1.10.100.255', common.checkValidation(done, 'not-valid-ipv4')); | ||
}); | ||
it("should not pass '1.10.100.0'", function (done) { | ||
validator('1.10.100.0', common.checkValidation(done, 'not-valid-ipv4')); | ||
validator.validate('1.10.100.0', common.checkValidation(done, 'not-valid-ipv4')); | ||
}); | ||
it("should not pass '0.1.2.3'", function (done) { | ||
validator('0.1.2.3', common.checkValidation(done, 'not-valid-ipv4')); | ||
validator.validate('0.1.2.3', common.checkValidation(done, 'not-valid-ipv4')); | ||
}); | ||
describe("width custom error", function () { | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'not-valid-ipv4')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'not-valid-ipv4')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.patterns.ipv4('custom-error'); | ||
it("should not pass '0.1.2.3' with 'custom-error'", function (done) { | ||
validator('0.1.2.3', common.checkValidation(done, 'custom-error')); | ||
validator.validate('0.1.2.3', common.checkValidation(done, 'custom-error')); | ||
}); | ||
}); | ||
}); |
@@ -22,14 +22,30 @@ var should = require("should"); | ||
it("should pass 5", function (done) { | ||
validator(5, common.checkValidation(done)); | ||
validator.validate(5, common.checkValidation(done)); | ||
}); | ||
it("should not pass -5", function (done) { | ||
validator(-5, common.checkValidation(done, 'out-of-range-number')); | ||
validator.validate(-5, common.checkValidation(done, 'out-of-range-number')); | ||
}); | ||
describe("width custom error", function () { | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'undefined')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'undefined')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.ranges.number(0, 10, 'custom-error'); | ||
it("should not pass -5 with 'custom-error'", function (done) { | ||
validator(-5, common.checkValidation(done, 'custom-error')); | ||
validator.validate(-5, common.checkValidation(done, 'custom-error')); | ||
}); | ||
@@ -43,26 +59,42 @@ }); | ||
it("should pass 0", function (done) { | ||
validator(0, common.checkValidation(done)); | ||
validator.validate(0, common.checkValidation(done)); | ||
}); | ||
it("should pass 5", function (done) { | ||
validator(5, common.checkValidation(done)); | ||
validator.validate(5, common.checkValidation(done)); | ||
}); | ||
it("should pass -5", function (done) { | ||
validator(-5, common.checkValidation(done)); | ||
validator.validate(-5, common.checkValidation(done)); | ||
}); | ||
it("should not pass null", function (done) { | ||
validator(null, common.checkValidation(done, 'undefined')); | ||
validator.validate(null, common.checkValidation(done, 'undefined')); | ||
}); | ||
it("should not pass 15", function (done) { | ||
validator(15, common.checkValidation(done, 'out-of-range-number')); | ||
validator.validate(15, common.checkValidation(done, 'out-of-range-number')); | ||
}); | ||
describe("width custom error", function () { | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'undefined')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'undefined')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.ranges.number(undefined, 10, 'custom-error'); | ||
it("should not pass 15 with 'custom-error'", function (done) { | ||
validator(15, common.checkValidation(done, 'custom-error')); | ||
validator.validate(15, common.checkValidation(done, 'custom-error')); | ||
}); | ||
@@ -76,26 +108,38 @@ }); | ||
it("should pass 0", function (done) { | ||
validator(0, common.checkValidation(done)); | ||
validator.validate(0, common.checkValidation(done)); | ||
}); | ||
it("should pass -5", function (done) { | ||
validator(-5, common.checkValidation(done)); | ||
validator.validate(-5, common.checkValidation(done)); | ||
}); | ||
it("should pass 5", function (done) { | ||
validator(5, common.checkValidation(done)); | ||
validator.validate(5, common.checkValidation(done)); | ||
}); | ||
it("should not pass -15", function (done) { | ||
validator.validate(-15, common.checkValidation(done, 'out-of-range-number')); | ||
}); | ||
it("should not pass null", function (done) { | ||
validator(null, common.checkValidation(done, 'undefined')); | ||
validator.validate(null, common.checkValidation(done, 'undefined')); | ||
}); | ||
it("should not pass -15", function (done) { | ||
validator(-15, common.checkValidation(done, 'out-of-range-number')); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'undefined')); | ||
}); | ||
describe("width custom error", function () { | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.ranges.number(-10, undefined, 'custom-error'); | ||
it("should not pass -15 with 'custom-error'", function (done) { | ||
validator(-15, common.checkValidation(done, 'custom-error')); | ||
validator.validate(-15, common.checkValidation(done, 'custom-error')); | ||
}); | ||
@@ -109,22 +153,34 @@ }); | ||
it("should pass 0", function (done) { | ||
validator(0, common.checkValidation(done)); | ||
validator.validate(0, common.checkValidation(done)); | ||
}); | ||
it("should pass 5", function (done) { | ||
validator(5, common.checkValidation(done)); | ||
validator.validate(5, common.checkValidation(done)); | ||
}); | ||
it("should not pass -5", function (done) { | ||
validator.validate(-5, common.checkValidation(done, 'out-of-range-number')); | ||
}); | ||
it("should not pass null", function (done) { | ||
validator(null, common.checkValidation(done, 'undefined')); | ||
validator.validate(null, common.checkValidation(done, 'undefined')); | ||
}); | ||
it("should not pass -5", function (done) { | ||
validator(-5, common.checkValidation(done, 'out-of-range-number')); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'undefined')); | ||
}); | ||
describe("width custom error", function () { | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.ranges.number(0, undefined, 'custom-error'); | ||
it("should not pass -15 with 'custom-error'", function (done) { | ||
validator(-15, common.checkValidation(done, 'custom-error')); | ||
validator.validate(-15, common.checkValidation(done, 'custom-error')); | ||
}); | ||
@@ -138,14 +194,14 @@ }); | ||
it("should pass 'test'", function (done) { | ||
validator('test', common.checkValidation(done)); | ||
validator.validate('test', common.checkValidation(done)); | ||
}); | ||
it("should not pass 'hello world'", function (done) { | ||
validator('hello world', common.checkValidation(done, 'out-of-range-length')); | ||
validator.validate('hello world', common.checkValidation(done, 'out-of-range-length')); | ||
}); | ||
describe("width custom error", function () { | ||
describe("with custom error", function () { | ||
var validator = enforce.ranges.length(0, 10, 'custom-error'); | ||
it("should not pass 'hello world' with 'custom-error'", function (done) { | ||
validator('hello world', common.checkValidation(done, 'custom-error')); | ||
validator.validate('hello world', common.checkValidation(done, 'custom-error')); | ||
}); | ||
@@ -159,14 +215,14 @@ }); | ||
it("should pass 'test'", function (done) { | ||
validator('test', common.checkValidation(done)); | ||
validator.validate('test', common.checkValidation(done)); | ||
}); | ||
it("should not pass 'hello world'", function (done) { | ||
validator('hello world', common.checkValidation(done, 'out-of-range-length')); | ||
validator.validate('hello world', common.checkValidation(done, 'out-of-range-length')); | ||
}); | ||
describe("width custom error", function () { | ||
describe("with custom error", function () { | ||
var validator = enforce.ranges.length(undefined, 10, 'custom-error'); | ||
it("should not pass 'hello world' with 'custom-error'", function (done) { | ||
validator('hello world', common.checkValidation(done, 'custom-error')); | ||
validator.validate('hello world', common.checkValidation(done, 'custom-error')); | ||
}); | ||
@@ -180,7 +236,7 @@ }); | ||
it("should pass 'test'", function (done) { | ||
validator('test', common.checkValidation(done)); | ||
validator.validate('test', common.checkValidation(done)); | ||
}); | ||
it("should pass 'hello world'", function (done) { | ||
validator('hello world', common.checkValidation(done)); | ||
validator.validate('hello world', common.checkValidation(done)); | ||
}); | ||
@@ -193,16 +249,16 @@ }); | ||
it("should pass 'hello world'", function (done) { | ||
validator('hello world', common.checkValidation(done)); | ||
validator.validate('hello world', common.checkValidation(done)); | ||
}); | ||
it("should not pass 'test'", function (done) { | ||
validator('test', common.checkValidation(done, 'out-of-range-length')); | ||
validator.validate('test', common.checkValidation(done, 'out-of-range-length')); | ||
}); | ||
describe("width custom error", function () { | ||
describe("with custom error", function () { | ||
var validator = enforce.ranges.length(5, undefined, 'custom-error'); | ||
it("should not pass 'test' with 'custom-error'", function (done) { | ||
validator('test', common.checkValidation(done, 'custom-error')); | ||
validator.validate('test', common.checkValidation(done, 'custom-error')); | ||
}); | ||
}); | ||
}); |
@@ -17,14 +17,30 @@ var should = require("should"); | ||
it("should pass 'Passw0r∂'", function (done) { | ||
validator('Passw0r∂', common.checkValidation(done)); | ||
validator.validate('Passw0r∂', common.checkValidation(done)); | ||
}); | ||
it("should not pass 'password'", function (done) { | ||
validator('password', common.checkValidation(done, 'weak-password')); | ||
validator.validate('password', common.checkValidation(done, 'weak-password')); | ||
}); | ||
describe("width custom error", function () { | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'weak-password')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'weak-password')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.security.password('custom-error'); | ||
it("should not pass 'password' with 'custom-error'", function (done) { | ||
validator('password', common.checkValidation(done, 'custom-error')); | ||
validator.validate('password', common.checkValidation(done, 'custom-error')); | ||
}); | ||
@@ -38,22 +54,38 @@ }); | ||
it("should pass 'a1'", function (done) { | ||
validator('a1', common.checkValidation(done)); | ||
validator.validate('a1', common.checkValidation(done)); | ||
}); | ||
it("should pass 'A1'", function (done) { | ||
validator('A1', common.checkValidation(done)); | ||
validator.validate('A1', common.checkValidation(done)); | ||
}); | ||
it("should pass '_A1'", function (done) { | ||
validator('_A1', common.checkValidation(done)); | ||
validator.validate('_A1', common.checkValidation(done)); | ||
}); | ||
it("should not pass 'a'", function (done) { | ||
validator('a', common.checkValidation(done, 'invalid-username')); | ||
validator.validate('a', common.checkValidation(done, 'invalid-username')); | ||
}); | ||
describe("width custom error", function () { | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'invalid-username')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'invalid-username')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
describe("with custom error", function () { | ||
var validator = enforce.security.username('custom-error'); | ||
it("should not pass 'a' with 'custom-error'", function (done) { | ||
validator('a', common.checkValidation(done, 'custom-error')); | ||
validator.validate('a', common.checkValidation(done, 'custom-error')); | ||
}); | ||
@@ -67,11 +99,27 @@ }); | ||
it("should not pass 'a1'", function (done) { | ||
validator('a1', common.checkValidation(done, 'invalid-username')); | ||
validator.validate('a1', common.checkValidation(done, 'invalid-username')); | ||
}); | ||
it("should not pass 'abcd'", function (done) { | ||
validator('abcd', common.checkValidation(done, 'invalid-username')); | ||
validator.validate('abcd', common.checkValidation(done, 'invalid-username')); | ||
}); | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'invalid-username')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'invalid-username')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
it("should pass 'username'", function (done) { | ||
validator('username', common.checkValidation(done)); | ||
validator.validate('username', common.checkValidation(done)); | ||
}); | ||
@@ -84,20 +132,36 @@ }); | ||
it("should pass 'Passw0r∂'", function (done) { | ||
validator('Passw0r∂', common.checkValidation(done)); | ||
validator.validate('Passw0r∂', common.checkValidation(done)); | ||
}); | ||
it("should pass 'Passw0rd'", function (done) { | ||
validator('Passw0rd', common.checkValidation(done)); | ||
validator.validate('Passw0rd', common.checkValidation(done)); | ||
}); | ||
it("should not pass 'P12345'", function (done) { | ||
validator('P12345', common.checkValidation(done, 'weak')); | ||
validator.validate('P12345', common.checkValidation(done, 'weak')); | ||
}); | ||
it("should not pass 'password'", function (done) { | ||
validator('password', common.checkValidation(done, 'weak')); | ||
validator.validate('password', common.checkValidation(done, 'weak')); | ||
}); | ||
it("should not pass 'p12'", function (done) { | ||
validator('p12', common.checkValidation(done, 'weak')); | ||
validator.validate('p12', common.checkValidation(done, 'weak')); | ||
}); | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'weak')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'weak')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
}); | ||
@@ -109,36 +173,52 @@ | ||
it("should pass '4716025001543325' (VISA)", function (done) { | ||
validator('4716025001543325', common.checkValidation(done)); | ||
validator.validate('4716025001543325', common.checkValidation(done)); | ||
}); | ||
it("should pass '6011484016942229' (Discover)", function (done) { | ||
validator('6011484016942229', common.checkValidation(done)); | ||
validator.validate('6011484016942229', common.checkValidation(done)); | ||
}); | ||
it("should pass '5194162135294737' (Mastercard)", function (done) { | ||
validator('5194162135294737', common.checkValidation(done)); | ||
validator.validate('5194162135294737', common.checkValidation(done)); | ||
}); | ||
it("should pass '6304709865953729' (Maestro)", function (done) { | ||
validator('6304709865953729', common.checkValidation(done)); | ||
validator.validate('6304709865953729', common.checkValidation(done)); | ||
}); | ||
it("should pass '342106257219198' (American Express)", function (done) { | ||
validator('342106257219198', common.checkValidation(done)); | ||
validator.validate('342106257219198', common.checkValidation(done)); | ||
}); | ||
it("should not pass '342106257219199'", function (done) { | ||
validator('342106257219199', common.checkValidation(done, 'not-valid-creditcard')); | ||
validator.validate('342106257219199', common.checkValidation(done, 'not-valid-creditcard')); | ||
}); | ||
it("should not pass '5194162135294734'", function (done) { | ||
validator('5194162135294734', common.checkValidation(done, 'not-valid-creditcard')); | ||
validator.validate('5194162135294734', common.checkValidation(done, 'not-valid-creditcard')); | ||
}); | ||
it("should not pass '6011484019642229'", function (done) { | ||
validator('6011484019642229', common.checkValidation(done, 'not-valid-creditcard')); | ||
validator.validate('6011484019642229', common.checkValidation(done, 'not-valid-creditcard')); | ||
}); | ||
it("should not pass '4917972403611992'", function (done) { | ||
validator('4917972403611992', common.checkValidation(done, 'not-valid-creditcard')); | ||
validator.validate('4917972403611992', common.checkValidation(done, 'not-valid-creditcard')); | ||
}); | ||
it("should not pass null", function (done) { | ||
validator.validate(null, common.checkValidation(done, 'not-valid-creditcard')); | ||
}); | ||
it("should not pass undefined", function (done) { | ||
validator.validate(undefined, common.checkValidation(done, 'not-valid-creditcard')); | ||
}); | ||
it("should pass null with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(null, common.checkValidation(done)); | ||
}); | ||
it("should pass undefined with .ifDefined()", function (done) { | ||
validator.ifDefined().validate(undefined, common.checkValidation(done)); | ||
}); | ||
}); | ||
@@ -150,7 +230,7 @@ | ||
it("should pass '342106257219198' (American Express)", function (done) { | ||
validator('342106257219198', common.checkValidation(done)); | ||
validator.validate('342106257219198', common.checkValidation(done)); | ||
}); | ||
it("should not pass '4716025001543325' (VISA) with 'not-valid'", function (done) { | ||
validator('4716025001543325', common.checkValidation(done, 'not-valid')); | ||
validator.validate('4716025001543325', common.checkValidation(done, 'not-valid')); | ||
}); | ||
@@ -160,10 +240,10 @@ }); | ||
describe("enforce.security.creditcard([ 'visa' ], 'not-valid')", function () { | ||
var validator = enforce.security.creditcard([ 'visa' ], 'not-valid'); | ||
var validator = enforce.security.creditcard(['visa'], 'not-valid'); | ||
it("should pass '4716025001543325' (VISA)", function (done) { | ||
validator('4716025001543325', common.checkValidation(done)); | ||
validator.validate('4716025001543325', common.checkValidation(done)); | ||
}); | ||
it("should not pass '342106257219198' (American Express) with 'not-valid'", function (done) { | ||
validator('342106257219198', common.checkValidation(done, 'not-valid')); | ||
validator.validate('342106257219198', common.checkValidation(done, 'not-valid')); | ||
}); | ||
@@ -173,14 +253,14 @@ }); | ||
describe("enforce.security.creditcard([ 'maestro', 'discover' ], 'not-valid')", function () { | ||
var validator = enforce.security.creditcard([ 'maestro', 'discover' ], 'not-valid'); | ||
var validator = enforce.security.creditcard(['maestro', 'discover'], 'not-valid'); | ||
it("should pass '5038328991436353' (Maestro)", function (done) { | ||
validator('5038328991436353', common.checkValidation(done)); | ||
validator.validate('5038328991436353', common.checkValidation(done)); | ||
}); | ||
it("should pass '6011734449679115' (Discover)", function (done) { | ||
validator('6011734449679115', common.checkValidation(done)); | ||
validator.validate('6011734449679115', common.checkValidation(done)); | ||
}); | ||
it("should not pass '4485227446136769' (VISA) with 'not-valid'", function (done) { | ||
validator('4485227446136769', common.checkValidation(done, 'not-valid')); | ||
validator.validate('4485227446136769', common.checkValidation(done, 'not-valid')); | ||
}); | ||
@@ -190,11 +270,11 @@ }); | ||
describe("enforce.security.creditcard([ 'luhn' ], 'not-valid')", function () { | ||
var validator = enforce.security.creditcard([ 'luhn' ], 'not-valid'); | ||
var validator = enforce.security.creditcard(['luhn'], 'not-valid'); | ||
it("should pass '12344' (Luhn)", function (done) { | ||
validator('12344', common.checkValidation(done)); | ||
validator.validate('12344', common.checkValidation(done)); | ||
}); | ||
it("should not pass '12345' with 'not-valid'", function (done) { | ||
validator('12345', common.checkValidation(done, 'not-valid')); | ||
validator.validate('12345', common.checkValidation(done, 'not-valid')); | ||
}); | ||
}); |
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
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
123775
32
2789
191
3
1