Comparing version 0.9.6 to 0.9.7
@@ -56,3 +56,3 @@ /** | ||
Anchor.prototype.to = function (ruleset) { | ||
Anchor.prototype.to = function (ruleset, context) { | ||
@@ -72,3 +72,3 @@ var errors = []; | ||
// Stop at default maxDepth (50) to prevent infinite loops in self-associations | ||
errors = errors.concat(Anchor.match.type(this.data, ruleset['type'])); | ||
errors = errors.concat(Anchor.match.type.call(context, this.data, ruleset['type'])); | ||
} | ||
@@ -78,3 +78,3 @@ | ||
else { | ||
errors = errors.concat(Anchor.match.rule(this.data, rule, ruleset[rule])); | ||
errors = errors.concat(Anchor.match.rule.call(context, this.data, rule, ruleset[rule])); | ||
} | ||
@@ -81,0 +81,0 @@ } |
@@ -30,3 +30,4 @@ /** | ||
function match ( data, ruleName, args ) { | ||
var errors = []; | ||
var self = this, | ||
errors = []; | ||
@@ -53,3 +54,3 @@ // if args is an array we need to make it a nested array | ||
try { | ||
outcome = rule.apply(rule, args); | ||
outcome = rule.apply(self, args); | ||
} | ||
@@ -105,7 +106,9 @@ catch (e) { | ||
var errors = []; | ||
var self = this, | ||
errors = []; | ||
// If ruleset is not an object or array, use the provided function to validate | ||
if (!_.isObject(ruleset)) { | ||
return matchType(data,ruleset, keyName); | ||
return matchType.call(self, data,ruleset, keyName); | ||
} | ||
@@ -130,3 +133,3 @@ | ||
_.each(data, function (model) { | ||
errors = errors.concat(deepMatchType(model, ruleset[0], depth+1, keyName)); | ||
errors = errors.concat(deepMatchType.call(self, model, ruleset[0], depth+1, keyName)); | ||
}); | ||
@@ -136,3 +139,3 @@ return errors; | ||
// Leaf rules land here and execute the iterator | ||
else return matchType(data, ruleset, keyName); | ||
else return matchType.call(self, data, ruleset, keyName); | ||
} | ||
@@ -148,3 +151,3 @@ | ||
if (_.keys(ruleset).length === 0) { | ||
return matchType(data, ruleset, keyName); | ||
return matchType.call(self, data, ruleset, keyName); | ||
} | ||
@@ -154,3 +157,3 @@ else { | ||
_.each(ruleset, function (subRule, key) { | ||
errors = errors.concat(deepMatchType(data[key], ruleset[key], depth+1, key)); | ||
errors = errors.concat(deepMatchType.call(self, data[key], ruleset[key], depth+1, key)); | ||
}); | ||
@@ -178,2 +181,4 @@ return errors; | ||
var self = this; | ||
try { | ||
@@ -200,3 +205,3 @@ var outcome, rule; | ||
} | ||
else x.match(ruleName); | ||
else x.match.call(self, ruleName); | ||
}; | ||
@@ -212,3 +217,3 @@ } | ||
} | ||
else outcome = rule(datum); | ||
else outcome = rule.call(self, datum); | ||
@@ -215,0 +220,0 @@ // If validation failed, return an error |
@@ -19,3 +19,2 @@ /** | ||
'required' : function (x) { | ||
// Transform data to work properly with node validator | ||
@@ -32,4 +31,4 @@ if(!x && x !== 0) x = ''; | ||
// Transform data to work properly with node validator | ||
if(!x) x = ''; | ||
else if(typeof x.toString !== 'undefined') x = x.toString(); | ||
if (!x) x = ''; | ||
else if (typeof x.toString !== 'undefined') x = x.toString(); | ||
else x = '' + x; | ||
@@ -44,3 +43,3 @@ | ||
'json' : function (x) { | ||
if(_.isUndefined(x)) return false; | ||
if (_.isUndefined(x)) return false; | ||
try { JSON.stringify(x); } | ||
@@ -53,5 +52,7 @@ catch(err) { return false; } | ||
'string' : _.isString, | ||
'alpha' : function (x){ return check(x).isAlpha();}, | ||
'alpha' : function (x){ return check(x).isAlpha();}, | ||
'alphadashed': function (x) {return /^[a-zA-Z-_]*$/.test(x)}, | ||
'numeric' : function (x){ return check(x).isNumeric();}, | ||
'alphanumeric' : function (x){ return check(x).isAlphanumeric();}, | ||
'alphanumeric': function (x){ return check(x).isAlphanumeric();}, | ||
'alphanumericdashed': function (x) {return /^[a-zA-Z0-9-_]*$/.test(x)}, | ||
'email' : function (x){ return check(x).isEmail();}, | ||
@@ -91,3 +92,3 @@ 'url' : function (x){ return check(x).isUrl();}, | ||
'hexadecimal': function (x) { return check(x).hexadecimal(); }, | ||
'hexColor': function (x) { return check(x).hexColor(); }, | ||
'hexColor': function (x) { return check(x).isHexColor(); }, | ||
@@ -94,0 +95,0 @@ 'lowercase': function (x) { return check(x).lowercase(); }, |
{ | ||
"name": "anchor", | ||
"version": "0.9.6", | ||
"version": "0.9.7", | ||
"description": "Recursive validation library with support for objects and lists", | ||
@@ -20,6 +20,9 @@ "main": "index.js", | ||
"dependencies": { | ||
"validator": "~1.5.1", | ||
"lodash": "~2.2.1", | ||
"validator": "~2.0.0", | ||
"lodash": "~2.4.1", | ||
"async": "0.2.9" | ||
}, | ||
"devDependencies": { | ||
"mocha": "1.9.x" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
var anchor = require('../index.js'); | ||
@@ -130,2 +130,2 @@ var testType = require('./util/testType.js'); | ||
}); | ||
}); |
@@ -1,2 +0,2 @@ | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
var anchor = require('../index.js'); | ||
@@ -9,3 +9,3 @@ var testType = require('./util/testType.js'); | ||
it(' should create an anchor object in naive usage',function () { | ||
it('should create an anchor object in naive usage',function () { | ||
anchor('foo'); | ||
@@ -16,31 +16,31 @@ return true; | ||
describe ('falsey values',function () { | ||
it (' should support "empty" rule ', function () { | ||
it ('should support "empty" rule ', function () { | ||
return testType('empty','','foo'); | ||
}); | ||
it (' should support "undefined" rule ', function () { | ||
it ('should support "undefined" rule ', function () { | ||
return testType('undefined',undefined,'foo'); | ||
}); | ||
it (' "null" rule should not work with undefined ', function () { | ||
it ('"null" rule should not work with undefined ', function () { | ||
return testType('null',null,undefined); | ||
}); | ||
it (' "null" rule should not work with empty string ', function () { | ||
it ('"null" rule should not work with empty string ', function () { | ||
return testType('null',null,''); | ||
}); | ||
it (' "falsey" rule should correctly identify falsy values ', function () { | ||
it ('"falsey" rule should correctly identify falsy values ', function () { | ||
return testType('falsey','','whatever'); | ||
}); | ||
it (' "falsey" rule should correctly identify falsy values ', function () { | ||
it ('"falsey" rule should correctly identify falsy values ', function () { | ||
return testType('falsey',null,'whatever'); | ||
}); | ||
it (' "falsey" rule should correctly identify falsy values ', function () { | ||
it ('"falsey" rule should correctly identify falsy values ', function () { | ||
return testType('falsey',undefined,'whatever'); | ||
}); | ||
it (' "falsey" rule should correctly identify falsy values ', function () { | ||
it ('"falsey" rule should correctly identify falsy values ', function () { | ||
return testType('falsey',0,'whatever'); | ||
}); | ||
it (' "falsey" rule should correctly identify falsy values ', function () { | ||
it ('"falsey" rule should correctly identify falsy values ', function () { | ||
return testType('falsey',false,'whatever'); | ||
@@ -52,14 +52,22 @@ }); | ||
it (' should support "string" rule ', function () { | ||
it ('should support "string" rule ', function () { | ||
return testType('string','foo',22482); | ||
}); | ||
it (' should support "email" rule ', function () { | ||
it ('should support "email" rule ', function () { | ||
return testType('email','fox.and.the.hound@theforest.com','foo'); | ||
}); | ||
it (' should support "email" rule ', function () { | ||
it ('should support "email" rule ', function () { | ||
return testType('email','fox.and.the.hound@theforest.com','foo'); | ||
}); | ||
it('should support "alphadashed" rule ', function () { | ||
return testType('alphadashed','test-Test_test','Inv@lid'); | ||
}); | ||
it('should support "alphanumericdashed" rule ', function () { | ||
return testType('alphanumericdashed','test123-Test456_test789','Inv@lid'); | ||
}); | ||
}); | ||
@@ -70,11 +78,11 @@ | ||
it (' "finite" should identify integers and reject NaN ', function () { | ||
it ('"finite" should identify integers and reject NaN ', function () { | ||
return testType('finite',3,NaN); | ||
}); | ||
it (' "number" should identify NaN ', function () { | ||
it ('"number" should identify NaN ', function () { | ||
return testType('number',NaN,'foo'); | ||
}); | ||
it (' "number" should identify integers ', function () { | ||
it ('"number" should identify integers ', function () { | ||
return testType('number',28235,'foo'); | ||
@@ -81,0 +89,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
var anchor = require('../index.js'); | ||
@@ -16,3 +16,3 @@ var async = require('async'); | ||
}, | ||
isfive = function(val){ | ||
@@ -26,3 +26,3 @@ return val === 5; | ||
describe('Dictonary odf Definitions', function() { | ||
describe('Dictonary of Definitions', function() { | ||
it(' should properly validate a simple object with a custom type', function() { | ||
@@ -50,2 +50,16 @@ var rules = { | ||
}); | ||
describe('Context', function() { | ||
it(' should support providing context for rules via',function () { | ||
var user = { password: 'passW0rd', passwordConfirmation: 'passW0rd' }; | ||
anchor.define('password', function (password) { | ||
return password === this.passwordConfirmation; | ||
}); | ||
var outcome = anchor(user.password).to({ type: 'password' }, user); | ||
assert.equal(false, outcome); | ||
}); | ||
}); | ||
}); |
@@ -1,2 +0,2 @@ | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
var anchor = require('../index.js'); | ||
@@ -3,0 +3,0 @@ var testRules = require('./util/testRules.js'); |
@@ -1,2 +0,2 @@ | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
var anchor = require('../index.js'); | ||
@@ -102,2 +102,2 @@ var testType = require('./util/testType.js'); | ||
}); | ||
}); |
@@ -1,2 +0,2 @@ | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
var anchor = require('../../index.js'); | ||
@@ -23,6 +23,6 @@ var async = require('async'); | ||
if (!_.isArray(nonexampleOutcome)) { | ||
return gotErrors('Invalid input (' + nonexample + ') allowed through.', | ||
return gotErrors('Invalid input (' + nonexample + ') allowed through.', | ||
rules, nonexample); | ||
} | ||
function gotErrors (errMsg, err, data) { | ||
@@ -33,2 +33,2 @@ console.error(err); | ||
} | ||
}; | ||
}; |
@@ -1,2 +0,2 @@ | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
var anchor = require('../../index.js'); | ||
@@ -29,3 +29,3 @@ var async = require('async'); | ||
} | ||
function gotErrors (err) { | ||
@@ -39,2 +39,2 @@ console.error('*****************'); | ||
} | ||
}; | ||
}; |
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
37251
858
1
+ Addedlodash@2.4.2(transitive)
+ Addedvalidator@2.0.0(transitive)
- Removedlodash@2.2.1(transitive)
- Removedvalidator@1.5.1(transitive)
Updatedlodash@~2.4.1
Updatedvalidator@~2.0.0