Comparing version 0.1.0 to 0.1.2-0
80
index.js
@@ -5,3 +5,8 @@ var _ = require('underscore'); | ||
// Public access | ||
module.exports = function (entity) { | ||
return new Anchor(entity); | ||
}; | ||
// Specify the function, object, or list to be anchored | ||
@@ -58,8 +63,10 @@ function Anchor (entity) { | ||
// Enforce the data with the specified ruleset | ||
Anchor.prototype.to = function (ruleset, error) { | ||
// Enforce that the data matches the specified ruleset | ||
// If it doesn't, throw an error. | ||
// If the callback is specified, instead of throwing, use first arg | ||
Anchor.prototype.to = function (ruleset, cb) { | ||
var self = this; | ||
// If error is specififed, handle error instead of throwing it | ||
if (error) self.errorFn = error; | ||
// If callback is specififed, handle error instead of throwing it | ||
if (cb) self.cb = cb; | ||
@@ -71,5 +78,16 @@ // Use deep match to descend into the collection and verify each item and/or key | ||
// Coerce the data to the specified ruleset if possible | ||
// otherwise throw an error | ||
Anchor.prototype.cast = function (ruleset) { | ||
todo(); | ||
}; | ||
// Coerce the data to the specified ruleset no matter what | ||
Anchor.prototype.hurl = function (ruleset) { | ||
todo(); | ||
}; | ||
// Specify default values to automatically populated when undefined | ||
Anchor.prototype.defaults = function (ruleset) { | ||
todo(); | ||
}; | ||
@@ -79,3 +97,3 @@ | ||
Anchor.prototype.define = function (name) { | ||
todo(); | ||
}; | ||
@@ -85,3 +103,3 @@ | ||
Anchor.prototype.as = function (ruleset) { | ||
todo(); | ||
}; | ||
@@ -92,3 +110,3 @@ | ||
Anchor.prototype.args = function (args) { | ||
todo(); | ||
}; | ||
@@ -99,10 +117,6 @@ | ||
var usages = _.toArray(arguments); | ||
todo(); | ||
}; | ||
// Public access | ||
module.exports = function (entity) { | ||
return new Anchor(entity); | ||
}; | ||
// Return whether a piece of data matches a rule | ||
@@ -143,4 +157,4 @@ // ruleName :: (STRING) | ||
// Return outcome or handle failure | ||
if (!outcome) failure(datum,ruleName, outcome); | ||
else return outcome; | ||
if (!outcome) return failure(datum,ruleName, outcome); | ||
else return success(outcome); | ||
} | ||
@@ -153,5 +167,5 @@ catch (e) { | ||
// Allow .error() to handle the error instead of throwing it | ||
if (ctx.errorFn) { | ||
ctx.errorFn(err); | ||
return err; | ||
if (ctx.cb) { | ||
ctx.cb(err); | ||
return err || new Error ('Validation error: "'+datum+'" is not of type "'+ruleName+'"'); | ||
} | ||
@@ -161,2 +175,9 @@ else if (err) throw new Error(err); | ||
} | ||
function success(outcome) { | ||
if (ctx.cb) { | ||
return ctx.cb(null, outcome); | ||
} | ||
else return outcome; | ||
} | ||
}; | ||
@@ -181,10 +202,2 @@ | ||
// console.log("\n\n*********:***********:********"); | ||
// console.log("depth:", depth); | ||
// console.log("key:", key); | ||
// console.log("rule:", rule); | ||
// console.log("ruleset:", ruleset); | ||
// console.log("data:", data); | ||
// console.log("keyChain:", keyChain); | ||
// If this is a schema rule, check each item in the data collection | ||
@@ -218,13 +231,4 @@ if (_.isArray(ruleset) && ruleset.length !== 0) { | ||
function reduceKeyChain (data, keyChain) { | ||
// Get full .-delimited attr name and value | ||
var topLevelAttrName = keyChain.shift(); | ||
var topLevelAttrVal = data[topLevelAttrName]; | ||
var attrName = _.reduce(keyChain,function(memo,key) { | ||
return memo + "." + key; | ||
},topLevelAttrName); | ||
return topLevelAttrName && _.reduce(keyChain,function(memo,key) { | ||
return memo && memo[key]; | ||
}, topLevelAttrVal); | ||
} | ||
function todo() { | ||
throw new Error("Not implemented yet! If you'd like to contribute, tweet @mikermcneil."); | ||
} |
{ | ||
"name": "anchor", | ||
"version": "0.1.0", | ||
"version": "0.1.2-0", | ||
"description": "Recursive validation library with support for objects and lists", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
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
29017
572
0