Socket
Socket
Sign inDemoInstall

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.2.1 to 0.3.0

37

index.js

@@ -52,4 +52,37 @@ var _ = require('underscore');

// Coerce the data to the specified ruleset no matter what
Anchor.prototype.hurl = function (ruleset) {
todo();
Anchor.prototype.hurl = function (ruleset, cb) {
// If callback is specififed, trigger it at the end
// also, handle error instead of throwing it
if (cb) this.cb = cb;
// Iterate trough given data attributes
// to check if they exists in the ruleset
for(var attr in this.data) {
if(this.data.hasOwnProperty(attr)) {
// If it doesnt...
if(!ruleset[attr]) {
// Declaring err here as error helpers live in match.js
var err = new Error('Validation error: Attribute \"' + attr + '\" is not in the ruleset.');
// If a callback has been passed pass the error there
if(typeof cb === 'function') {
return cb(err);
}
// Or just throw it
else throw err;
}
}
}
// Once we make sure that attributes match
// we can just proceed to deepMatch
Anchor.match(this.data, ruleset, this);
// 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(this.error);
};

@@ -56,0 +89,0 @@

32

lib/match.js

@@ -6,4 +6,5 @@ var _ = require('underscore');

// Max depth value
var maxDepth = 50;
/**

@@ -17,11 +18,10 @@ *

*/
function deepMatch (data, ruleset, ctx, depth, maxDepth) {
function deepMatch (data, ruleset, ctx, depth, keyName) {
// If ruleset is not an object or array, use the provided function to validate
if (!_.isObject(ruleset)) {
return match(data,ruleset,ctx);
return match(data,ruleset,ctx, keyName);
}
// Default value for maxDepth and depth
maxDepth = maxDepth || 50;
// Default value for depth
depth = depth || 0;

@@ -48,3 +48,3 @@

if (_.keys(ruleset).length === 0) {
return match(data, ruleset, ctx);
return match(data, ruleset, ctx, keyName);
}

@@ -55,3 +55,3 @@ else return _.all(ruleset,matchDict);

// Leaf rules land here and execute the iterator
else return match(data, ruleset, ctx);
else return match(data, ruleset, ctx, keyName);

@@ -62,3 +62,3 @@

if (ctx && ctx.error) return false;
else return deepMatch(data[key], ruleset[key], ctx, depth+1);
else return deepMatch(data[key], ruleset[key], ctx, depth+1, key);
}

@@ -69,3 +69,3 @@

if (ctx && ctx.error) return false;
else return deepMatch(model, ruleset[0], ctx, depth+1);
else return deepMatch(model, ruleset[0], ctx, depth+1, keyName);
}

@@ -90,3 +90,3 @@ }

*/
function match (datum, ruleName, ctx) {
function match (datum, ruleName, ctx, keyName) {

@@ -125,7 +125,7 @@

// False outcome is a failure
if (!outcome) return failure(datum,ruleName);
if (!outcome) return failure(datum,ruleName, keyName);
else return outcome;
}
catch (e) {
failure(datum, ruleName);
failure(datum, ruleName, keyName);
}

@@ -136,6 +136,10 @@

// Otherwise, throw an error.
function failure(datum, ruleName) {
function failure(datum, ruleName, keyName) {
// Construct error
var err = new Error ('Validation error: "'+datum+'" is not of type "'+ruleName+'"');
var err = '';
err += 'Validation error: "'+datum+'" ';
err += keyName ? '('+keyName+') ' : '';
err += 'is not of type "'+ruleName+'"';
err = new Error(err);

@@ -142,0 +146,0 @@ // Handle the error in callback instead of throwing it

{
"name": "anchor",
"version": "0.2.1",
"version": "0.3.0",
"description": "Recursive validation library with support for objects and lists",

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

anchor
======
> IMPORTANT: This is an **unfinished work in progress**! Please follow/star and keep up to date as the project develops.
> IMPORTANT: Anchor is under **active development**! Please follow/star and keep up to date as the project develops.
> If you're interested in contributing, drop me a note at @mikermcneil. I welcome your feedback, ideas, and pull requests :)

@@ -6,0 +6,0 @@

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