async-validator
Advanced tools
Comparing version 1.0.1 to 1.0.3
@@ -25,3 +25,3 @@ var Schema = require('../'); | ||
setTimeout(function () { | ||
callback([new Error(rule.message)]); | ||
callback(rule.message); | ||
}, 100); | ||
@@ -28,0 +28,0 @@ }, |
@@ -159,6 +159,12 @@ var util = require('./util'); | ||
//console.log("Completed rule validation..."); | ||
if (errors && !Array.isArray(errors)) { | ||
errors = [errors]; | ||
} | ||
if (errors) { | ||
errors.forEach(function (e) { | ||
errors.forEach(function (e, i) { | ||
if (!e.message) { | ||
e = new Error(e); | ||
} | ||
e.field = e.field || rule.fullField; | ||
errors[i] = e; | ||
}); | ||
@@ -251,4 +257,3 @@ } | ||
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"); | ||
} | ||
@@ -258,4 +263,2 @@ validators[type] = validator; | ||
module.exports.rule = require('./rule/'); | ||
module.exports.validators = validators; | ||
module.exports.messages = messages; |
var util = require('../util'); | ||
var error = require('./error'); | ||
@@ -18,4 +17,3 @@ /** | ||
if (rule['enum'].indexOf(value) === -1) { | ||
errors.push(error(rule, | ||
util.format(options.messages['enum'], rule.fullField, rule['enum'].join(', ')))); | ||
errors.push(util.format(options.messages['enum'], rule.fullField, rule['enum'].join(', '))); | ||
} | ||
@@ -22,0 +20,0 @@ }; |
module.exports = { | ||
error: require('./error'), | ||
required: require('./required'), | ||
@@ -4,0 +3,0 @@ whitespace: require('./whitespace'), |
var util = require('../util'); | ||
var error = require('./error'); | ||
@@ -20,5 +19,4 @@ /** | ||
if (!rule.pattern.test(value)) { | ||
errors.push(error(rule, | ||
util.format(options.messages.pattern.mismatch, | ||
rule.fullField, value, rule.pattern))); | ||
errors.push(util.format(options.messages.pattern.mismatch, | ||
rule.fullField, value, rule.pattern)); | ||
} | ||
@@ -25,0 +23,0 @@ } |
var util = require('../util'); | ||
var error = require('./error'); | ||
@@ -42,15 +41,11 @@ /** | ||
if (val !== rule.len) { | ||
errors.push(error(rule, | ||
util.format(options.messages[key].len, rule.fullField, rule.len))); | ||
errors.push(util.format(options.messages[key].len, rule.fullField, rule.len)); | ||
} | ||
} else if (min && !max && val < rule.min) { | ||
errors.push(error(rule, | ||
util.format(options.messages[key].min, rule.fullField, rule.min))); | ||
errors.push(util.format(options.messages[key].min, rule.fullField, rule.min)); | ||
} else if (max && !min && val > rule.max) { | ||
errors.push(error(rule, | ||
util.format(options.messages[key].max, rule.fullField, rule.max))); | ||
errors.push(util.format(options.messages[key].max, rule.fullField, rule.max)); | ||
} else if (min && max && (val < rule.min || val > rule.max)) { | ||
errors.push(error(rule, | ||
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)); | ||
} | ||
@@ -57,0 +52,0 @@ }; |
var util = require('../util'); | ||
var error = require('./error'); | ||
@@ -17,4 +16,3 @@ /** | ||
if (rule.required && (!source.hasOwnProperty(rule.field) || value === undefined || value === null)) { | ||
errors.push(error(rule, | ||
util.format(options.messages.required, rule.fullField))); | ||
errors.push(util.format(options.messages.required, rule.fullField)); | ||
} | ||
@@ -21,0 +19,0 @@ }; |
var util = require('../util'); | ||
var error = require('./error'); | ||
var types = {}; | ||
var types = {}; | ||
types.integer = function (value) { | ||
@@ -78,4 +77,3 @@ return typeof(value) === 'number' && parseInt(value) === value; | ||
if (!types[type](value)) { | ||
errors.push(error(rule, | ||
util.format(options.messages.types[type], rule.fullField, rule.type))); | ||
errors.push(util.format(options.messages.types[type], rule.fullField, rule.type)); | ||
} | ||
@@ -86,4 +84,3 @@ // straight typeof check | ||
//console.log("checking value %s", value); | ||
errors.push(error(rule, | ||
util.format(options.messages.types[type], rule.fullField, rule.type))); | ||
errors.push(util.format(options.messages.types[type], rule.fullField, rule.type)); | ||
} | ||
@@ -90,0 +87,0 @@ }; |
var util = require('../util'); | ||
var error = require('./error'); | ||
@@ -17,4 +16,3 @@ /** | ||
if (/^\s+$/.test(value) || value === "") { | ||
errors.push(error(rule, | ||
util.format(options.messages.whitespace, rule.fullField))); | ||
errors.push(util.format(options.messages.whitespace, rule.fullField)); | ||
} | ||
@@ -21,0 +19,0 @@ }; |
{ | ||
"name": "async-validator", | ||
"version": "1.0.1", | ||
"version": "1.0.3", | ||
"description": "validate form asynchronous", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -89,4 +89,3 @@ # async-validator | ||
* `options`: Additional options. | ||
* `options.messages`: The object containing validation error messages. | ||
* `options.error`: A helper function for generating validation errors. | ||
* `options.messages`: The object containing validation error messages | ||
@@ -124,3 +123,3 @@ The options passed to `validate` are passed on to the validation functions so that you may reference transient data (such as model references) in validation functions. However, some option names are reserved; if you use these properties of the options object they are overwritten. The reserved properties are `messages`, `exception` and `error`. | ||
{type: "string", required: true, pattern: schema.pattern.email}, | ||
function(rule, value, callback, source, options) { | ||
{validator:function(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -130,3 +129,3 @@ // test if email address already exists in a database | ||
callback(errors); | ||
} | ||
}} | ||
] | ||
@@ -312,2 +311,23 @@ } | ||
If you are defining your own validation functions it is better practice to assign the message strings to a messages object and then access the messages via the `options.messages` property within the validation function. | ||
If you are defining your own validation functions it is better practice to assign the message strings to a messages object and then access the messages via the `options.messages` property within the validation function. | ||
### validator | ||
you can custom validate function for specified field: | ||
```js | ||
{ | ||
asyncField:{ | ||
validator: function(rule,value,callback){ | ||
ajax({ | ||
url:'xx', | ||
value:value | ||
}).then(function(data){ | ||
callback(null); | ||
},function(error){ | ||
callback(new Error(error)) | ||
}); | ||
} | ||
} | ||
} | ||
``` |
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
329
43199
32
907