Comparing version 0.9.0 to 0.9.1
202
index.js
@@ -1,6 +0,15 @@ | ||
var _ = require('underscore'); | ||
/** | ||
* Module dependencies | ||
*/ | ||
var util = require('underscore'); | ||
var sanitize = require('validator').sanitize; | ||
// Public access | ||
/** | ||
* Public access | ||
*/ | ||
module.exports = function (entity) { | ||
@@ -11,5 +20,12 @@ return new Anchor(entity); | ||
// Specify the function, object, or list to be anchored | ||
/** | ||
* Constructor of individual instance of Anchor | ||
* Specify the function, object, or list to be anchored | ||
*/ | ||
function Anchor (entity) { | ||
if (_.isFunction(entity)) { | ||
if (util.isFunction(entity)) { | ||
this.fn = entity; | ||
@@ -23,6 +39,20 @@ throw new Error ('Anchor does not support functions yet!'); | ||
// Built-in data type rules | ||
/** | ||
* Built-in data type rules | ||
*/ | ||
Anchor.prototype.rules = require('./lib/rules'); | ||
// Enforce that the data matches the specified ruleset | ||
/** | ||
* Enforce that the data matches the specified ruleset | ||
*/ | ||
Anchor.prototype.to = function (ruleset) { | ||
@@ -64,4 +94,37 @@ | ||
// Coerce the data to the specified ruleset if possible | ||
// otherwise throw an error | ||
/** | ||
* Coerce the data to the specified ruleset if possible | ||
* otherwise throw an error | ||
* Priority: this should probably provide the default | ||
* implementation in Waterline core. Currently it's completely | ||
* up to the adapter to define type coercion. | ||
* | ||
* Which is fine!.. but complicates custom CRUD adapter development. | ||
* Much handier would be an evented architecture, that allows | ||
* for adapter developers to write: | ||
* | ||
{ | ||
// Called before find() receives criteria | ||
// Here, criteria refers to just attributes (the `where`) | ||
// limit, skip, and sort are not included | ||
coerceCriteria: function (criteria) { | ||
return criteria; | ||
}, | ||
// Called before create() or update() receive values | ||
coerceValues: function () {} | ||
} | ||
* | ||
* Adapter developers would be able to use Anchor.prototype.cast() | ||
* to declaritively define these type coercions. | ||
* Down the line, we could take this further for an even nicer API, | ||
* but for now, this alone would be a nice improvement. | ||
* | ||
*/ | ||
Anchor.prototype.cast = function (ruleset) { | ||
@@ -71,12 +134,18 @@ todo(); | ||
// Coerce the data to the specified ruleset no matter what | ||
/** | ||
* Coerce the data to the specified ruleset no matter what | ||
*/ | ||
Anchor.prototype.hurl = function (ruleset) { | ||
// Iterate trough given data attributes | ||
// to check if they exists in the ruleset | ||
for(var attr in this.data) { | ||
if(this.data.hasOwnProperty(attr)) { | ||
// to check if they exist in the ruleset | ||
for (var attr in this.data) { | ||
if (this.data.hasOwnProperty(attr)) { | ||
// If it doesnt... | ||
if(!ruleset[attr]) { | ||
if (!ruleset[attr]) { | ||
@@ -97,3 +166,10 @@ // Declaring err here as error helpers live in match.js | ||
// Specify default values to automatically populated when undefined | ||
/** | ||
* Specify default values to automatically populated when undefined | ||
*/ | ||
Anchor.prototype.defaults = function (ruleset) { | ||
@@ -103,8 +179,55 @@ todo(); | ||
// Declare name of custom data type | ||
Anchor.prototype.define = function (name) { | ||
todo(); | ||
/** | ||
* Declare a custom data type | ||
* If function definition is specified, `name` is required. | ||
* Otherwise, if dictionary-type `definition` is specified, | ||
* `name` must not be present. | ||
* | ||
* @param {String} name [optional] | ||
* @param {Object|Function} definition | ||
*/ | ||
Anchor.prototype.define = function (name, definition) { | ||
// check to see if we have an dictionary | ||
if ( util.isObject(name) ) { | ||
// if so all the attributes should be validation functions | ||
for (var attr in name){ | ||
if(!util.isFunction(name[attr])){ | ||
throw new Error('Definition error: \"' + attr + '\" does not have a definition'); | ||
} | ||
} | ||
// add the new custom data types | ||
util.extend(Anchor.prototype.rules, name); | ||
return this; | ||
} | ||
if ( util.isFunction(definition) && util.isString(name) ) { | ||
// Add a single data type | ||
Anchor.prototype.rules[name] = definition; | ||
return this; | ||
} | ||
throw new Error('Definition error: \"' + name + '\" is not a valid definition.'); | ||
}; | ||
// Specify custom ruleset | ||
/** | ||
* Specify custom ruleset | ||
*/ | ||
Anchor.prototype.as = function (ruleset) { | ||
@@ -115,3 +238,8 @@ todo(); | ||
// Specify named arguments and their rulesets as an object | ||
/** | ||
* Specify named arguments and their rulesets as an object | ||
*/ | ||
Anchor.prototype.args = function (args) { | ||
@@ -121,13 +249,39 @@ todo(); | ||
// Specify each of the permitted usages for this function | ||
/** | ||
* Specify each of the permitted usages for this function | ||
*/ | ||
Anchor.prototype.usage = function () { | ||
var usages = _.toArray(arguments); | ||
var usages = util.toArray(arguments); | ||
todo(); | ||
}; | ||
// Deep-match a complex collection or model against a schema | ||
/** | ||
* Deep-match a complex collection or model against a schema | ||
*/ | ||
Anchor.match = require('./lib/match.js'); | ||
/** | ||
* Expose `define` so it can be used globally | ||
*/ | ||
module.exports.define = Anchor.prototype.define; | ||
function todo() { | ||
throw new Error("Not implemented yet! If you'd like to contribute, tweet @mikermcneil."); | ||
} | ||
throw new Error('Not implemented yet! If you\'d like to contribute, tweet @mikermcneil.'); | ||
} |
@@ -0,4 +1,13 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
var _ = require('underscore'); | ||
var rules = require('./rules'); | ||
/** | ||
* Expose functions | ||
*/ | ||
module.exports = { | ||
@@ -9,5 +18,7 @@ type: deepMatchType, | ||
// Max depth value | ||
var maxDepth = 50; | ||
/** | ||
@@ -18,2 +29,3 @@ * Match a miscellaneous rule | ||
*/ | ||
function match ( data, ruleName, args ) { | ||
@@ -79,10 +91,16 @@ var errors = []; | ||
/** | ||
* | ||
* | ||
* | ||
* Match a complex collection or model against a schema | ||
* Return a list of errors (or an empty list if no errors were found) | ||
* | ||
*/ | ||
* Match a complex collection or model against a schema | ||
* | ||
* @param {} data | ||
* @param {} ruleset | ||
* @param {} depth | ||
* @param {} keyName | ||
* @returns a list of errors (or an empty list if no errors were found) | ||
*/ | ||
function deepMatchType (data, ruleset, depth, keyName) { | ||
@@ -145,13 +163,10 @@ | ||
/** | ||
* Return whether a piece of data matches a rule | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* Return whether a piece of data matches a rule | ||
* ruleName :: (STRING) | ||
* returns a list of errors, or an empty list in the absense of them | ||
* @param {*} datum | ||
* @param {Array|Object|String|Regexp} ruleName | ||
* @param {String} keyName | ||
* @returns a list of errors, or an empty list in the absense of them | ||
*/ | ||
function matchType (datum, ruleName, keyName) { | ||
@@ -158,0 +173,0 @@ |
@@ -0,7 +1,16 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
var _ = require('underscore'); | ||
var check = require('validator').check; | ||
/** | ||
* Type rules | ||
*/ | ||
module.exports = { | ||
// Type rules | ||
'empty' : function (x) { return x === ''; }, | ||
@@ -8,0 +17,0 @@ 'required' : function (x) { return check(x).notEmpty(); }, |
{ | ||
"name": "anchor", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"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
35702
14
811