async-validator
Advanced tools
Comparing version
@@ -140,17 +140,16 @@ 'use strict'; | ||
} | ||
function format() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
function format(template) { | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
var i = 1; | ||
var f = args[0]; | ||
var i = 0; | ||
var len = args.length; | ||
if (typeof f === 'function') { | ||
return f.apply(null, args.slice(1)); | ||
if (typeof template === 'function') { | ||
return template.apply(null, args); | ||
} | ||
if (typeof f === 'string') { | ||
var str = String(f).replace(formatRegExp, function (x) { | ||
if (typeof template === 'string') { | ||
var str = template.replace(formatRegExp, function (x) { | ||
if (x === '%%') { | ||
@@ -187,3 +186,3 @@ return '%'; | ||
return f; | ||
return template; | ||
} | ||
@@ -217,3 +216,3 @@ | ||
function count(errors) { | ||
results.push.apply(results, errors); | ||
results.push.apply(results, errors || []); | ||
total++; | ||
@@ -257,3 +256,3 @@ | ||
Object.keys(objArr).forEach(function (k) { | ||
ret.push.apply(ret, objArr[k]); | ||
ret.push.apply(ret, objArr[k] || []); | ||
}); | ||
@@ -277,3 +276,3 @@ return ret; | ||
}( /*#__PURE__*/_wrapNativeSuper(Error)); | ||
function asyncMap(objArr, option, func, callback) { | ||
function asyncMap(objArr, option, func, callback, source) { | ||
if (option.first) { | ||
@@ -283,3 +282,3 @@ var _pending = new Promise(function (resolve, reject) { | ||
callback(errors); | ||
return errors.length ? reject(new AsyncValidationError(errors, convertFieldsError(errors))) : resolve(); | ||
return errors.length ? reject(new AsyncValidationError(errors, convertFieldsError(errors))) : resolve(source); | ||
}; | ||
@@ -298,8 +297,3 @@ | ||
var firstFields = option.firstFields || []; | ||
if (firstFields === true) { | ||
firstFields = Object.keys(objArr); | ||
} | ||
var firstFields = option.firstFields === true ? Object.keys(objArr) : option.firstFields || []; | ||
var objArrKeys = Object.keys(objArr); | ||
@@ -316,3 +310,3 @@ var objArrLength = objArrKeys.length; | ||
callback(results); | ||
return results.length ? reject(new AsyncValidationError(results, convertFieldsError(results))) : resolve(); | ||
return results.length ? reject(new AsyncValidationError(results, convertFieldsError(results))) : resolve(source); | ||
} | ||
@@ -323,3 +317,3 @@ }; | ||
callback(results); | ||
resolve(); | ||
resolve(source); | ||
} | ||
@@ -342,6 +336,34 @@ | ||
} | ||
function complementError(rule) { | ||
function isErrorObj(obj) { | ||
return !!(obj && obj.message); | ||
} | ||
function getValue(value, path) { | ||
var v = value; | ||
for (var i = 0; i < path.length; i++) { | ||
if (v == undefined) { | ||
return v; | ||
} | ||
v = v[path[i]]; | ||
} | ||
return v; | ||
} | ||
function complementError(rule, source) { | ||
return function (oe) { | ||
if (oe && oe.message) { | ||
var fieldValue; | ||
if (rule.fullFields) { | ||
fieldValue = getValue(source, rule.fullFields); | ||
} else { | ||
fieldValue = source[oe.field || rule.fullField]; | ||
} | ||
if (isErrorObj(oe)) { | ||
oe.field = oe.field || rule.fullField; | ||
oe.fieldValue = fieldValue; | ||
return oe; | ||
@@ -352,2 +374,3 @@ } | ||
message: typeof oe === 'function' ? oe() : oe, | ||
fieldValue: fieldValue, | ||
field: oe.field || rule.fullField | ||
@@ -375,19 +398,7 @@ }; | ||
/** | ||
* Rule for validating required fields. | ||
* | ||
* @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. | ||
*/ | ||
function required(rule, value, source, errors, options, type) { | ||
var required$1 = function required(rule, value, source, errors, options, type) { | ||
if (rule.required && (!source.hasOwnProperty(rule.field) || isEmptyValue(value, type || rule.type))) { | ||
errors.push(format(options.messages.required, rule.fullField)); | ||
} | ||
} | ||
}; | ||
@@ -406,11 +417,11 @@ /** | ||
function whitespace(rule, value, source, errors, options) { | ||
var whitespace = function whitespace(rule, value, source, errors, options) { | ||
if (/^\s+$/.test(value) || value === '') { | ||
errors.push(format(options.messages.whitespace, rule.fullField)); | ||
} | ||
} | ||
}; | ||
/* eslint max-len:0 */ | ||
var pattern = { | ||
var pattern$2 = { | ||
// http://emailregex.com/ | ||
@@ -459,26 +470,15 @@ email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, | ||
email: function email(value) { | ||
return typeof value === 'string' && !!value.match(pattern.email) && value.length < 255; | ||
return typeof value === 'string' && !!value.match(pattern$2.email) && value.length < 255; | ||
}, | ||
url: function url(value) { | ||
return typeof value === 'string' && !!value.match(pattern.url); | ||
return typeof value === 'string' && !!value.match(pattern$2.url); | ||
}, | ||
hex: function hex(value) { | ||
return typeof value === 'string' && !!value.match(pattern.hex); | ||
return typeof value === 'string' && !!value.match(pattern$2.hex); | ||
} | ||
}; | ||
/** | ||
* 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. | ||
*/ | ||
function type(rule, value, source, errors, options) { | ||
var type$1 = function type(rule, value, source, errors, options) { | ||
if (rule.required && value === undefined) { | ||
required(rule, value, source, errors, options); | ||
required$1(rule, value, source, errors, options); | ||
return; | ||
@@ -498,17 +498,5 @@ } | ||
} | ||
} | ||
}; | ||
/** | ||
* Rule for validating minimum and maximum allowed values. | ||
* | ||
* @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. | ||
*/ | ||
function range(rule, value, source, errors, options) { | ||
var range = function range(rule, value, source, errors, options) { | ||
var len = typeof rule.len === 'number'; | ||
@@ -560,38 +548,15 @@ var min = typeof rule.min === 'number'; | ||
} | ||
} | ||
}; | ||
var ENUM = 'enum'; | ||
/** | ||
* Rule for validating a value exists in an enumerable list. | ||
* | ||
* @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. | ||
*/ | ||
var ENUM$1 = 'enum'; | ||
function enumerable(rule, value, source, errors, options) { | ||
rule[ENUM] = Array.isArray(rule[ENUM]) ? rule[ENUM] : []; | ||
var enumerable$1 = function enumerable(rule, value, source, errors, options) { | ||
rule[ENUM$1] = Array.isArray(rule[ENUM$1]) ? rule[ENUM$1] : []; | ||
if (rule[ENUM].indexOf(value) === -1) { | ||
errors.push(format(options.messages[ENUM], rule.fullField, rule[ENUM].join(', '))); | ||
if (rule[ENUM$1].indexOf(value) === -1) { | ||
errors.push(format(options.messages[ENUM$1], rule.fullField, rule[ENUM$1].join(', '))); | ||
} | ||
} | ||
}; | ||
/** | ||
* Rule for validating a regular expression pattern. | ||
* | ||
* @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. | ||
*/ | ||
function pattern$1(rule, value, source, errors, options) { | ||
var pattern$1 = function pattern(rule, value, source, errors, options) { | ||
if (rule.pattern) { | ||
@@ -615,25 +580,14 @@ if (rule.pattern instanceof RegExp) { | ||
} | ||
} | ||
}; | ||
var rules = { | ||
required: required, | ||
required: required$1, | ||
whitespace: whitespace, | ||
type: type, | ||
type: type$1, | ||
range: range, | ||
"enum": enumerable, | ||
"enum": enumerable$1, | ||
pattern: pattern$1 | ||
}; | ||
/** | ||
* Performs validation for string types. | ||
* | ||
* @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. | ||
*/ | ||
function string(rule, value, callback, source, options) { | ||
var string = function string(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -661,16 +615,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates a function. | ||
* | ||
* @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. | ||
*/ | ||
function method(rule, value, callback, source, options) { | ||
var method = function method(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -692,16 +635,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates a number. | ||
* | ||
* @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. | ||
*/ | ||
function number(rule, value, callback, source, options) { | ||
var number = function number(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -728,16 +660,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates a boolean. | ||
* | ||
* @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. | ||
*/ | ||
function _boolean(rule, value, callback, source, options) { | ||
var _boolean = function _boolean(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -759,16 +680,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates the regular expression type. | ||
* | ||
* @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. | ||
*/ | ||
function regexp(rule, value, callback, source, options) { | ||
var regexp = function regexp(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -790,16 +700,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates a number is an integer. | ||
* | ||
* @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. | ||
*/ | ||
function integer(rule, value, callback, source, options) { | ||
var integer = function integer(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -822,16 +721,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates a number is a floating point number. | ||
* | ||
* @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. | ||
*/ | ||
function floatFn(rule, value, callback, source, options) { | ||
var floatFn = function floatFn(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -854,16 +742,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates an array. | ||
* | ||
* @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. | ||
*/ | ||
function array(rule, value, callback, source, options) { | ||
var array = function array(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -886,16 +763,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates an object. | ||
* | ||
* @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. | ||
*/ | ||
function object(rule, value, callback, source, options) { | ||
var object = function object(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -917,17 +783,7 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
var ENUM$1 = 'enum'; | ||
/** | ||
* Validates an enumerable list. | ||
* | ||
* @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. | ||
*/ | ||
var ENUM = 'enum'; | ||
function enumerable$1(rule, value, callback, source, options) { | ||
var enumerable = function enumerable(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -944,3 +800,3 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
if (value !== undefined) { | ||
rules[ENUM$1](rule, value, source, errors, options); | ||
rules[ENUM](rule, value, source, errors, options); | ||
} | ||
@@ -950,19 +806,5 @@ } | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates a regular expression pattern. | ||
* | ||
* Performs validation when a rule only contains | ||
* a pattern property but is not declared as a string type. | ||
* | ||
* @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. | ||
*/ | ||
function pattern$2(rule, value, callback, source, options) { | ||
var pattern = function pattern(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -984,5 +826,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
function date(rule, value, callback, source, options) { | ||
var date = function date(rule, value, callback, source, options) { | ||
// console.log('integer rule called %j', rule); | ||
@@ -1017,5 +859,5 @@ var errors = []; | ||
callback(errors); | ||
} | ||
}; | ||
function required$1(rule, value, callback, source, options) { | ||
var required = function required(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -1025,5 +867,5 @@ var type = Array.isArray(value) ? 'array' : typeof value; | ||
callback(errors); | ||
} | ||
}; | ||
function type$1(rule, value, callback, source, options) { | ||
var type = function type(rule, value, callback, source, options) { | ||
var ruleType = rule.type; | ||
@@ -1046,16 +888,5 @@ var errors = []; | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Performs validation for any type. | ||
* | ||
* @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. | ||
*/ | ||
function any(rule, value, callback, source, options) { | ||
var any = function any(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -1073,3 +904,3 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
@@ -1086,9 +917,9 @@ var validators = { | ||
object: object, | ||
"enum": enumerable$1, | ||
pattern: pattern$2, | ||
"enum": enumerable, | ||
pattern: pattern, | ||
date: date, | ||
url: type$1, | ||
hex: type$1, | ||
email: type$1, | ||
required: required$1, | ||
url: type, | ||
hex: type, | ||
email: type, | ||
required: required, | ||
any: any | ||
@@ -1160,17 +991,16 @@ }; | ||
function Schema(descriptor) { | ||
this.rules = null; | ||
this._messages = messages; | ||
this.define(descriptor); | ||
} | ||
var Schema = /*#__PURE__*/function () { | ||
// ========================= Static ========================= | ||
// ======================== Instance ======================== | ||
function Schema(descriptor) { | ||
this.rules = null; | ||
this._messages = messages; | ||
this.define(descriptor); | ||
} | ||
Schema.prototype = { | ||
messages: function messages(_messages) { | ||
if (_messages) { | ||
this._messages = deepMerge(newMessages(), _messages); | ||
} | ||
var _proto = Schema.prototype; | ||
return this._messages; | ||
}, | ||
define: function define(rules) { | ||
_proto.define = function define(rules) { | ||
var _this = this; | ||
if (!rules) { | ||
@@ -1185,15 +1015,19 @@ throw new Error('Cannot configure a schema with no rules'); | ||
this.rules = {}; | ||
var z; | ||
var item; | ||
Object.keys(rules).forEach(function (name) { | ||
var item = rules[name]; | ||
_this.rules[name] = Array.isArray(item) ? item : [item]; | ||
}); | ||
}; | ||
for (z in rules) { | ||
if (rules.hasOwnProperty(z)) { | ||
item = rules[z]; | ||
this.rules[z] = Array.isArray(item) ? item : [item]; | ||
} | ||
_proto.messages = function messages(_messages) { | ||
if (_messages) { | ||
this._messages = deepMerge(newMessages(), _messages); | ||
} | ||
}, | ||
validate: function validate(source_, o, oc) { | ||
var _this = this; | ||
return this._messages; | ||
}; | ||
_proto.validate = function validate(source_, o, oc) { | ||
var _this2 = this; | ||
if (o === void 0) { | ||
@@ -1218,10 +1052,9 @@ o = {}; | ||
if (callback) { | ||
callback(); | ||
callback(null, source); | ||
} | ||
return Promise.resolve(); | ||
return Promise.resolve(source); | ||
} | ||
function complete(results) { | ||
var i; | ||
var errors = []; | ||
@@ -1240,3 +1073,3 @@ var fields = {}; | ||
for (i = 0; i < results.length; i++) { | ||
for (var i = 0; i < results.length; i++) { | ||
add(results[i]); | ||
@@ -1246,9 +1079,7 @@ } | ||
if (!errors.length) { | ||
errors = null; | ||
fields = null; | ||
callback(null, source); | ||
} else { | ||
fields = convertFieldsError(errors); | ||
callback(errors, fields); | ||
} | ||
callback(errors, fields); | ||
} | ||
@@ -1269,9 +1100,7 @@ | ||
var arr; | ||
var value; | ||
var series = {}; | ||
var keys = options.keys || Object.keys(this.rules); | ||
keys.forEach(function (z) { | ||
arr = _this.rules[z]; | ||
value = source[z]; | ||
var arr = _this2.rules[z]; | ||
var value = source[z]; | ||
arr.forEach(function (r) { | ||
@@ -1294,9 +1123,7 @@ var rule = r; | ||
rule = _extends({}, rule); | ||
} | ||
} // Fill validator. Skip if nothing need to validate | ||
rule.validator = _this.getValidationMethod(rule); | ||
rule.field = z; | ||
rule.fullField = rule.fullField || z; | ||
rule.type = _this.getType(rule); | ||
rule.validator = _this2.getValidationMethod(rule); | ||
if (!rule.validator) { | ||
@@ -1306,2 +1133,5 @@ return; | ||
rule.field = z; | ||
rule.fullField = rule.fullField || z; | ||
rule.type = _this2.getType(rule); | ||
series[z] = series[z] || []; | ||
@@ -1323,5 +1153,6 @@ series[z].push({ | ||
function addFullfield(key, schema) { | ||
function addFullField(key, schema) { | ||
return _extends({}, schema, { | ||
fullField: rule.fullField + "." + key | ||
fullField: rule.fullField + "." + key, | ||
fullFields: rule.fullFields ? [].concat(rule.fullFields, [key]) : [key] | ||
}); | ||
@@ -1335,25 +1166,22 @@ } | ||
var errors = e; | ||
var errorList = Array.isArray(e) ? e : [e]; | ||
if (!Array.isArray(errors)) { | ||
errors = [errors]; | ||
if (!options.suppressWarning && errorList.length) { | ||
Schema.warning('async-validator:', errorList); | ||
} | ||
if (!options.suppressWarning && errors.length) { | ||
Schema.warning('async-validator:', errors); | ||
} | ||
if (errorList.length && rule.message !== undefined) { | ||
errorList = [].concat(rule.message); | ||
} // Fill error info | ||
if (errors.length && rule.message !== undefined) { | ||
errors = [].concat(rule.message); | ||
} | ||
errors = errors.map(complementError(rule)); | ||
var filledErrors = errorList.map(complementError(rule, source)); | ||
if (options.first && errors.length) { | ||
if (options.first && filledErrors.length) { | ||
errorFields[rule.field] = 1; | ||
return doIt(errors); | ||
return doIt(filledErrors); | ||
} | ||
if (!deep) { | ||
doIt(errors); | ||
doIt(filledErrors); | ||
} else { | ||
@@ -1365,8 +1193,8 @@ // if rule is required but the target object | ||
if (rule.message !== undefined) { | ||
errors = [].concat(rule.message).map(complementError(rule)); | ||
filledErrors = [].concat(rule.message).map(complementError(rule, source)); | ||
} else if (options.error) { | ||
errors = [options.error(rule, format(options.messages.required, rule.field))]; | ||
filledErrors = [options.error(rule, format(options.messages.required, rule.field))]; | ||
} | ||
return doIt(errors); | ||
return doIt(filledErrors); | ||
} | ||
@@ -1377,19 +1205,15 @@ | ||
if (rule.defaultField) { | ||
for (var k in data.value) { | ||
if (data.value.hasOwnProperty(k)) { | ||
fieldsSchema[k] = rule.defaultField; | ||
} | ||
} | ||
Object.keys(data.value).map(function (key) { | ||
fieldsSchema[key] = rule.defaultField; | ||
}); | ||
} | ||
fieldsSchema = _extends({}, fieldsSchema, data.rule.fields); | ||
for (var f in fieldsSchema) { | ||
if (fieldsSchema.hasOwnProperty(f)) { | ||
var fieldSchema = Array.isArray(fieldsSchema[f]) ? fieldsSchema[f] : [fieldsSchema[f]]; | ||
fieldsSchema[f] = fieldSchema.map(addFullfield.bind(null, f)); | ||
} | ||
} | ||
var schema = new Schema(fieldsSchema); | ||
var paredFieldsSchema = {}; | ||
Object.keys(fieldsSchema).forEach(function (field) { | ||
var fieldSchema = fieldsSchema[field]; | ||
var fieldSchemaList = Array.isArray(fieldSchema) ? fieldSchema : [fieldSchema]; | ||
paredFieldsSchema[field] = fieldSchemaList.map(addFullField.bind(null, field)); | ||
}); | ||
var schema = new Schema(paredFieldsSchema); | ||
schema.messages(options.messages); | ||
@@ -1405,4 +1229,4 @@ | ||
if (errors && errors.length) { | ||
finalErrors.push.apply(finalErrors, errors); | ||
if (filledErrors && filledErrors.length) { | ||
finalErrors.push.apply(finalErrors, filledErrors); | ||
} | ||
@@ -1429,3 +1253,3 @@ | ||
} else if (res === false) { | ||
cb(rule.message || rule.field + " fails"); | ||
cb(typeof rule.message === 'function' ? rule.message(rule.fullField || rule.field) : rule.message || (rule.fullField || rule.field) + " fails"); | ||
} else if (res instanceof Array) { | ||
@@ -1447,5 +1271,6 @@ cb(res); | ||
complete(results); | ||
}); | ||
}, | ||
getType: function getType(rule) { | ||
}, source); | ||
}; | ||
_proto.getType = function getType(rule) { | ||
if (rule.type === undefined && rule.pattern instanceof RegExp) { | ||
@@ -1460,4 +1285,5 @@ rule.type = 'pattern'; | ||
return rule.type || 'string'; | ||
}, | ||
getValidationMethod: function getValidationMethod(rule) { | ||
}; | ||
_proto.getValidationMethod = function getValidationMethod(rule) { | ||
if (typeof rule.validator === 'function') { | ||
@@ -1478,6 +1304,8 @@ return rule.validator; | ||
return validators[this.getType(rule)] || false; | ||
} | ||
}; | ||
return validators[this.getType(rule)] || undefined; | ||
}; | ||
return Schema; | ||
}(); | ||
Schema.register = function register(type, validator) { | ||
@@ -1495,3 +1323,3 @@ if (typeof validator !== 'function') { | ||
exports.default = Schema; | ||
exports['default'] = Schema; | ||
//# sourceMappingURL=index.js.map |
@@ -136,17 +136,16 @@ function _extends() { | ||
} | ||
function format() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
function format(template) { | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
var i = 1; | ||
var f = args[0]; | ||
var i = 0; | ||
var len = args.length; | ||
if (typeof f === 'function') { | ||
return f.apply(null, args.slice(1)); | ||
if (typeof template === 'function') { | ||
return template.apply(null, args); | ||
} | ||
if (typeof f === 'string') { | ||
var str = String(f).replace(formatRegExp, function (x) { | ||
if (typeof template === 'string') { | ||
var str = template.replace(formatRegExp, function (x) { | ||
if (x === '%%') { | ||
@@ -183,3 +182,3 @@ return '%'; | ||
return f; | ||
return template; | ||
} | ||
@@ -213,3 +212,3 @@ | ||
function count(errors) { | ||
results.push.apply(results, errors); | ||
results.push.apply(results, errors || []); | ||
total++; | ||
@@ -253,3 +252,3 @@ | ||
Object.keys(objArr).forEach(function (k) { | ||
ret.push.apply(ret, objArr[k]); | ||
ret.push.apply(ret, objArr[k] || []); | ||
}); | ||
@@ -273,3 +272,3 @@ return ret; | ||
}( /*#__PURE__*/_wrapNativeSuper(Error)); | ||
function asyncMap(objArr, option, func, callback) { | ||
function asyncMap(objArr, option, func, callback, source) { | ||
if (option.first) { | ||
@@ -279,3 +278,3 @@ var _pending = new Promise(function (resolve, reject) { | ||
callback(errors); | ||
return errors.length ? reject(new AsyncValidationError(errors, convertFieldsError(errors))) : resolve(); | ||
return errors.length ? reject(new AsyncValidationError(errors, convertFieldsError(errors))) : resolve(source); | ||
}; | ||
@@ -294,8 +293,3 @@ | ||
var firstFields = option.firstFields || []; | ||
if (firstFields === true) { | ||
firstFields = Object.keys(objArr); | ||
} | ||
var firstFields = option.firstFields === true ? Object.keys(objArr) : option.firstFields || []; | ||
var objArrKeys = Object.keys(objArr); | ||
@@ -312,3 +306,3 @@ var objArrLength = objArrKeys.length; | ||
callback(results); | ||
return results.length ? reject(new AsyncValidationError(results, convertFieldsError(results))) : resolve(); | ||
return results.length ? reject(new AsyncValidationError(results, convertFieldsError(results))) : resolve(source); | ||
} | ||
@@ -319,3 +313,3 @@ }; | ||
callback(results); | ||
resolve(); | ||
resolve(source); | ||
} | ||
@@ -338,6 +332,34 @@ | ||
} | ||
function complementError(rule) { | ||
function isErrorObj(obj) { | ||
return !!(obj && obj.message); | ||
} | ||
function getValue(value, path) { | ||
var v = value; | ||
for (var i = 0; i < path.length; i++) { | ||
if (v == undefined) { | ||
return v; | ||
} | ||
v = v[path[i]]; | ||
} | ||
return v; | ||
} | ||
function complementError(rule, source) { | ||
return function (oe) { | ||
if (oe && oe.message) { | ||
var fieldValue; | ||
if (rule.fullFields) { | ||
fieldValue = getValue(source, rule.fullFields); | ||
} else { | ||
fieldValue = source[oe.field || rule.fullField]; | ||
} | ||
if (isErrorObj(oe)) { | ||
oe.field = oe.field || rule.fullField; | ||
oe.fieldValue = fieldValue; | ||
return oe; | ||
@@ -348,2 +370,3 @@ } | ||
message: typeof oe === 'function' ? oe() : oe, | ||
fieldValue: fieldValue, | ||
field: oe.field || rule.fullField | ||
@@ -371,19 +394,7 @@ }; | ||
/** | ||
* Rule for validating required fields. | ||
* | ||
* @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. | ||
*/ | ||
function required(rule, value, source, errors, options, type) { | ||
var required$1 = function required(rule, value, source, errors, options, type) { | ||
if (rule.required && (!source.hasOwnProperty(rule.field) || isEmptyValue(value, type || rule.type))) { | ||
errors.push(format(options.messages.required, rule.fullField)); | ||
} | ||
} | ||
}; | ||
@@ -402,11 +413,11 @@ /** | ||
function whitespace(rule, value, source, errors, options) { | ||
var whitespace = function whitespace(rule, value, source, errors, options) { | ||
if (/^\s+$/.test(value) || value === '') { | ||
errors.push(format(options.messages.whitespace, rule.fullField)); | ||
} | ||
} | ||
}; | ||
/* eslint max-len:0 */ | ||
var pattern = { | ||
var pattern$2 = { | ||
// http://emailregex.com/ | ||
@@ -455,26 +466,15 @@ email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, | ||
email: function email(value) { | ||
return typeof value === 'string' && !!value.match(pattern.email) && value.length < 255; | ||
return typeof value === 'string' && !!value.match(pattern$2.email) && value.length < 255; | ||
}, | ||
url: function url(value) { | ||
return typeof value === 'string' && !!value.match(pattern.url); | ||
return typeof value === 'string' && !!value.match(pattern$2.url); | ||
}, | ||
hex: function hex(value) { | ||
return typeof value === 'string' && !!value.match(pattern.hex); | ||
return typeof value === 'string' && !!value.match(pattern$2.hex); | ||
} | ||
}; | ||
/** | ||
* 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. | ||
*/ | ||
function type(rule, value, source, errors, options) { | ||
var type$1 = function type(rule, value, source, errors, options) { | ||
if (rule.required && value === undefined) { | ||
required(rule, value, source, errors, options); | ||
required$1(rule, value, source, errors, options); | ||
return; | ||
@@ -494,17 +494,5 @@ } | ||
} | ||
} | ||
}; | ||
/** | ||
* Rule for validating minimum and maximum allowed values. | ||
* | ||
* @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. | ||
*/ | ||
function range(rule, value, source, errors, options) { | ||
var range = function range(rule, value, source, errors, options) { | ||
var len = typeof rule.len === 'number'; | ||
@@ -556,38 +544,15 @@ var min = typeof rule.min === 'number'; | ||
} | ||
} | ||
}; | ||
var ENUM = 'enum'; | ||
/** | ||
* Rule for validating a value exists in an enumerable list. | ||
* | ||
* @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. | ||
*/ | ||
var ENUM$1 = 'enum'; | ||
function enumerable(rule, value, source, errors, options) { | ||
rule[ENUM] = Array.isArray(rule[ENUM]) ? rule[ENUM] : []; | ||
var enumerable$1 = function enumerable(rule, value, source, errors, options) { | ||
rule[ENUM$1] = Array.isArray(rule[ENUM$1]) ? rule[ENUM$1] : []; | ||
if (rule[ENUM].indexOf(value) === -1) { | ||
errors.push(format(options.messages[ENUM], rule.fullField, rule[ENUM].join(', '))); | ||
if (rule[ENUM$1].indexOf(value) === -1) { | ||
errors.push(format(options.messages[ENUM$1], rule.fullField, rule[ENUM$1].join(', '))); | ||
} | ||
} | ||
}; | ||
/** | ||
* Rule for validating a regular expression pattern. | ||
* | ||
* @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. | ||
*/ | ||
function pattern$1(rule, value, source, errors, options) { | ||
var pattern$1 = function pattern(rule, value, source, errors, options) { | ||
if (rule.pattern) { | ||
@@ -611,25 +576,14 @@ if (rule.pattern instanceof RegExp) { | ||
} | ||
} | ||
}; | ||
var rules = { | ||
required: required, | ||
required: required$1, | ||
whitespace: whitespace, | ||
type: type, | ||
type: type$1, | ||
range: range, | ||
"enum": enumerable, | ||
"enum": enumerable$1, | ||
pattern: pattern$1 | ||
}; | ||
/** | ||
* Performs validation for string types. | ||
* | ||
* @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. | ||
*/ | ||
function string(rule, value, callback, source, options) { | ||
var string = function string(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -657,16 +611,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates a function. | ||
* | ||
* @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. | ||
*/ | ||
function method(rule, value, callback, source, options) { | ||
var method = function method(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -688,16 +631,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates a number. | ||
* | ||
* @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. | ||
*/ | ||
function number(rule, value, callback, source, options) { | ||
var number = function number(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -724,16 +656,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates a boolean. | ||
* | ||
* @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. | ||
*/ | ||
function _boolean(rule, value, callback, source, options) { | ||
var _boolean = function _boolean(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -755,16 +676,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates the regular expression type. | ||
* | ||
* @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. | ||
*/ | ||
function regexp(rule, value, callback, source, options) { | ||
var regexp = function regexp(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -786,16 +696,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates a number is an integer. | ||
* | ||
* @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. | ||
*/ | ||
function integer(rule, value, callback, source, options) { | ||
var integer = function integer(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -818,16 +717,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates a number is a floating point number. | ||
* | ||
* @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. | ||
*/ | ||
function floatFn(rule, value, callback, source, options) { | ||
var floatFn = function floatFn(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -850,16 +738,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates an array. | ||
* | ||
* @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. | ||
*/ | ||
function array(rule, value, callback, source, options) { | ||
var array = function array(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -882,16 +759,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates an object. | ||
* | ||
* @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. | ||
*/ | ||
function object(rule, value, callback, source, options) { | ||
var object = function object(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -913,17 +779,7 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
var ENUM$1 = 'enum'; | ||
/** | ||
* Validates an enumerable list. | ||
* | ||
* @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. | ||
*/ | ||
var ENUM = 'enum'; | ||
function enumerable$1(rule, value, callback, source, options) { | ||
var enumerable = function enumerable(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -940,3 +796,3 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
if (value !== undefined) { | ||
rules[ENUM$1](rule, value, source, errors, options); | ||
rules[ENUM](rule, value, source, errors, options); | ||
} | ||
@@ -946,19 +802,5 @@ } | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Validates a regular expression pattern. | ||
* | ||
* Performs validation when a rule only contains | ||
* a pattern property but is not declared as a string type. | ||
* | ||
* @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. | ||
*/ | ||
function pattern$2(rule, value, callback, source, options) { | ||
var pattern = function pattern(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -980,5 +822,5 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
function date(rule, value, callback, source, options) { | ||
var date = function date(rule, value, callback, source, options) { | ||
// console.log('integer rule called %j', rule); | ||
@@ -1013,5 +855,5 @@ var errors = []; | ||
callback(errors); | ||
} | ||
}; | ||
function required$1(rule, value, callback, source, options) { | ||
var required = function required(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -1021,5 +863,5 @@ var type = Array.isArray(value) ? 'array' : typeof value; | ||
callback(errors); | ||
} | ||
}; | ||
function type$1(rule, value, callback, source, options) { | ||
var type = function type(rule, value, callback, source, options) { | ||
var ruleType = rule.type; | ||
@@ -1042,16 +884,5 @@ var errors = []; | ||
callback(errors); | ||
} | ||
}; | ||
/** | ||
* Performs validation for any type. | ||
* | ||
* @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. | ||
*/ | ||
function any(rule, value, callback, source, options) { | ||
var any = function any(rule, value, callback, source, options) { | ||
var errors = []; | ||
@@ -1069,3 +900,3 @@ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
callback(errors); | ||
} | ||
}; | ||
@@ -1082,9 +913,9 @@ var validators = { | ||
object: object, | ||
"enum": enumerable$1, | ||
pattern: pattern$2, | ||
"enum": enumerable, | ||
pattern: pattern, | ||
date: date, | ||
url: type$1, | ||
hex: type$1, | ||
email: type$1, | ||
required: required$1, | ||
url: type, | ||
hex: type, | ||
email: type, | ||
required: required, | ||
any: any | ||
@@ -1156,17 +987,16 @@ }; | ||
function Schema(descriptor) { | ||
this.rules = null; | ||
this._messages = messages; | ||
this.define(descriptor); | ||
} | ||
var Schema = /*#__PURE__*/function () { | ||
// ========================= Static ========================= | ||
// ======================== Instance ======================== | ||
function Schema(descriptor) { | ||
this.rules = null; | ||
this._messages = messages; | ||
this.define(descriptor); | ||
} | ||
Schema.prototype = { | ||
messages: function messages(_messages) { | ||
if (_messages) { | ||
this._messages = deepMerge(newMessages(), _messages); | ||
} | ||
var _proto = Schema.prototype; | ||
return this._messages; | ||
}, | ||
define: function define(rules) { | ||
_proto.define = function define(rules) { | ||
var _this = this; | ||
if (!rules) { | ||
@@ -1181,15 +1011,19 @@ throw new Error('Cannot configure a schema with no rules'); | ||
this.rules = {}; | ||
var z; | ||
var item; | ||
Object.keys(rules).forEach(function (name) { | ||
var item = rules[name]; | ||
_this.rules[name] = Array.isArray(item) ? item : [item]; | ||
}); | ||
}; | ||
for (z in rules) { | ||
if (rules.hasOwnProperty(z)) { | ||
item = rules[z]; | ||
this.rules[z] = Array.isArray(item) ? item : [item]; | ||
} | ||
_proto.messages = function messages(_messages) { | ||
if (_messages) { | ||
this._messages = deepMerge(newMessages(), _messages); | ||
} | ||
}, | ||
validate: function validate(source_, o, oc) { | ||
var _this = this; | ||
return this._messages; | ||
}; | ||
_proto.validate = function validate(source_, o, oc) { | ||
var _this2 = this; | ||
if (o === void 0) { | ||
@@ -1214,10 +1048,9 @@ o = {}; | ||
if (callback) { | ||
callback(); | ||
callback(null, source); | ||
} | ||
return Promise.resolve(); | ||
return Promise.resolve(source); | ||
} | ||
function complete(results) { | ||
var i; | ||
var errors = []; | ||
@@ -1236,3 +1069,3 @@ var fields = {}; | ||
for (i = 0; i < results.length; i++) { | ||
for (var i = 0; i < results.length; i++) { | ||
add(results[i]); | ||
@@ -1242,9 +1075,7 @@ } | ||
if (!errors.length) { | ||
errors = null; | ||
fields = null; | ||
callback(null, source); | ||
} else { | ||
fields = convertFieldsError(errors); | ||
callback(errors, fields); | ||
} | ||
callback(errors, fields); | ||
} | ||
@@ -1265,9 +1096,7 @@ | ||
var arr; | ||
var value; | ||
var series = {}; | ||
var keys = options.keys || Object.keys(this.rules); | ||
keys.forEach(function (z) { | ||
arr = _this.rules[z]; | ||
value = source[z]; | ||
var arr = _this2.rules[z]; | ||
var value = source[z]; | ||
arr.forEach(function (r) { | ||
@@ -1290,9 +1119,7 @@ var rule = r; | ||
rule = _extends({}, rule); | ||
} | ||
} // Fill validator. Skip if nothing need to validate | ||
rule.validator = _this.getValidationMethod(rule); | ||
rule.field = z; | ||
rule.fullField = rule.fullField || z; | ||
rule.type = _this.getType(rule); | ||
rule.validator = _this2.getValidationMethod(rule); | ||
if (!rule.validator) { | ||
@@ -1302,2 +1129,5 @@ return; | ||
rule.field = z; | ||
rule.fullField = rule.fullField || z; | ||
rule.type = _this2.getType(rule); | ||
series[z] = series[z] || []; | ||
@@ -1319,5 +1149,6 @@ series[z].push({ | ||
function addFullfield(key, schema) { | ||
function addFullField(key, schema) { | ||
return _extends({}, schema, { | ||
fullField: rule.fullField + "." + key | ||
fullField: rule.fullField + "." + key, | ||
fullFields: rule.fullFields ? [].concat(rule.fullFields, [key]) : [key] | ||
}); | ||
@@ -1331,25 +1162,22 @@ } | ||
var errors = e; | ||
var errorList = Array.isArray(e) ? e : [e]; | ||
if (!Array.isArray(errors)) { | ||
errors = [errors]; | ||
if (!options.suppressWarning && errorList.length) { | ||
Schema.warning('async-validator:', errorList); | ||
} | ||
if (!options.suppressWarning && errors.length) { | ||
Schema.warning('async-validator:', errors); | ||
} | ||
if (errorList.length && rule.message !== undefined) { | ||
errorList = [].concat(rule.message); | ||
} // Fill error info | ||
if (errors.length && rule.message !== undefined) { | ||
errors = [].concat(rule.message); | ||
} | ||
errors = errors.map(complementError(rule)); | ||
var filledErrors = errorList.map(complementError(rule, source)); | ||
if (options.first && errors.length) { | ||
if (options.first && filledErrors.length) { | ||
errorFields[rule.field] = 1; | ||
return doIt(errors); | ||
return doIt(filledErrors); | ||
} | ||
if (!deep) { | ||
doIt(errors); | ||
doIt(filledErrors); | ||
} else { | ||
@@ -1361,8 +1189,8 @@ // if rule is required but the target object | ||
if (rule.message !== undefined) { | ||
errors = [].concat(rule.message).map(complementError(rule)); | ||
filledErrors = [].concat(rule.message).map(complementError(rule, source)); | ||
} else if (options.error) { | ||
errors = [options.error(rule, format(options.messages.required, rule.field))]; | ||
filledErrors = [options.error(rule, format(options.messages.required, rule.field))]; | ||
} | ||
return doIt(errors); | ||
return doIt(filledErrors); | ||
} | ||
@@ -1373,19 +1201,15 @@ | ||
if (rule.defaultField) { | ||
for (var k in data.value) { | ||
if (data.value.hasOwnProperty(k)) { | ||
fieldsSchema[k] = rule.defaultField; | ||
} | ||
} | ||
Object.keys(data.value).map(function (key) { | ||
fieldsSchema[key] = rule.defaultField; | ||
}); | ||
} | ||
fieldsSchema = _extends({}, fieldsSchema, data.rule.fields); | ||
for (var f in fieldsSchema) { | ||
if (fieldsSchema.hasOwnProperty(f)) { | ||
var fieldSchema = Array.isArray(fieldsSchema[f]) ? fieldsSchema[f] : [fieldsSchema[f]]; | ||
fieldsSchema[f] = fieldSchema.map(addFullfield.bind(null, f)); | ||
} | ||
} | ||
var schema = new Schema(fieldsSchema); | ||
var paredFieldsSchema = {}; | ||
Object.keys(fieldsSchema).forEach(function (field) { | ||
var fieldSchema = fieldsSchema[field]; | ||
var fieldSchemaList = Array.isArray(fieldSchema) ? fieldSchema : [fieldSchema]; | ||
paredFieldsSchema[field] = fieldSchemaList.map(addFullField.bind(null, field)); | ||
}); | ||
var schema = new Schema(paredFieldsSchema); | ||
schema.messages(options.messages); | ||
@@ -1401,4 +1225,4 @@ | ||
if (errors && errors.length) { | ||
finalErrors.push.apply(finalErrors, errors); | ||
if (filledErrors && filledErrors.length) { | ||
finalErrors.push.apply(finalErrors, filledErrors); | ||
} | ||
@@ -1425,3 +1249,3 @@ | ||
} else if (res === false) { | ||
cb(rule.message || rule.field + " fails"); | ||
cb(typeof rule.message === 'function' ? rule.message(rule.fullField || rule.field) : rule.message || (rule.fullField || rule.field) + " fails"); | ||
} else if (res instanceof Array) { | ||
@@ -1443,5 +1267,6 @@ cb(res); | ||
complete(results); | ||
}); | ||
}, | ||
getType: function getType(rule) { | ||
}, source); | ||
}; | ||
_proto.getType = function getType(rule) { | ||
if (rule.type === undefined && rule.pattern instanceof RegExp) { | ||
@@ -1456,4 +1281,5 @@ rule.type = 'pattern'; | ||
return rule.type || 'string'; | ||
}, | ||
getValidationMethod: function getValidationMethod(rule) { | ||
}; | ||
_proto.getValidationMethod = function getValidationMethod(rule) { | ||
if (typeof rule.validator === 'function') { | ||
@@ -1474,6 +1300,8 @@ return rule.validator; | ||
return validators[this.getType(rule)] || false; | ||
} | ||
}; | ||
return validators[this.getType(rule)] || undefined; | ||
}; | ||
return Schema; | ||
}(); | ||
Schema.register = function register(type, validator) { | ||
@@ -1491,3 +1319,3 @@ if (typeof validator !== 'function') { | ||
export default Schema; | ||
export { Schema as default }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "async-validator", | ||
"description": "validate form asynchronous", | ||
"version": "3.5.2", | ||
"version": "4.0.0", | ||
"license": "MIT", | ||
@@ -27,20 +27,22 @@ "files": [ | ||
"devDependencies": { | ||
"@babel/core": "^7.15.0", | ||
"@babel/node": "^7.14.9", | ||
"@babel/preset-env": "^7.8.7", | ||
"@babel/preset-typescript": "^7.13.0", | ||
"@pika/pack": "^0.5.0", | ||
"@pika/plugin-build-types": "^0.6.0", | ||
"@pika/plugin-standard-pkg": "^0.6.0", | ||
"@pika/types": "^0.6.0", | ||
"babel-jest": "^24.8.0", | ||
"@types/jest": "27.x", | ||
"babel-jest": "27.x", | ||
"coveralls": "^2.13.1", | ||
"jest": "^24.8.0", | ||
"jest": "27.x", | ||
"lint-staged": "^7.2.0", | ||
"np": "^5.0.3", | ||
"pika-plugin-build-web-babel": "^0.8.0", | ||
"pika-plugin-clean-dist-src": "^0.1.1", | ||
"pika-plugin-build-web-babel": "^0.10.0", | ||
"pika-plugin-ts-types": "0.1.x", | ||
"pre-commit": "^1.2.2", | ||
"prettier": "^1.11.1" | ||
"prettier": "^1.11.1", | ||
"typescript": "^4.3.2" | ||
}, | ||
"types": "dist-types/index.d.ts", | ||
"main": "dist-node/index.js", | ||
"module": "dist-web/index.js", | ||
"types": "dist-types/index.d.ts" | ||
"module": "dist-web/index.js" | ||
} |
@@ -303,3 +303,3 @@ # async-validator | ||
Sometimes it is necessary to transform a value before validation, possibly to coerce the value or to sanitize it in some way. To do this add a `transform` function to the validation rule. The property is transformed prior to validation and re-assigned to the source object to mutate the value of the property in place. | ||
Sometimes it is necessary to transform a value before validation, possibly to coerce the value or to sanitize it in some way. To do this add a `transform` function to the validation rule. The property is transformed prior to validation and returned as promise result or callback result when pass validation. | ||
@@ -320,4 +320,9 @@ ```js | ||
const source = { name: ' user ' }; | ||
validator.validate(source) | ||
.then(() => assert.equal(source.name, 'user')); | ||
.then((data) => assert.equal(data.name, 'user')); | ||
validator.validate(source,(errors, data)=>{ | ||
assert.equal(data.name, 'user')); | ||
}); | ||
``` | ||
@@ -324,0 +329,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
50
525%468
1.08%254124
-0.08%16
14.29%2371
-4.32%