Socket
Socket
Sign inDemoInstall

async-validator

Package Overview
Dependencies
Maintainers
1
Versions
86
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-validator - npm Package Compare versions

Comparing version 1.1.1 to 1.1.2

98

lib/index.js

@@ -0,4 +1,6 @@

'use strict';
var util = require('./util');
var validators = require('./validator/');
var messages = require('./messages');
var defaultMessages = require('./messages');
var error = require('./rule/').error;

@@ -39,3 +41,3 @@

this.rules = null;
this._messages = messages;
this._messages = defaultMessages;
this.define(descriptor);

@@ -67,7 +69,6 @@ };

if (!rules) {
throw new Error(
"Cannot configure a schema with no rules");
throw new Error('Cannot configure a schema with no rules');
}
if (typeof rules !== 'object' || Array.isArray(rules)) {
throw new Error("Rules must be an object");
throw new Error('Rules must be an object');
}

@@ -92,4 +93,6 @@ this.rules = {};

Schema.prototype.validate = function (source, options, callback) {
var _this = this;
if (!this.rules) {
throw new Error("Cannot validate with no rules.");
throw new Error('Cannot validate with no rules.');
}

@@ -101,7 +104,10 @@ options = options || {};

}
var complete = function (results) {
//console.log("got validation results %j", results);
var i, field, errors = [], fields = {};
var add = function (e) {
if ((e instanceof Error)) {
var complete = function complete(results) {
//console.log('got validation results %j', results);
var i,
field,
errors = [],
fields = {};
var add = function add(e) {
if (e instanceof Error) {
errors.push(e);

@@ -130,20 +136,18 @@ } else if (Array.isArray(e)) {

};
var messages = options.messages || this.messages();
options.messages = messages;
options.messages = options.messages || this.messages();
options.error = error;
var j, z, arr, value, i, rule, series = [];
var arr,
value,
series = [];
var keys = options.keys || Object.keys(this.rules);
for (j = 0; j < keys.length; j++) {
z = keys[j];
arr = this.rules[z];
keys.forEach(function (z) {
arr = _this.rules[z];
value = source[z];
//console.log('validate on key %s', z);
for (i = 0; i < arr.length; i++) {
rule = arr[i];
arr.forEach(function (rule) {
//console.log('validate on rule %j', rule);
if (typeof(rule.transform) === 'function') {
if (typeof rule.transform === 'function') {
value = source[z] = rule.transform(value);
}
if (typeof(rule) === 'function') {
if (typeof rule === 'function') {
rule = {

@@ -155,21 +159,21 @@ validator: rule

rule.fullField = rule.fullField || z;
rule.type = this.getType(rule);
rule.validator = this.getValidationMethod(rule);
rule.type = _this.getType(rule);
rule.validator = _this.getValidationMethod(rule);
if (!rule.validator) {
//console.log('no validator found for %s', z);
continue;
return;
}
series.push({rule: rule, value: value, source: source, field: z});
}
}
asyncMap(series, function (data, callback) {
series.push({ rule: rule, value: value, source: source, field: z });
});
});
asyncMap(series, function (data, doIt) {
var rule = data.rule;
var deep = (rule.type === 'object' || rule.type === 'array') && typeof(rule.fields) === 'object';
deep = deep && (rule.required || (!rule.required && data.value));
//console.log("Validating field %s", rule.field);
var deep = (rule.type === 'object' || rule.type === 'array') && typeof rule.fields === 'object';
deep = deep && (rule.required || !rule.required && data.value);
//console.log('Validating field %s', rule.field);
rule.field = data.field;
var cb = function (errors) {
var cb = function cb(errors) {
//delete rule.validator;
//delete rule.field;
//console.log("Completed rule validation...");
//console.log('doItd rule validation...');
if (errors && !Array.isArray(errors)) {

@@ -185,6 +189,6 @@ errors = [errors];

if (options.first && errors && errors.length) {
return complete(errors);
return doIt(errors);
}
if (!deep) {
callback(null, errors);
doIt(null, errors);
} else {

@@ -201,3 +205,3 @@ errors = errors || [];

}
return callback(null, errors);
return doIt(null, errors);
}

@@ -215,10 +219,8 @@ var fieldsSchema = data.rule.fields;

}
schema.validate(
data.value, data.rule.options || options, function (errs) {
callback(null, errs && errs.length ? errors.concat(errs) : errs);
});
schema.validate(data.value, data.rule.options || options, function (errs) {
doIt(null, errs && errs.length ? errors.concat(errs) : errs);
});
}
};
rule.validator(
rule, data.value, cb, data.source, options);
rule.validator(rule, data.value, cb, data.source, options);
}, function (err, results) {

@@ -237,3 +239,3 @@ complete(results);

Schema.prototype.getType = function (rule) {
if (rule.type === undefined && (rule.pattern instanceof RegExp)) {
if (rule.type === undefined && rule.pattern instanceof RegExp) {
rule.type = 'pattern';

@@ -245,4 +247,4 @@ }

//console.dir(rule);
if (typeof(rule.validator) !== 'function' && (!rule.type || !validators.hasOwnProperty(rule.type))) {
throw new Error(util.format("Unknown rule type %s", rule.type));
if (typeof rule.validator !== 'function' && (!rule.type || !validators.hasOwnProperty(rule.type))) {
throw new Error(util.format('Unknown rule type %s', rule.type));
}

@@ -276,3 +278,3 @@ return rule.type;

if (typeof validator !== 'function') {
throw new Error("Cannot register a validator by type, validator is not a function");
throw new Error('Cannot register a validator by type, validator is not a function');
}

@@ -282,2 +284,2 @@ validators[type] = validator;

module.exports.messages = messages;
module.exports.messages = defaultMessages;

@@ -1,4 +0,3 @@

/**
* Default validation error messages.
*/
'use strict';
var messages = {

@@ -10,5 +9,5 @@ 'default': 'Validation error on field %s',

date: {
format: "%s date %s is invalid for format %s",
parse: "%s date could not be parsed, %s is invalid ",
invalid: "%s date %s is invalid"
format: '%s date %s is invalid for format %s',
parse: '%s date could not be parsed, %s is invalid ',
invalid: '%s date %s is invalid'
},

@@ -50,3 +49,3 @@ types: {

},
clone: function () {
clone: function clone() {
var cloned = JSON.parse(JSON.stringify(this));

@@ -57,2 +56,2 @@ cloned.clone = this.clone;

};
module.exports = messages;
module.exports = messages;

@@ -0,2 +1,5 @@

'use strict';
var util = require('../util');
var ENUM = 'enum';

@@ -14,9 +17,9 @@ /**

*/
var enumerable = function (rule, value, source, errors, options) {
rule['enum'] = Array.isArray(rule['enum']) ? rule['enum'] : [];
if (rule['enum'].indexOf(value) === -1) {
errors.push(util.format(options.messages['enum'], rule.fullField, rule['enum'].join(', ')));
var enumerable = function enumerable(rule, value, source, errors, options) {
rule[ENUM] = Array.isArray(rule[ENUM]) ? rule[ENUM] : [];
if (rule[ENUM].indexOf(value) === -1) {
errors.push(util.format(options.messages[ENUM], rule.fullField, rule[ENUM].join(', ')));
}
};
module.exports = enumerable;
module.exports = enumerable;

@@ -0,1 +1,3 @@

'use strict';
module.exports = {

@@ -8,2 +10,2 @@ required: require('./required'),

pattern: require('./pattern')
};
};

@@ -0,1 +1,3 @@

'use strict';
var util = require('../util');

@@ -14,3 +16,3 @@

*/
var pattern = function (rule, value, source, errors, options) {
var pattern = function pattern(rule, value, source, errors, options) {
//console.log('testing pattern %s', value);

@@ -20,4 +22,3 @@ //console.log('testing with rule %s', rule.pattern);

if (!rule.pattern.test(value)) {
errors.push(util.format(options.messages.pattern.mismatch,
rule.fullField, value, rule.pattern));
errors.push(util.format(options.messages.pattern.mismatch, rule.fullField, value, rule.pattern));
}

@@ -27,2 +28,2 @@ }

module.exports = pattern;
module.exports = pattern;

@@ -0,1 +1,3 @@

'use strict';
var util = require('../util');

@@ -14,3 +16,3 @@

*/
var range = function (rule, value, source, errors, options) {
var range = function range(rule, value, source, errors, options) {
var len = typeof rule.len === 'number';

@@ -21,4 +23,4 @@ var min = typeof rule.min === 'number';

var key = null;
var num = typeof(value) === 'number';
var str = typeof(value) === 'string';
var num = typeof value === 'number';
var str = typeof value === 'string';
var arr = Array.isArray(value);

@@ -50,7 +52,6 @@ if (num) {

} else if (min && max && (val < rule.min || val > rule.max)) {
errors.push(util.format(options.messages[key].range,
rule.fullField, rule.min, rule.max));
errors.push(util.format(options.messages[key].range, rule.fullField, rule.min, rule.max));
}
};
module.exports = range;
module.exports = range;

@@ -0,1 +1,3 @@

'use strict';
var util = require('../util');

@@ -14,3 +16,3 @@

*/
var required = function (rule, value, source, errors, options) {
var required = function required(rule, value, source, errors, options) {
if (rule.required && (!source.hasOwnProperty(rule.field) || value === undefined || value === null)) {

@@ -21,2 +23,2 @@ errors.push(util.format(options.messages.required, rule.fullField));

module.exports = required;
module.exports = required;

@@ -0,35 +1,4 @@

'use strict';
var util = require('../util');
var types = {};
types.integer = function (value) {
return typeof(value) === 'number' && parseInt(value) === value;
};
types.float = function (value) {
return typeof(value) === 'number' && !types.integer(value);
};
types.array = function (value) {
return Array.isArray(value);
};
types.regexp = function (value) {
if (value instanceof RegExp) {
return true;
}
try {
return !!new RegExp(value);
} catch (e) {
return false;
}
};
types.object = function (value) {
return typeof(value) === 'object' && !types.array(value);
};
types.method = function (value) {
return typeof(value) === 'function';
};
var pattern = {

@@ -41,17 +10,45 @@ email: /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/,

types.email = function (value) {
return typeof(value) === 'string' && !!value.match(pattern.email);
var types = {
integer: function integer(value) {
return types.number(value) && parseInt(value) === value;
},
float: function float(value) {
return types.number(value) && !types.integer(value);
},
array: function array(value) {
return Array.isArray(value);
},
regexp: function regexp(value) {
if (value instanceof RegExp) {
return true;
}
try {
return !!new RegExp(value);
} catch (e) {
return false;
}
},
number: function number(value) {
if (isNaN(value)) {
return false;
}
return typeof value === 'number';
},
object: function object(value) {
return typeof value === 'object' && !types.array(value);
},
method: function method(value) {
return typeof value === 'function';
},
email: function email(value) {
return typeof value === 'string' && !!value.match(pattern.email);
},
url: function url(value) {
return typeof value === 'string' && !!value.match(pattern.url);
},
hex: function hex(value) {
return typeof value === 'string' && !!value.match(pattern.hex);
}
};
types.url = function (value) {
return typeof(value) === 'string' && !!value.match(pattern.url);
};
types.hex = function (value) {
return typeof(value) === 'string' && !!value.match(pattern.hex);
};
//types.string = function(value) {
//return typeof(value) === 'string' || (value instanceof String);
//}
/**

@@ -68,3 +65,3 @@ * Rule for validating the type of a value.

*/
var type = function (rule, value, source, errors, options) {
var type = function type(rule, value, source, errors, options) {
// if value is required and value is undefined

@@ -75,16 +72,16 @@ // no need to add this error message

}
var custom = ['integer', 'float', 'array', 'regexp', 'object', 'method', 'email'];
var type = rule.type;
if (custom.indexOf(type) > -1) {
if (!types[type](value)) {
errors.push(util.format(options.messages.types[type], rule.fullField, rule.type));
var custom = ['integer', 'float', 'array', 'regexp', 'object', 'method', 'email', 'number'];
var ruleType = rule.type;
if (custom.indexOf(ruleType) > -1) {
if (!types[ruleType](value)) {
errors.push(util.format(options.messages.types[ruleType], rule.fullField, rule.type));
}
// straight typeof check
} else if (type && typeof(value) !== rule.type) {
} else if (ruleType && typeof value !== rule.type) {
//console.log("checking type %s", type);
//console.log("checking value %s", value);
errors.push(util.format(options.messages.types[type], rule.fullField, rule.type));
errors.push(util.format(options.messages.types[ruleType], rule.fullField, rule.type));
}
};
module.exports = type;
module.exports = type;

@@ -0,1 +1,3 @@

'use strict';
var util = require('../util');

@@ -14,4 +16,4 @@

*/
var whitespace = function (rule, value, source, errors, options) {
if (/^\s+$/.test(value) || value === "") {
var whitespace = function whitespace(rule, value, source, errors, options) {
if (/^\s+$/.test(value) || value === '') {
errors.push(util.format(options.messages.whitespace, rule.fullField));

@@ -21,2 +23,2 @@ }

module.exports = whitespace;
module.exports = whitespace;

@@ -0,1 +1,4 @@

'use strict';
var _arguments = arguments;
var formatRegExp = /%[sdj%]/g;

@@ -5,3 +8,3 @@

var i = 1;
var args = arguments;
var args = _arguments;
var len = args.length;

@@ -31,6 +34,6 @@ var str = String(f).replace(formatRegExp, function (x) {

});
for (var x = args[i]; i < len; x = args[++i]) {
str += ' ' + x;
for (var arg = args[i]; i < len; arg = args[++i]) {
str += ' ' + arg;
}
return str;
};
};

@@ -0,1 +1,3 @@

'use strict';
var rules = require('../rule/');

@@ -13,5 +15,5 @@

*/
var array = function (rule, value, callback, source, options) {
var array = function array(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
if (validate) {

@@ -30,2 +32,2 @@ if (value === undefined && !rule.required) {

module.exports = array;
module.exports = array;

@@ -0,1 +1,3 @@

'use strict';
var rules = require('../rule/');

@@ -13,5 +15,5 @@

*/
var boolean = function (rule, value, callback, source, options) {
var boolean = function boolean(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
if (validate) {

@@ -27,2 +29,2 @@ if (value === undefined && !rule.required) {

module.exports = boolean;
module.exports = boolean;

@@ -0,2 +1,5 @@

'use strict';
var rules = require('../rule/');
var ENUM = 'enum';

@@ -13,5 +16,5 @@ /**

*/
var enumerable = function (rule, value, callback, source, options) {
var enumerable = function enumerable(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
if (validate) {

@@ -22,3 +25,3 @@ if (value === undefined && !rule.required) {

rules.required(rule, value, source, errors, options);
rules['enum'](rule, value, source, errors, options);
rules[ENUM](rule, value, source, errors, options);
}

@@ -28,2 +31,2 @@ callback(errors);

module.exports = enumerable;
module.exports = enumerable;

@@ -0,1 +1,3 @@

'use strict';
var rules = require('../rule/');

@@ -13,5 +15,5 @@

*/
var float = function (rule, value, callback, source, options) {
var float = function float(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
if (validate) {

@@ -28,2 +30,2 @@ if (value === undefined && !rule.required) {

module.exports = float;
module.exports = float;

@@ -0,1 +1,3 @@

'use strict';
module.exports = {

@@ -13,5 +15,5 @@ string: require('./string'),

pattern: require('./pattern'),
email:require('./type'),
url:require('./type'),
hex:require('./type')
};
email: require('./type'),
url: require('./type'),
hex: require('./type')
};

@@ -0,1 +1,3 @@

'use strict';
var rules = require('../rule/');

@@ -13,6 +15,6 @@

*/
var integer = function (rule, value, callback, source, options) {
var integer = function integer(rule, value, callback, source, options) {
//console.log('integer rule called %j', rule);
var errors = [];
var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
//console.log('validate on %s value', value);

@@ -30,2 +32,2 @@ if (validate) {

module.exports = integer;
module.exports = integer;

@@ -0,1 +1,3 @@

'use strict';
var rules = require('../rule/');

@@ -13,5 +15,5 @@

*/
var method = function (rule, value, callback, source, options) {
var method = function method(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
if (validate) {

@@ -27,2 +29,2 @@ if (value === undefined && !rule.required) {

module.exports = method;
module.exports = method;

@@ -0,1 +1,3 @@

'use strict';
var rules = require('../rule/');

@@ -13,5 +15,5 @@

*/
var number = function (rule, value, callback, source, options) {
var number = function number(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
if (validate) {

@@ -28,2 +30,2 @@ if (value === undefined && !rule.required) {

module.exports = number;
module.exports = number;

@@ -0,1 +1,3 @@

'use strict';
var rules = require('../rule/');

@@ -13,5 +15,5 @@

*/
var object = function (rule, value, callback, source, options) {
var object = function object(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
if (validate) {

@@ -29,2 +31,2 @@ if (value === undefined && !rule.required) {

module.exports = object;
module.exports = object;

@@ -0,1 +1,3 @@

'use strict';
var rules = require('../rule/');

@@ -16,5 +18,5 @@

*/
var pattern = function (rule, value, callback, source, options) {
var pattern = function pattern(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
if (validate) {

@@ -29,2 +31,2 @@ if (value === undefined && !rule.required) {

module.exports = pattern;
module.exports = pattern;

@@ -0,1 +1,3 @@

'use strict';
var rules = require('../rule/');

@@ -13,5 +15,5 @@

*/
var regexp = function (rule, value, callback, source, options) {
var regexp = function regexp(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
if (validate) {

@@ -27,2 +29,2 @@ if (value === undefined && !rule.required) {

module.exports = regexp;
module.exports = regexp;

@@ -0,1 +1,3 @@

'use strict';
var rules = require('../rule/');

@@ -13,5 +15,5 @@

*/
var string = function (rule, value, callback, source, options) {
var string = function string(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
if (validate) {

@@ -32,2 +34,2 @@ if (value === undefined && !rule.required) {

module.exports = string;
module.exports = string;

@@ -0,4 +1,6 @@

'use strict';
var rules = require('../rule/');
var type = function (rule, value, callback, source, options) {
var type = function type(rule, value, callback, source, options) {
var errors = [];

@@ -9,2 +11,2 @@ rules.type(rule, value, source, errors, options);

module.exports = type;
module.exports = type;
{
"name": "async-validator",
"version": "1.1.1",
"version": "1.1.2",
"description": "validate form asynchronous",

@@ -16,2 +16,3 @@ "keywords": [

},
"main": "./lib/index",
"bugs": {

@@ -21,4 +22,2 @@ "url": "http://github.com/yiminghe/async-validator/issues"

"licenses": "MIT",
"spm": {
},
"config": {

@@ -28,10 +27,13 @@ "port": 8010

"scripts": {
"build": "rc-tools run build",
"precommit": "rc-tools run precommit",
"less": "rc-tools run less",
"gh-pages": "rc-tools run gh-pages",
"history": "rc-tools run history",
"start": "node --harmony node_modules/.bin/rc-server",
"publish": "rc-tools run tag && spm publish",
"publish": "rc-tools run tag",
"lint": "rc-tools run lint",
"test": "",
"saucelabs": "rc-tools run saucelabs",
"browser-test": "rc-tools run browser-test",
"browser-test-cover": "rc-tools run browser-test-cover"
"saucelabs": "node --harmony node_modules/.bin/rc-tools run saucelabs",
"browser-test": "node --harmony node_modules/.bin/rc-tools run browser-test",
"browser-test-cover": "node --harmony node_modules/.bin/rc-tools run browser-test-cover"
},

@@ -41,8 +43,9 @@ "devDependencies": {

"precommit-hook": "^1.0.7",
"rc-server": "^2.0.0",
"rc-tools": "^1.1.0"
"rc-server": "3.x",
"rc-tools": "3.x",
"react": "~0.13.3"
},
"precommit": [
"lint"
"precommit"
]
}
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