Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

anchor

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

anchor - npm Package Compare versions

Comparing version 0.1.2-0 to 0.2.0

rules.js

104

index.js
var _ = require('underscore');
var check = require('validator').check;
var sanitize = require('validator').sanitize;
// Public access

@@ -24,40 +24,4 @@ module.exports = function (entity) {

// Built-in data type rules
Anchor.prototype.rules = {
'empty' : function (x) { return x === ''; },
'undefined' : _.isUndefined,
Anchor.prototype.rules = require('./rules');
'string' : _.isString,
'alpha' : function (x){ return check(x).isAlpha();},
'numeric' : function (x){ return check(x).isNumeric();},
'alphanumeric' : function (x){ return check(x).isAlphanumeric();},
'email' : function (x){ return check(x).isEmail();},
'url' : function (x){ return check(x).isUrl();},
'urlish' : /^\s([^\/]+\.)+.+\s*$/g,
'ip' : function (x){ return check(x).isIP(); },
'creditcard': function (x){ return check(x).isCreditCard();},
'uuid' : function (x, version){ return check(x).isUUID(version);},
'int' : function (x) { return check(x).isInt(); },
'integer' : function (x) { return check(x).isInt(); },
'number' : _.isNumber,
'finite' : _.isFinite,
'decimal' : function (x) { return check(x).isDecimal(); },
'float' : function (x) { return check(x).isDecimal(); },
'falsey' : function (x) { return !x; },
'truthy' : function (x) { return !!x; },
'null' : _.isNull,
'boolean' : _.isBoolean,
'array' : _.isArray,
'date' : _.isDate,
'after' : function (x,date) { return check(x).isAfter(date); },
'before' : function (x,date) { return check(x).isBefore(date); }
};
// Enforce that the data matches the specified ruleset

@@ -69,3 +33,4 @@ // If it doesn't, throw an error.

// If callback is specififed, handle error instead of throwing it
// If callback is specififed, trigger it at the end
// also, handle error instead of throwing it
if (cb) self.cb = cb;

@@ -75,3 +40,8 @@

// Stop at default maxDepth (50) to prevent infinite loops in self-associations
return Anchor.deepMatch(self.data, ruleset, self);
Anchor.deepMatch(self.data, ruleset, self);
// If a callback was specified, trigger it
// If an error object was stowed away in the ctx, pass it along
// (otherwise we never should have made it this far, the error should have been thrown)
cb && cb(self.error);
};

@@ -152,25 +122,26 @@

// Return outcome or handle failure
if (!outcome) return failure(datum,ruleName, outcome);
else return success(outcome);
// False outcome is a failure
if (!outcome) return failure(datum,ruleName);
else return outcome;
}
catch (e) {
failure(datum, ruleName, e);
failure(datum, ruleName);
}
function failure(datum, ruleName, err) {
// Allow .error() to handle the error instead of throwing it
// On failure-- stop and get out.
// If a cb was specified, call it with a first-arity error object.
// Otherwise, throw an error.
function failure(datum, ruleName) {
// Construct error
var err = new Error ('Validation error: "'+datum+'" is not of type "'+ruleName+'"');
// Handle the error in callback instead of throwing it
if (ctx.cb) {
ctx.cb(err);
return err || new Error ('Validation error: "'+datum+'" is not of type "'+ruleName+'"');
ctx.error = err;
return false;
}
else if (err) throw new Error(err);
else throw new Error ('Validation error: "'+datum+'" is not of type "'+ruleName+'"');
}
function success(outcome) {
if (ctx.cb) {
return ctx.cb(null, outcome);
}
else return outcome;
// Or throw error if there was no callback
else throw err;
}

@@ -203,5 +174,3 @@ };

// Handle plurals (arrays with a schema rule)
else return _.all(data, function (model) {
return Anchor.deepMatch(model, ruleset[0], ctx, depth+1);
});
return _.all(data, matchArray);
}

@@ -216,5 +185,3 @@

}
else return _.all(ruleset,function(subRule,key) {
return Anchor.deepMatch(data[key], ruleset[key], ctx, depth+1);
});
else return _.all(ruleset,matchDict);
}

@@ -224,2 +191,15 @@

else return Anchor.match(data, ruleset, ctx);
// Iterate through rules in dictionary until error is detected
function matchDict(subRule,key) {
if (ctx && ctx.error) return false;
else return Anchor.deepMatch(data[key], ruleset[key], ctx, depth+1);
}
// Match each object in array against ruleset until error is detected
function matchArray(model) {
if (ctx && ctx.error) return false;
else return Anchor.deepMatch(model, ruleset[0], ctx, depth+1);
}
};

@@ -226,0 +206,0 @@

{
"name": "anchor",
"version": "0.1.2-0",
"version": "0.2.0",
"description": "Recursive validation library with support for objects and lists",

@@ -5,0 +5,0 @@ "main": "index.js",

var _ = require('underscore');
var anchor = require('../index.js');
var testRule = require('./testRule.js');

@@ -4,0 +5,0 @@ describe('arrays', function() {

var _ = require('underscore');
var anchor = require('../index.js');
var testRule = require('./testRule.js');
beforeEach (function () {
testRule = function testRule(rule, example, nonexample) {
var err = false;
// Should not throw error
anchor(example).to(rule);
// Should throw error
try {
anchor(nonexample).to(rule);
describe('basic rules', function() {
// Should never reach here
err = 'Invalid input (' + nonexample + ') allowed through as a ' + rule + '.';
} catch(e) {
return true;
}
if(err) {
console.error('*****************');
console.error('nonexample', nonexample);
console.error('rule', rule);
throw new Error(err);
}
};
});
describe('basic usage', function() {
it(' should create an anchor object in naive usage',function () {

@@ -34,0 +10,0 @@ anchor('foo');

var _ = require('underscore');
var anchor = require('../index.js');
var testRule = require('./testRule.js');

@@ -4,0 +5,0 @@ describe('error usage', function() {

var _ = require('underscore');
var anchor = require('../index.js');
var testRule = require('./testRule.js');

@@ -4,0 +5,0 @@ describe('objects', function() {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc