async-validator
Advanced tools
Comparing version 1.4.2 to 1.5.0
# History | ||
---- | ||
## 1.5.0 / 2016-02-02 | ||
- support deep merge with default messages | ||
- support rule message of any type(exp: jsx) | ||
## 1.4.0 / 2015-01-12 | ||
@@ -5,0 +10,0 @@ |
@@ -19,6 +19,14 @@ 'use strict'; | ||
var _messages3 = _interopRequireDefault(_messages2); | ||
var _rule = require('./rule/'); | ||
var _lodashMergewith = require('lodash.mergewith'); | ||
var _lodashMergewith2 = _interopRequireDefault(_lodashMergewith); | ||
function mergeCustomizer(objValue, srcValue) { | ||
if (typeof objValue !== 'object') { | ||
return srcValue; | ||
} | ||
} | ||
/** | ||
@@ -32,3 +40,3 @@ * Encapsulates a validation schema. | ||
this.rules = null; | ||
this._messages = _messages3['default']; | ||
this._messages = _messages2.messages; | ||
this.define(descriptor); | ||
@@ -40,3 +48,3 @@ } | ||
if (_messages) { | ||
this._messages = _messages; | ||
this._messages = (0, _lodashMergewith2['default'])((0, _messages2.newMessages)(), _messages, mergeCustomizer); | ||
} | ||
@@ -84,6 +92,6 @@ return this._messages; | ||
function add(e) { | ||
if (e instanceof Error) { | ||
if (Array.isArray(e)) { | ||
errors = errors.concat.apply(errors, e); | ||
} else { | ||
errors.push(e); | ||
} else if (Array.isArray(e)) { | ||
errors = errors.concat.apply(errors, e); | ||
} | ||
@@ -108,3 +116,13 @@ } | ||
options.messages = options.messages || this.messages(); | ||
if (options.messages) { | ||
var messages = this.messages(); | ||
if (messages === _messages2.messages) { | ||
messages = (0, _messages2.newMessages)(); | ||
} | ||
(0, _lodashMergewith2['default'])(messages, options.messages, mergeCustomizer); | ||
options.messages = messages; | ||
} else { | ||
options.messages = this.messages(); | ||
} | ||
options.error = _rule.error; | ||
@@ -234,5 +252,5 @@ var arr = undefined; | ||
Schema.messages = _messages3['default']; | ||
Schema.messages = _messages2.messages; | ||
exports['default'] = Schema; | ||
module.exports = exports['default']; |
@@ -6,55 +6,60 @@ 'use strict'; | ||
}); | ||
var messages = { | ||
'default': 'Validation error on field %s', | ||
required: '%s is required', | ||
'enum': '%s must be one of %s', | ||
whitespace: '%s cannot be empty', | ||
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' | ||
}, | ||
types: { | ||
string: '%s is not a %s', | ||
method: '%s is not a %s (function)', | ||
array: '%s is not an %s', | ||
object: '%s is not an %s', | ||
number: '%s is not a %s', | ||
date: '%s is not a %s', | ||
boolean: '%s is not a %s', | ||
integer: '%s is not an %s', | ||
float: '%s is not a %s', | ||
regexp: '%s is not a valid %s', | ||
email: '%s is not a valid %s', | ||
url: '%s is not a valid %s', | ||
hex: '%s is not a valid %s' | ||
}, | ||
string: { | ||
len: '%s must be exactly %s characters', | ||
min: '%s must be at least %s characters', | ||
max: '%s cannot be longer than %s characters', | ||
range: '%s must be between %s and %s characters' | ||
}, | ||
number: { | ||
len: '%s must equal %s', | ||
min: '%s cannot be less than %s', | ||
max: '%s cannot be greater than %s', | ||
range: '%s must be between %s and %s' | ||
}, | ||
array: { | ||
len: '%s must be exactly %s in length', | ||
min: '%s cannot be less than %s in length', | ||
max: '%s cannot be greater than %s in length', | ||
range: '%s must be between %s and %s in length' | ||
}, | ||
pattern: { | ||
mismatch: '%s value %s does not match pattern %s' | ||
}, | ||
clone: function clone() { | ||
var cloned = JSON.parse(JSON.stringify(this)); | ||
cloned.clone = this.clone; | ||
return cloned; | ||
} | ||
}; | ||
exports['default'] = messages; | ||
module.exports = exports['default']; | ||
exports.newMessages = newMessages; | ||
function newMessages() { | ||
return { | ||
'default': 'Validation error on field %s', | ||
required: '%s is required', | ||
'enum': '%s must be one of %s', | ||
whitespace: '%s cannot be empty', | ||
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' | ||
}, | ||
types: { | ||
string: '%s is not a %s', | ||
method: '%s is not a %s (function)', | ||
array: '%s is not an %s', | ||
object: '%s is not an %s', | ||
number: '%s is not a %s', | ||
date: '%s is not a %s', | ||
boolean: '%s is not a %s', | ||
integer: '%s is not an %s', | ||
float: '%s is not a %s', | ||
regexp: '%s is not a valid %s', | ||
email: '%s is not a valid %s', | ||
url: '%s is not a valid %s', | ||
hex: '%s is not a valid %s' | ||
}, | ||
string: { | ||
len: '%s must be exactly %s characters', | ||
min: '%s must be at least %s characters', | ||
max: '%s cannot be longer than %s characters', | ||
range: '%s must be between %s and %s characters' | ||
}, | ||
number: { | ||
len: '%s must equal %s', | ||
min: '%s cannot be less than %s', | ||
max: '%s cannot be greater than %s', | ||
range: '%s must be between %s and %s' | ||
}, | ||
array: { | ||
len: '%s must be exactly %s in length', | ||
min: '%s cannot be less than %s in length', | ||
max: '%s cannot be greater than %s in length', | ||
range: '%s must be between %s and %s in length' | ||
}, | ||
pattern: { | ||
mismatch: '%s value %s does not match pattern %s' | ||
}, | ||
clone: function clone() { | ||
var cloned = JSON.parse(JSON.stringify(this)); | ||
cloned.clone = this.clone; | ||
return cloned; | ||
} | ||
}; | ||
} | ||
var messages = newMessages(); | ||
exports.messages = messages; |
@@ -21,29 +21,35 @@ 'use strict'; | ||
var len = args.length; | ||
var str = String(f).replace(formatRegExp, function (x) { | ||
if (x === '%%') { | ||
return '%'; | ||
} | ||
if (i >= len) { | ||
return x; | ||
} | ||
switch (x) { | ||
case '%s': | ||
return String(args[i++]); | ||
case '%d': | ||
return Number(args[i++]); | ||
case '%j': | ||
try { | ||
return JSON.stringify(args[i++]); | ||
} catch (_) { | ||
return '[Circular]'; | ||
} | ||
break; | ||
default: | ||
if (typeof f === 'function') { | ||
return f.apply(null, args.slice(1)); | ||
} | ||
if (typeof f === 'string') { | ||
var str = String(f).replace(formatRegExp, function (x) { | ||
if (x === '%%') { | ||
return '%'; | ||
} | ||
if (i >= len) { | ||
return x; | ||
} | ||
switch (x) { | ||
case '%s': | ||
return String(args[i++]); | ||
case '%d': | ||
return Number(args[i++]); | ||
case '%j': | ||
try { | ||
return JSON.stringify(args[i++]); | ||
} catch (_) { | ||
return '[Circular]'; | ||
} | ||
break; | ||
default: | ||
return x; | ||
} | ||
}); | ||
for (var arg = args[i]; i < len; arg = args[++i]) { | ||
str += ' ' + arg; | ||
} | ||
}); | ||
for (var arg = args[i]; i < len; arg = args[++i]) { | ||
str += ' ' + arg; | ||
return str; | ||
} | ||
return str; | ||
return f; | ||
} | ||
@@ -151,9 +157,11 @@ | ||
return function (oe) { | ||
var e = oe; | ||
if (!e.message) { | ||
e = new Error(e); | ||
if (oe && oe.message) { | ||
oe.field = oe.field || rule.fullField; | ||
return oe; | ||
} | ||
e.field = e.field || rule.fullField; | ||
return e; | ||
return { | ||
message: oe, | ||
field: oe.field || rule.fullField | ||
}; | ||
}; | ||
} |
{ | ||
"name": "async-validator", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"description": "validate form asynchronous", | ||
@@ -46,3 +46,6 @@ "keywords": [ | ||
"lint" | ||
] | ||
} | ||
], | ||
"dependencies": { | ||
"lodash.mergewith": "^4.0.3" | ||
} | ||
} |
@@ -84,3 +84,3 @@ # async-validator | ||
* `options`: Additional options. | ||
* `options.messages`: The object containing validation error messages | ||
* `options.messages`: The object containing validation error messages, will be deep merged with defaultMessages. | ||
@@ -279,24 +279,21 @@ 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`. | ||
Potentially you may require the same schema validation rules for different languages, in which case duplicating the schema rules for each language does not make sense. | ||
Message can be any type, such as jsx format. | ||
In this scenario you could just require your own messages file for the language and assign it to the schema: | ||
```javascript | ||
var schema = require('async-validator'); | ||
var es = require('messages-es'); | ||
var descriptor = {name:{type: "string", required: true}}; | ||
var validator = new schema(descriptor); | ||
validator.messages(es); | ||
... | ||
{name:{type: "string", required: true, message: <b>Name is required</b>}} | ||
``` | ||
Or you could clone a default messages instance and then assign language specific messages to the schema using the `messages` method. | ||
Potentially you may require the same schema validation rules for different languages, in which case duplicating the schema rules for each language does not make sense. | ||
In this scenario you could just provide your own messages for the language and assign it to the schema: | ||
```javascript | ||
var schema = require('async-validator'); | ||
var es = schema.messages.clone(); | ||
es.required = "%s es un campo obligatorio"; // change the message | ||
var cn = { | ||
required: '%s 必填', | ||
}; | ||
var descriptor = {name:{type: "string", required: true}}; | ||
var validator = new schema(descriptor); | ||
validator.messages(es); // ensure this schema uses the altered messages | ||
// deep merge with defaultMessages | ||
validator.messages(cn); | ||
... | ||
@@ -303,0 +300,0 @@ ``` |
52526
1130
1
326
+ Addedlodash.mergewith@^4.0.3
+ Addedlodash.mergewith@4.6.2(transitive)