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

async-validate

Package Overview
Dependencies
Maintainers
1
Versions
97
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-validate - npm Package Compare versions

Comparing version 0.2.1 to 0.2.2

17

lib/rule/date.js

@@ -8,13 +8,12 @@ var format = require('../format')

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param source The source object being validated.
* @param errors An array of errors that this rule may add
* validation errors to.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
*/
function date(rule, value, source, errors, options) {
function date(opts) {
var rule = opts.rule
, value = opts.value
, options = opts.options
, errors = opts.errors;
if(!rule.required
&& (value == undefined || value == "")) {
&& (value === undefined || value === "")) {
return false;

@@ -21,0 +20,0 @@ }

@@ -7,11 +7,9 @@ var format = require('../format')

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param source The source object being validated.
* @param errors An array of errors that this rule may add
* validation errors to.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
*/
function enumerable(rule, value, source, errors, options) {
function enumerable(opts) {
var rule = opts.rule
, value = opts.value
, options = opts.options
, errors = opts.errors;
rule.enum = Array.isArray(rule.enum) ? rule.enum : [];

@@ -18,0 +16,0 @@ if(rule.enum.indexOf(value) == -1) {

@@ -7,13 +7,10 @@ var format = require('../format')

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param source The source object being validated.
* @param errors An array of errors that this rule may add
* validation errors to.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
*/
function pattern(rule, value, source, errors, options) {
//console.log('testing pattern %s', value);
//console.log('testing with rule %s', rule.pattern);
function pattern(opts) {
var rule = opts.rule
, value = opts.value
, options = opts.options
, errors = opts.errors;
if(rule.pattern instanceof RegExp) {

@@ -20,0 +17,0 @@ if(!rule.pattern.test(value)) {

@@ -7,11 +7,10 @@ var format = require('../format')

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param source The source object being validated.
* @param errors An array of errors that this rule may add
* validation errors to.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
*/
function range(rule, value, source, errors, options) {
function range(opts) {
var rule = opts.rule
, value = opts.value
, options = opts.options
, errors = opts.errors;
var len = typeof rule.len === 'number';

@@ -18,0 +17,0 @@ var min = typeof rule.min === 'number';

@@ -7,11 +7,11 @@ var format = require('../format')

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param source The source object being validated.
* @param errors An array of errors that this rule may add
* validation errors to.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
*/
function required(rule, value, source, errors, options) {
function required(opts) {
var rule = opts.rule
, value = opts.value
, source = opts.source
, options = opts.options
, errors = opts.errors;
if(rule.required

@@ -18,0 +18,0 @@ && (!source.hasOwnProperty(rule.field)

@@ -37,24 +37,19 @@ var format = require('../format')

//types.string = function(value) {
//return typeof(value) == 'string' || (value instanceof String);
//}
/**
* Rule for validating the type of a value.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param source The source object being validated.
* @param errors An array of errors that this rule may add
* validation errors to.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
*/
function type(rule, value, source, errors, options) {
function type(opts) {
var rule = opts.rule
, value = opts.value
, options = opts.options
, errors = opts.errors;
// if value is required and value is undefined
// no need to add this error message
if(rule.required && value == undefined) {
if(rule.required && (value === undefined || value === null)) {
return;
}
var custom = ['integer', 'float', 'array', 'regexp', 'object', 'method'];

@@ -69,4 +64,2 @@ var type = rule.type;

}else if(type && !(typeof(value) == rule.type)) {
//console.log("checking type %s", type);
//console.log("checking value %s", value);
errors.push(error(rule,

@@ -73,0 +66,0 @@ format(options.messages.types[type], rule.field, rule.type)));

@@ -7,11 +7,9 @@ var format = require('../format')

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param source The source object being validated.
* @param errors An array of errors that this rule may add
* validation errors to.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
*/
function whitespace(rule, value, source, errors, options) {
function whitespace(opts) {
var rule = opts.rule
, value = opts.value
, options = opts.options
, errors = opts.errors;
if(/^\s+$/.test(value) || value == "") {

@@ -18,0 +16,0 @@ errors.push(error(rule,

@@ -15,5 +15,4 @@ var iterator = require('./iterator')

opts = opts || {};
var msg = opts.messages || require('../messages');
this.rules = null;
this.messages(msg);
this.rules = {};
this.messages(opts.messages || require('../messages'));
this.define(descriptor);

@@ -39,14 +38,14 @@ }

*
* If rules are already defined new rules will overwrite or add to the
* previously defined rules.
*
* @param rules The schema rules.
*
* @api public
*/
function define(rules) {
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")
if(typeof rules !== 'object' || Array.isArray(rules)) {
throw new Error('Rules must be an object')
}
this.rules = {};
var z, item;

@@ -65,8 +64,6 @@ for(z in rules) {

* @param callback A callback to invoke when validation is complete.
*
* @api public
*/
function validate(source, options, callback) {
if(!this.rules) {
throw new Error("Cannot validate with no rules.");
throw new Error('Cannot validate with no rules.');
}

@@ -169,4 +166,8 @@ options = options || {};

}
rule.validator(
rule, data.value, cb, data.source, options);
var opts = getValidationOptions(rule, data, options, cb);
rule.validator(opts, cb);
//rule.validator(
//rule, data.value, cb, data.source, options);
}, function(err, results) {

@@ -177,2 +178,15 @@ complete(results);

function getValidationOptions(rule, data, options, cb) {
return {
callback: cb,
rule: rule,
value: data.value,
source: data.source,
data: data,
errors: [],
options: options,
messages: options.messages
}
}
/**

@@ -182,4 +196,2 @@ * Infer the type of a rule when necessary.

* @param rule The validation rule.
*
* @api private
*/

@@ -193,3 +205,3 @@ function getType(rule) {

&& (!rule.type || !validators.hasOwnProperty(rule.type))) {
throw new Error(format("Unknown rule type %s", rule.type));
throw new Error(format('Unknown rule type %s', rule.type));
}

@@ -203,4 +215,2 @@ return rule.type;

* @param rule The validation rule.
*
* @api private
*/

@@ -233,3 +243,3 @@ function getValidationMethod(rule) {

throw new Error(
"Cannot register a validator by type, validator is not a function");
'Cannot register a validator by type, validator is not a function');
}

@@ -236,0 +246,0 @@ validators[type] = validator;

@@ -6,24 +6,26 @@ var rules = require('../rule');

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
* @param cb The callback function.
*/
function array(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
function array(opts, cb) {
var errors = opts.errors
, rule = opts.rule
, value = opts.value
, source = opts.source
, validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
if(validate) {
if(value === undefined && !rule.required) return callback();
rules.required(rule, value, source, errors, options);
if(rule.required || value != undefined) {
rules.type(rule, value, source, errors, options);
rules.range(rule, value, source, errors, options);
if(value === undefined && !rule.required) {
return cb();
}
rules.required(opts);
if(rule.required || value !== undefined) {
rules.type(opts);
rules.range(opts);
}
}
callback(errors);
cb(errors);
}
module.exports = array;

@@ -6,21 +6,23 @@ var rules = require('../rule');

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
* @param cb The callback function.
*/
function bool(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
function bool(opts, cb) {
var errors = opts.errors
, rule = opts.rule
, value = opts.value
, source = opts.source
, validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
if(validate) {
if(value === undefined && !rule.required) return callback();
rules.required(rule, value, source, errors, options);
rules.type(rule, value, source, errors, options);
if(value === undefined && !rule.required) {
return cb();
}
rules.required(opts);
rules.type(opts);
}
callback(errors);
cb(errors);
}
module.exports = bool;

@@ -6,23 +6,25 @@ var rules = require('../rule');

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
* @param cb The callback function.
*/
function date(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field) && source[rule.field]);
function date(opts, cb) {
var errors = opts.errors
, rule = opts.rule
, value = opts.value
, source = opts.source
, validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field)
&& source[rule.field]);
if(validate) {
//console.log("Validating date %s with value: %s", rule.field, value);
if(value === undefined && !rule.required) return callback();
rules.required(rule, value, source, errors, options);
rules.pattern(rule, value, source, errors, options);
rules.date(rule, value, source, errors, options);
if(value === undefined && !rule.required) {
return cb();
}
rules.required(opts);
rules.pattern(opts);
rules.date(opts);
}
callback(errors);
cb(errors);
}
module.exports = date;

@@ -6,21 +6,23 @@ var rules = require('../rule');

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
* @param cb The callback function.
*/
function enumerable(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
function enumerable(opts, cb) {
var errors = opts.errors
, rule = opts.rule
, value = opts.value
, source = opts.source
, validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
if(validate) {
if(value === undefined && !rule.required) return callback();
rules.required(rule, value, source, errors, options);
rules.enum(rule, value, source, errors, options);
if(value === undefined && !rule.required) {
return cb();
}
rules.required(opts);
rules.enum(opts);
}
callback(errors);
cb(errors);
}
module.exports = enumerable;

@@ -6,22 +6,24 @@ var rules = require('../rule');

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
* @param cb The callback function.
*/
function float(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
function fraction(opts, cb) {
var errors = opts.errors
, rule = opts.rule
, value = opts.value
, source = opts.source
, validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
if(validate) {
if(value === undefined && !rule.required) return callback();
rules.required(rule, value, source, errors, options);
rules.type(rule, value, source, errors, options);
rules.range(rule, value, source, errors, options);
if(value === undefined && !rule.required) {
return cb();
}
rules.required(opts);
rules.type(opts);
rules.range(opts);
}
callback(errors);
cb(errors);
}
module.exports = float;
module.exports = fraction;

@@ -6,24 +6,24 @@ var rules = require('../rule');

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
* @param cb The callback function.
*/
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));
//console.log('validate on %s value', value);
function integer(opts, cb) {
var errors = opts.errors
, rule = opts.rule
, value = opts.value
, source = opts.source
, validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
if(validate) {
if(value === undefined && !rule.required) return callback();
rules.required(rule, value, source, errors, options);
rules.type(rule, value, source, errors, options);
rules.range(rule, value, source, errors, options);
if(value === undefined && !rule.required) {
return cb();
}
rules.required(opts);
rules.type(opts);
rules.range(opts);
}
callback(errors);
cb(errors);
}
module.exports = integer;

@@ -6,21 +6,23 @@ var rules = require('../rule');

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
* @param cb The callback function.
*/
function method(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
function method(opts, cb) {
var errors = opts.errors
, rule = opts.rule
, value = opts.value
, source = opts.source
, validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
if(validate) {
if(value === undefined && !rule.required) return callback();
rules.required(rule, value, source, errors, options);
rules.type(rule, value, source, errors, options);
if(value === undefined && !rule.required) {
return cb();
}
rules.required(opts);
rules.type(opts);
}
callback(errors);
cb(errors);
}
module.exports = method;

@@ -6,22 +6,25 @@ var rules = require('../rule');

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
* @param cb The callback function.
*/
function number(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
function number(opts, cb) {
var errors = opts.errors
, rule = opts.rule
, value = opts.value
, source = opts.source
, validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
if(validate) {
if(value === undefined && !rule.required) return callback();
rules.required(rule, value, source, errors, options);
rules.type(rule, value, source, errors, options);
rules.range(rule, value, source, errors, options);
if(value === undefined && !rule.required) {
return cb();
}
rules.required(opts);
rules.type(opts);
rules.range(opts);
}
callback(errors);
cb(errors);
}
module.exports = number;

@@ -6,23 +6,23 @@ var rules = require('../rule');

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
* @param cb The callback function.
*/
function object(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
function object(opts, cb) {
var errors = opts.errors
, rule = opts.rule
, value = opts.value
, source = opts.source
, validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
if(validate) {
if(value === undefined && !rule.required) return callback();
rules.required(rule, value, source, errors, options);
if(value === undefined && !rule.required) return cb();
rules.required(opts);
if(rule.required || value != undefined) {
rules.type(rule, value, source, errors, options);
rules.type(opts);
}
}
callback(errors);
cb(errors);
}
module.exports = object;

@@ -9,20 +9,22 @@ var rules = require('../rule');

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
* @param cb The callback function.
*/
function pattern(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
function pattern(opts, cb) {
var errors = opts.errors
, rule = opts.rule
, value = opts.value
, source = opts.source
, validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
if(validate) {
if(value === undefined && !rule.required) return callback();
rules.pattern(rule, value, source, errors, options);
if(value === undefined && !rule.required) {
return cb();
}
rules.pattern(opts);
}
callback(errors);
cb(errors);
}
module.exports = pattern;

@@ -6,21 +6,23 @@ var rules = require('../rule');

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
* @param cb The callback function.
*/
function regexp(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
function regexp(opts, cb) {
var errors = opts.errors
, rule = opts.rule
, value = opts.value
, source = opts.source
, validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
if(validate) {
if(value === undefined && !rule.required) return callback();
rules.required(rule, value, source, errors, options);
rules.type(rule, value, source, errors, options);
if(value === undefined && !rule.required) {
return cb();
}
rules.required(opts);
rules.type(opts);
}
callback(errors);
cb(errors);
}
module.exports = regexp;

@@ -6,26 +6,30 @@ var rules = require('../rule');

*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
* @param opts The validation options.
* @param cb The callback function.
*/
function string(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
function string(opts, cb) {
var errors = opts.errors
, rule = opts.rule
, value = opts.value
, source = opts.source
, validate = rule.required
|| (!rule.required && source.hasOwnProperty(rule.field));
if(validate) {
if(value === undefined && !rule.required) return callback();
rules.required(rule, value, source, errors, options);
rules.type(rule, value, source, errors, options);
rules.range(rule, value, source, errors, options);
rules.pattern(rule, value, source, errors, options);
if(value === undefined && !rule.required) {
return cb();
}
rules.required(opts);
rules.type(opts);
rules.range(opts);
rules.pattern(opts);
if(rule.whitespace === true) {
rules.whitespace(rule, value, source, errors, options);
rules.whitespace(opts);
}
}
callback(errors);
cb(errors);
}
module.exports = string;
{
"name": "async-validate",
"description": "Asynchronous validation for object properties.",
"version": "0.2.1",
"version": "0.2.2",
"author": "muji <noop@xpm.io>",

@@ -27,3 +27,3 @@ "repository": {

"scripts": {
"browserify": "browserify -o async-validate.js -e ./lib/index.js",
"browserify": "browserify -o async-validate.js -e ./lib/schema.js",
"clean": "rm -rf coverage",

@@ -30,0 +30,0 @@ "test": "NODE_ENV=test mocha ${SPEC:-test/spec}",

@@ -39,5 +39,8 @@ var util = require('util');

var descriptor = {
name: function(rule, value, callback, source, options) {
var errors = options.error(rule, "%s is a required field", rule.field);
callback(errors);
name: function(opts ,cb) {
var errors =
opts.options.error(
opts.rule, "%s is a required field", opts.rule.field);
cb(errors);
}

@@ -44,0 +47,0 @@ }

@@ -45,10 +45,10 @@ var util = require('util');

std.email,
function(descriptor, value, callback, values) {
function(opts, cb) {
var errors = [];
var email = "user@example.com";
if(value == email) {
if(opts.value === email) {
errors.push(new ValidationError(
util.format("Email address %s already exists", email)));
}
callback(errors);
cb(errors);
}

@@ -55,0 +55,0 @@ ]

@@ -9,10 +9,10 @@ var util = require('util');

before(function(done) {
var validator = function(rule, value, callback, source) {
var errors = [];
var validator = function(opts, cb) {
var errors = opts.errors;
var re = /^[^-][a-zA-Z0-9-]+$/;
if(!re.test(value)) {
if(!re.test(opts.value)) {
errors.push(new ValidationError(
util.format("%s is not a valid identifier", rule.field)));
util.format("%s is not a valid identifier", opts.rule.field)));
}
callback(errors);
cb(errors);
}

@@ -19,0 +19,0 @@ schema.register('id', validator);

@@ -21,11 +21,11 @@ var util = require('util');

var descriptor = {
name: function(descriptor, value, callback, values) {
var errors = [];
if(!/^[a-z0-9]+$/.test(value)) {
name: function(opts, cb) {
var errors = opts.errors;
if(!/^[a-z0-9]+$/.test(opts.value)) {
errors.push(
new ValidationError(
util.format("%s must be lowercase alphanumeric characters",
descriptor.field)));
opts.rule.field)));
}
callback(errors);
cb(errors);
}

@@ -32,0 +32,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