Socket
Socket
Sign inDemoInstall

async-validator

Package Overview
Dependencies
Maintainers
2
Versions
86
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-validator - npm Package Compare versions

Comparing version 4.0.5 to 4.0.6

646

dist-node/index.js

@@ -11,10 +11,121 @@ 'use strict';

function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
_setPrototypeOf(subClass, superClass);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
return true;
} catch (e) {
return false;
}
}
function _construct(Parent, args, Class) {
if (_isNativeReflectConstruct()) {
_construct = Reflect.construct;
} else {
_construct = function _construct(Parent, args, Class) {
var a = [null];
a.push.apply(a, args);
var Constructor = Function.bind.apply(Parent, a);
var instance = new Constructor();
if (Class) _setPrototypeOf(instance, Class.prototype);
return instance;
};
}
return _construct.apply(null, arguments);
}
function _isNativeFunction(fn) {
return Function.toString.call(fn).indexOf("[native code]") !== -1;
}
function _wrapNativeSuper(Class) {
var _cache = typeof Map === "function" ? new Map() : undefined;
_wrapNativeSuper = function _wrapNativeSuper(Class) {
if (Class === null || !_isNativeFunction(Class)) return Class;
if (typeof Class !== "function") {
throw new TypeError("Super expression must either be null or a function");
}
if (typeof _cache !== "undefined") {
if (_cache.has(Class)) return _cache.get(Class);
_cache.set(Class, Wrapper);
}
function Wrapper() {
return _construct(Class, arguments, _getPrototypeOf(this).constructor);
}
Wrapper.prototype = Object.create(Class.prototype, {
constructor: {
value: Wrapper,
enumerable: false,
writable: true,
configurable: true
}
});
return _setPrototypeOf(Wrapper, Class);
};
return _wrapNativeSuper(Class);
}
/* eslint no-console:0 */
const formatRegExp = /%[sdj%]/g;
let warning = () => {}; // don't print warning message when in production env or node runtime
var formatRegExp = /%[sdj%]/g;
var warning = function warning() {}; // don't print warning message when in production env or node runtime
if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV !== 'production' && typeof window !== 'undefined' && typeof document !== 'undefined') {
warning = (type, errors) => {
warning = function warning(type, errors) {
if (typeof console !== 'undefined' && console.warn && typeof ASYNC_VALIDATOR_NO_WARNING === 'undefined') {
if (errors.every(e => typeof e === 'string')) {
if (errors.every(function (e) {
return typeof e === 'string';
})) {
console.warn(type, errors);

@@ -28,5 +139,5 @@ }

if (!errors || !errors.length) return null;
const fields = {};
errors.forEach(error => {
const field = error.field;
var fields = {};
errors.forEach(function (error) {
var field = error.field;
fields[field] = fields[field] || [];

@@ -37,6 +148,10 @@ fields[field].push(error);

}
function format(template, ...args) {
let i = 0;
const len = args.length;
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 = 0;
var len = args.length;
if (typeof template === 'function') {

@@ -47,3 +162,3 @@ return template.apply(null, args);

if (typeof template === 'string') {
let str = template.replace(formatRegExp, x => {
var str = template.replace(formatRegExp, function (x) {
if (x === '%%') {

@@ -104,8 +219,8 @@ return '%';

function asyncParallelArray(arr, func, callback) {
const results = [];
let total = 0;
const arrLength = arr.length;
var results = [];
var total = 0;
var arrLength = arr.length;
function count(errors) {
results.push(...(errors || []));
results.push.apply(results, errors || []);
total++;

@@ -118,3 +233,3 @@

arr.forEach(a => {
arr.forEach(function (a) {
func(a, count);

@@ -125,4 +240,4 @@ });

function asyncSerialArray(arr, func, callback) {
let index = 0;
const arrLength = arr.length;
var index = 0;
var arrLength = arr.length;

@@ -135,3 +250,3 @@ function next(errors) {

const original = index;
var original = index;
index = index + 1;

@@ -150,5 +265,5 @@

function flattenObjArr(objArr) {
const ret = [];
Object.keys(objArr).forEach(k => {
ret.push(...(objArr[k] || []));
var ret = [];
Object.keys(objArr).forEach(function (k) {
ret.push.apply(ret, objArr[k] || []);
});

@@ -158,14 +273,20 @@ return ret;

class AsyncValidationError extends Error {
constructor(errors, fields) {
super('Async Validation Error');
this.errors = errors;
this.fields = fields;
var AsyncValidationError = /*#__PURE__*/function (_Error) {
_inheritsLoose(AsyncValidationError, _Error);
function AsyncValidationError(errors, fields) {
var _this;
_this = _Error.call(this, 'Async Validation Error') || this;
_this.errors = errors;
_this.fields = fields;
return _this;
}
}
return AsyncValidationError;
}( /*#__PURE__*/_wrapNativeSuper(Error));
function asyncMap(objArr, option, func, callback, source) {
if (option.first) {
const pending = new Promise((resolve, reject) => {
const next = errors => {
var _pending = new Promise(function (resolve, reject) {
var next = function next(errors) {
callback(errors);

@@ -175,16 +296,20 @@ return errors.length ? reject(new AsyncValidationError(errors, convertFieldsError(errors))) : resolve(source);

const flattenArr = flattenObjArr(objArr);
var flattenArr = flattenObjArr(objArr);
asyncSerialArray(flattenArr, func, next);
});
pending.catch(e => e);
return pending;
_pending["catch"](function (e) {
return e;
});
return _pending;
}
const firstFields = option.firstFields === true ? Object.keys(objArr) : option.firstFields || [];
const objArrKeys = Object.keys(objArr);
const objArrLength = objArrKeys.length;
let total = 0;
const results = [];
const pending = new Promise((resolve, reject) => {
const next = errors => {
var firstFields = option.firstFields === true ? Object.keys(objArr) : option.firstFields || [];
var objArrKeys = Object.keys(objArr);
var objArrLength = objArrKeys.length;
var total = 0;
var results = [];
var pending = new Promise(function (resolve, reject) {
var next = function next(errors) {
results.push.apply(results, errors);

@@ -204,4 +329,4 @@ total++;

objArrKeys.forEach(key => {
const arr = objArr[key];
objArrKeys.forEach(function (key) {
var arr = objArr[key];

@@ -215,3 +340,5 @@ if (firstFields.indexOf(key) !== -1) {

});
pending.catch(e => e);
pending["catch"](function (e) {
return e;
});
return pending;

@@ -225,5 +352,5 @@ }

function getValue(value, path) {
let v = value;
var v = value;
for (let i = 0; i < path.length; i++) {
for (var i = 0; i < path.length; i++) {
if (v == undefined) {

@@ -240,4 +367,4 @@ return v;

function complementError(rule, source) {
return oe => {
let fieldValue;
return function (oe) {
var fieldValue;

@@ -258,3 +385,3 @@ if (rule.fullFields) {

message: typeof oe === 'function' ? oe() : oe,
fieldValue,
fieldValue: fieldValue,
field: oe.field || rule.fullField

@@ -266,10 +393,8 @@ };

if (source) {
for (const s in source) {
for (var s in source) {
if (source.hasOwnProperty(s)) {
const value = source[s];
var value = source[s];
if (typeof value === 'object' && typeof target[s] === 'object') {
target[s] = { ...target[s],
...value
};
target[s] = _extends({}, target[s], value);
} else {

@@ -285,3 +410,3 @@ target[s] = value;

const required$1 = (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))) {

@@ -304,3 +429,3 @@ errors.push(format(options.messages.required, rule.fullField));

const whitespace = (rule, value, source, errors, options) => {
var whitespace = function whitespace(rule, value, source, errors, options) {
if (/^\s+$/.test(value) || value === '') {

@@ -313,3 +438,3 @@ errors.push(format(options.messages.whitespace, rule.fullField));

const pattern$2 = {
var pattern$2 = {
// http://emailregex.com/

@@ -322,16 +447,13 @@ email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+\.)+[a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}))$/,

};
const types = {
integer(value) {
var types = {
integer: function integer(value) {
return types.number(value) && parseInt(value, 10) === value;
},
float(value) {
"float": function float(value) {
return types.number(value) && !types.integer(value);
},
array(value) {
array: function array(value) {
return Array.isArray(value);
},
regexp(value) {
regexp: function regexp(value) {
if (value instanceof RegExp) {

@@ -347,8 +469,6 @@ return true;

},
date(value) {
date: function date(value) {
return typeof value.getTime === 'function' && typeof value.getMonth === 'function' && typeof value.getYear === 'function' && !isNaN(value.getTime());
},
number(value) {
number: function number(value) {
if (isNaN(value)) {

@@ -360,26 +480,20 @@ return false;

},
object(value) {
object: function object(value) {
return typeof value === 'object' && !types.array(value);
},
method(value) {
method: function method(value) {
return typeof value === 'function';
},
email(value) {
email: function email(value) {
return typeof value === 'string' && value.length <= 320 && !!value.match(pattern$2.email);
},
url(value) {
url: function url(value) {
return typeof value === 'string' && value.length <= 2048 && !!value.match(pattern$2.url);
},
hex(value) {
hex: function hex(value) {
return typeof value === 'string' && !!value.match(pattern$2.hex);
}
};
const type$1 = (rule, value, source, errors, options) => {
var type$1 = function type(rule, value, source, errors, options) {
if (rule.required && value === undefined) {

@@ -390,4 +504,4 @@ required$1(rule, value, source, errors, options);

const custom = ['integer', 'float', 'array', 'regexp', 'object', 'method', 'email', 'number', 'date', 'url', 'hex'];
const ruleType = rule.type;
var custom = ['integer', 'float', 'array', 'regexp', 'object', 'method', 'email', 'number', 'date', 'url', 'hex'];
var ruleType = rule.type;

@@ -404,13 +518,13 @@ if (custom.indexOf(ruleType) > -1) {

const range = (rule, value, source, errors, options) => {
const len = typeof rule.len === 'number';
const min = typeof rule.min === 'number';
const max = typeof rule.max === 'number'; // 正则匹配码点范围从U+010000一直到U+10FFFF的文字(补充平面Supplementary Plane)
var range = function range(rule, value, source, errors, options) {
var len = typeof rule.len === 'number';
var min = typeof rule.min === 'number';
var max = typeof rule.max === 'number'; // 正则匹配码点范围从U+010000一直到U+10FFFF的文字(补充平面Supplementary Plane)
const spRegexp = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
let val = value;
let key = null;
const num = typeof value === 'number';
const str = typeof value === 'string';
const arr = Array.isArray(value);
var spRegexp = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
var val = value;
var key = null;
var num = typeof value === 'number';
var str = typeof value === 'string';
var arr = Array.isArray(value);

@@ -454,5 +568,5 @@ if (num) {

const ENUM$1 = 'enum';
var ENUM$1 = 'enum';
const enumerable$1 = (rule, value, source, errors, options) => {
var enumerable$1 = function enumerable(rule, value, source, errors, options) {
rule[ENUM$1] = Array.isArray(rule[ENUM$1]) ? rule[ENUM$1] : [];

@@ -465,3 +579,3 @@

const pattern$1 = (rule, value, source, errors, options) => {
var pattern$1 = function pattern(rule, value, source, errors, options) {
if (rule.pattern) {

@@ -478,3 +592,3 @@ if (rule.pattern instanceof RegExp) {

} else if (typeof rule.pattern === 'string') {
const _pattern = new RegExp(rule.pattern);
var _pattern = new RegExp(rule.pattern);

@@ -490,12 +604,12 @@ if (!_pattern.test(value)) {

required: required$1,
whitespace,
whitespace: whitespace,
type: type$1,
range,
enum: enumerable$1,
range: range,
"enum": enumerable$1,
pattern: pattern$1
};
const string = (rule, value, callback, source, options) => {
const errors = [];
const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
var string = function string(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);

@@ -523,5 +637,5 @@ if (validate) {

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

@@ -543,5 +657,5 @@ if (validate) {

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

@@ -568,5 +682,5 @@ if (validate) {

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

@@ -588,5 +702,5 @@ if (validate) {

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

@@ -608,5 +722,5 @@ if (validate) {

const integer = (rule, value, callback, source, options) => {
const errors = [];
const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
var integer = function integer(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);

@@ -629,5 +743,5 @@ if (validate) {

const floatFn = (rule, value, callback, source, options) => {
const errors = [];
const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
var floatFn = function floatFn(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);

@@ -650,5 +764,5 @@ if (validate) {

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

@@ -671,5 +785,5 @@ if (validate) {

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

@@ -691,7 +805,7 @@ if (validate) {

const ENUM = 'enum';
var ENUM = 'enum';
const enumerable = (rule, value, callback, source, options) => {
const errors = [];
const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
var enumerable = function enumerable(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);

@@ -713,5 +827,5 @@ if (validate) {

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

@@ -733,6 +847,6 @@ if (validate) {

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

@@ -747,3 +861,3 @@ if (validate) {

if (!isEmptyValue(value, 'date')) {
let dateObject;
var dateObject;

@@ -767,5 +881,5 @@ if (value instanceof Date) {

const required = (rule, value, callback, source, options) => {
const errors = [];
const type = Array.isArray(value) ? 'array' : typeof value;
var required = function required(rule, value, callback, source, options) {
var errors = [];
var type = Array.isArray(value) ? 'array' : typeof value;
rules.required(rule, value, source, errors, options, type);

@@ -775,6 +889,6 @@ callback(errors);

const type = (rule, value, callback, source, options) => {
const ruleType = rule.type;
const errors = [];
const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
var type = function type(rule, value, callback, source, options) {
var ruleType = rule.type;
var errors = [];
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);

@@ -796,5 +910,5 @@ if (validate) {

const any = (rule, value, callback, source, options) => {
const errors = [];
const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
var any = function any(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);

@@ -813,19 +927,19 @@ if (validate) {

var validators = {
string,
method,
number,
boolean,
regexp,
integer,
float: floatFn,
array,
object,
enum: enumerable,
pattern,
date,
string: string,
method: method,
number: number,
"boolean": _boolean,
regexp: regexp,
integer: integer,
"float": floatFn,
array: array,
object: object,
"enum": enumerable,
pattern: pattern,
date: date,
url: type,
hex: type,
email: type,
required,
any
required: required,
any: any
};

@@ -835,5 +949,5 @@

return {
default: 'Validation error on field %s',
"default": 'Validation error on field %s',
required: '%s is required',
enum: '%s must be one of %s',
"enum": '%s must be one of %s',
whitespace: '%s cannot be empty',

@@ -852,5 +966,5 @@ date: {

date: '%s is not a %s',
boolean: '%s is not a %s',
"boolean": '%s is not a %s',
integer: '%s is not an %s',
float: '%s is not a %s',
"float": '%s is not a %s',
regexp: '%s is not a valid %s',

@@ -882,12 +996,10 @@ email: '%s is not a valid %s',

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

@@ -901,23 +1013,16 @@ /**

class Schema {
var Schema = /*#__PURE__*/function () {
// ========================= Static =========================
static register = function register(type, validator) {
if (typeof validator !== 'function') {
throw new Error('Cannot register a validator by type, validator is not a function');
}
// ======================== Instance ========================
function Schema(descriptor) {
this.rules = null;
this._messages = messages;
this.define(descriptor);
}
validators[type] = validator;
};
static warning = warning;
static messages = messages;
static validators = validators; // ======================== Instance ========================
var _proto = Schema.prototype;
rules = null;
_messages = messages;
_proto.define = function define(rules) {
var _this = this;
constructor(descriptor) {
this.define(descriptor);
}
define(rules) {
if (!rules) {

@@ -932,21 +1037,31 @@ throw new Error('Cannot configure a schema with no rules');

this.rules = {};
Object.keys(rules).forEach(name => {
const item = rules[name];
this.rules[name] = Array.isArray(item) ? item : [item];
Object.keys(rules).forEach(function (name) {
var item = rules[name];
_this.rules[name] = Array.isArray(item) ? item : [item];
});
}
};
messages(messages) {
if (messages) {
this._messages = deepMerge(newMessages(), messages);
_proto.messages = function messages(_messages) {
if (_messages) {
this._messages = deepMerge(newMessages(), _messages);
}
return this._messages;
}
};
validate(source_, o = {}, oc = () => {}) {
let source = source_;
let options = o;
let callback = oc;
_proto.validate = function validate(source_, o, oc) {
var _this2 = this;
if (o === void 0) {
o = {};
}
if (oc === void 0) {
oc = function oc() {};
}
var source = source_;
var options = o;
var callback = oc;
if (typeof options === 'function') {

@@ -966,8 +1081,10 @@ callback = options;

function complete(results) {
let errors = [];
let fields = {};
var errors = [];
var fields = {};
function add(e) {
if (Array.isArray(e)) {
errors = errors.concat(...e);
var _errors;
errors = (_errors = errors).concat.apply(_errors, e);
} else {

@@ -978,3 +1095,3 @@ errors.push(e);

for (let i = 0; i < results.length; i++) {
for (var i = 0; i < results.length; i++) {
add(results[i]);

@@ -992,3 +1109,3 @@ }

if (options.messages) {
let messages$1 = this.messages();
var messages$1 = this.messages();

@@ -1005,14 +1122,13 @@ if (messages$1 === messages) {

const series = {};
const keys = options.keys || Object.keys(this.rules);
keys.forEach(z => {
const arr = this.rules[z];
let value = source[z];
arr.forEach(r => {
let rule = r;
var series = {};
var keys = options.keys || Object.keys(this.rules);
keys.forEach(function (z) {
var arr = _this2.rules[z];
var value = source[z];
arr.forEach(function (r) {
var rule = r;
if (typeof rule.transform === 'function') {
if (source === source_) {
source = { ...source
};
source = _extends({}, source);
}

@@ -1028,8 +1144,7 @@

} else {
rule = { ...rule
};
rule = _extends({}, rule);
} // Fill validator. Skip if nothing need to validate
rule.validator = this.getValidationMethod(rule);
rule.validator = _this2.getValidationMethod(rule);

@@ -1042,8 +1157,8 @@ if (!rule.validator) {

rule.fullField = rule.fullField || z;
rule.type = this.getType(rule);
rule.type = _this2.getType(rule);
series[z] = series[z] || [];
series[z].push({
rule,
value,
source,
rule: rule,
value: value,
source: source,
field: z

@@ -1053,6 +1168,6 @@ });

});
const errorFields = {};
return asyncMap(series, options, (data, doIt) => {
const rule = data.rule;
let deep = (rule.type === 'object' || rule.type === 'array') && (typeof rule.fields === 'object' || typeof rule.defaultField === 'object');
var errorFields = {};
return asyncMap(series, options, function (data, doIt) {
var rule = data.rule;
var deep = (rule.type === 'object' || rule.type === 'array') && (typeof rule.fields === 'object' || typeof rule.defaultField === 'object');
deep = deep && (rule.required || !rule.required && data.value);

@@ -1062,11 +1177,15 @@ rule.field = data.field;

function addFullField(key, schema) {
return { ...schema,
fullField: `${rule.fullField}.${key}`,
fullFields: rule.fullFields ? [...rule.fullFields, key] : [key]
};
return _extends({}, schema, {
fullField: rule.fullField + "." + key,
fullFields: rule.fullFields ? [].concat(rule.fullFields, [key]) : [key]
});
}
function cb(e = []) {
let errorList = Array.isArray(e) ? e : [e];
function cb(e) {
if (e === void 0) {
e = [];
}
var errorList = Array.isArray(e) ? e : [e];
if (!options.suppressWarning && errorList.length) {

@@ -1081,3 +1200,3 @@ Schema.warning('async-validator:', errorList);

let filledErrors = errorList.map(complementError(rule, source));
var filledErrors = errorList.map(complementError(rule, source));

@@ -1105,6 +1224,6 @@ if (options.first && filledErrors.length) {

let fieldsSchema = {};
var fieldsSchema = {};
if (rule.defaultField) {
Object.keys(data.value).map(key => {
Object.keys(data.value).map(function (key) {
fieldsSchema[key] = rule.defaultField;

@@ -1114,12 +1233,10 @@ });

fieldsSchema = { ...fieldsSchema,
...data.rule.fields
};
const paredFieldsSchema = {};
Object.keys(fieldsSchema).forEach(field => {
const fieldSchema = fieldsSchema[field];
const fieldSchemaList = Array.isArray(fieldSchema) ? fieldSchema : [fieldSchema];
fieldsSchema = _extends({}, fieldsSchema, data.rule.fields);
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));
});
const schema = new Schema(paredFieldsSchema);
var schema = new Schema(paredFieldsSchema);
schema.messages(options.messages);

@@ -1132,11 +1249,11 @@

schema.validate(data.value, data.rule.options || options, errs => {
const finalErrors = [];
schema.validate(data.value, data.rule.options || options, function (errs) {
var finalErrors = [];
if (filledErrors && filledErrors.length) {
finalErrors.push(...filledErrors);
finalErrors.push.apply(finalErrors, filledErrors);
}
if (errs && errs.length) {
finalErrors.push(...errs);
finalErrors.push.apply(finalErrors, errs);
}

@@ -1149,3 +1266,3 @@

let res;
var res;

@@ -1160,3 +1277,3 @@ if (rule.asyncValidator) {

} else if (res === false) {
cb(typeof rule.message === 'function' ? rule.message(rule.fullField || rule.field) : rule.message || `${rule.fullField || 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) {

@@ -1170,10 +1287,14 @@ cb(res);

if (res && res.then) {
res.then(() => cb(), e => cb(e));
res.then(function () {
return cb();
}, function (e) {
return cb(e);
});
}
}, results => {
}, function (results) {
complete(results);
}, source);
}
};
getType(rule) {
_proto.getType = function getType(rule) {
if (rule.type === undefined && rule.pattern instanceof RegExp) {

@@ -1188,5 +1309,5 @@ rule.type = 'pattern';

return rule.type || 'string';
}
};
getValidationMethod(rule) {
_proto.getValidationMethod = function getValidationMethod(rule) {
if (typeof rule.validator === 'function') {

@@ -1196,4 +1317,4 @@ return rule.validator;

const keys = Object.keys(rule);
const messageIndex = keys.indexOf('message');
var keys = Object.keys(rule);
var messageIndex = keys.indexOf('message');

@@ -1209,7 +1330,20 @@ if (messageIndex !== -1) {

return validators[this.getType(rule)] || undefined;
};
return Schema;
}();
Schema.register = function register(type, validator) {
if (typeof validator !== 'function') {
throw new Error('Cannot register a validator by type, validator is not a function');
}
}
validators[type] = validator;
};
Schema.warning = warning;
Schema.messages = messages;
Schema.validators = validators;
exports['default'] = Schema;
//# sourceMappingURL=index.js.map
import urlRegex from 'url-regex';
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
_setPrototypeOf(subClass, superClass);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
return true;
} catch (e) {
return false;
}
}
function _construct(Parent, args, Class) {
if (_isNativeReflectConstruct()) {
_construct = Reflect.construct;
} else {
_construct = function _construct(Parent, args, Class) {
var a = [null];
a.push.apply(a, args);
var Constructor = Function.bind.apply(Parent, a);
var instance = new Constructor();
if (Class) _setPrototypeOf(instance, Class.prototype);
return instance;
};
}
return _construct.apply(null, arguments);
}
function _isNativeFunction(fn) {
return Function.toString.call(fn).indexOf("[native code]") !== -1;
}
function _wrapNativeSuper(Class) {
var _cache = typeof Map === "function" ? new Map() : undefined;
_wrapNativeSuper = function _wrapNativeSuper(Class) {
if (Class === null || !_isNativeFunction(Class)) return Class;
if (typeof Class !== "function") {
throw new TypeError("Super expression must either be null or a function");
}
if (typeof _cache !== "undefined") {
if (_cache.has(Class)) return _cache.get(Class);
_cache.set(Class, Wrapper);
}
function Wrapper() {
return _construct(Class, arguments, _getPrototypeOf(this).constructor);
}
Wrapper.prototype = Object.create(Class.prototype, {
constructor: {
value: Wrapper,
enumerable: false,
writable: true,
configurable: true
}
});
return _setPrototypeOf(Wrapper, Class);
};
return _wrapNativeSuper(Class);
}
/* eslint no-console:0 */
const formatRegExp = /%[sdj%]/g;
let warning = () => {}; // don't print warning message when in production env or node runtime
var formatRegExp = /%[sdj%]/g;
var warning = function warning() {}; // don't print warning message when in production env or node runtime
if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV !== 'production' && typeof window !== 'undefined' && typeof document !== 'undefined') {
warning = (type, errors) => {
warning = function warning(type, errors) {
if (typeof console !== 'undefined' && console.warn && typeof ASYNC_VALIDATOR_NO_WARNING === 'undefined') {
if (errors.every(e => typeof e === 'string')) {
if (errors.every(function (e) {
return typeof e === 'string';
})) {
console.warn(type, errors);

@@ -19,5 +130,5 @@ }

if (!errors || !errors.length) return null;
const fields = {};
errors.forEach(error => {
const field = error.field;
var fields = {};
errors.forEach(function (error) {
var field = error.field;
fields[field] = fields[field] || [];

@@ -28,6 +139,10 @@ fields[field].push(error);

}
function format(template, ...args) {
let i = 0;
const len = args.length;
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 = 0;
var len = args.length;
if (typeof template === 'function') {

@@ -38,3 +153,3 @@ return template.apply(null, args);

if (typeof template === 'string') {
let str = template.replace(formatRegExp, x => {
var str = template.replace(formatRegExp, function (x) {
if (x === '%%') {

@@ -95,8 +210,8 @@ return '%';

function asyncParallelArray(arr, func, callback) {
const results = [];
let total = 0;
const arrLength = arr.length;
var results = [];
var total = 0;
var arrLength = arr.length;
function count(errors) {
results.push(...(errors || []));
results.push.apply(results, errors || []);
total++;

@@ -109,3 +224,3 @@

arr.forEach(a => {
arr.forEach(function (a) {
func(a, count);

@@ -116,4 +231,4 @@ });

function asyncSerialArray(arr, func, callback) {
let index = 0;
const arrLength = arr.length;
var index = 0;
var arrLength = arr.length;

@@ -126,3 +241,3 @@ function next(errors) {

const original = index;
var original = index;
index = index + 1;

@@ -141,5 +256,5 @@

function flattenObjArr(objArr) {
const ret = [];
Object.keys(objArr).forEach(k => {
ret.push(...(objArr[k] || []));
var ret = [];
Object.keys(objArr).forEach(function (k) {
ret.push.apply(ret, objArr[k] || []);
});

@@ -149,14 +264,20 @@ return ret;

class AsyncValidationError extends Error {
constructor(errors, fields) {
super('Async Validation Error');
this.errors = errors;
this.fields = fields;
var AsyncValidationError = /*#__PURE__*/function (_Error) {
_inheritsLoose(AsyncValidationError, _Error);
function AsyncValidationError(errors, fields) {
var _this;
_this = _Error.call(this, 'Async Validation Error') || this;
_this.errors = errors;
_this.fields = fields;
return _this;
}
}
return AsyncValidationError;
}( /*#__PURE__*/_wrapNativeSuper(Error));
function asyncMap(objArr, option, func, callback, source) {
if (option.first) {
const pending = new Promise((resolve, reject) => {
const next = errors => {
var _pending = new Promise(function (resolve, reject) {
var next = function next(errors) {
callback(errors);

@@ -166,16 +287,20 @@ return errors.length ? reject(new AsyncValidationError(errors, convertFieldsError(errors))) : resolve(source);

const flattenArr = flattenObjArr(objArr);
var flattenArr = flattenObjArr(objArr);
asyncSerialArray(flattenArr, func, next);
});
pending.catch(e => e);
return pending;
_pending["catch"](function (e) {
return e;
});
return _pending;
}
const firstFields = option.firstFields === true ? Object.keys(objArr) : option.firstFields || [];
const objArrKeys = Object.keys(objArr);
const objArrLength = objArrKeys.length;
let total = 0;
const results = [];
const pending = new Promise((resolve, reject) => {
const next = errors => {
var firstFields = option.firstFields === true ? Object.keys(objArr) : option.firstFields || [];
var objArrKeys = Object.keys(objArr);
var objArrLength = objArrKeys.length;
var total = 0;
var results = [];
var pending = new Promise(function (resolve, reject) {
var next = function next(errors) {
results.push.apply(results, errors);

@@ -195,4 +320,4 @@ total++;

objArrKeys.forEach(key => {
const arr = objArr[key];
objArrKeys.forEach(function (key) {
var arr = objArr[key];

@@ -206,3 +331,5 @@ if (firstFields.indexOf(key) !== -1) {

});
pending.catch(e => e);
pending["catch"](function (e) {
return e;
});
return pending;

@@ -216,5 +343,5 @@ }

function getValue(value, path) {
let v = value;
var v = value;
for (let i = 0; i < path.length; i++) {
for (var i = 0; i < path.length; i++) {
if (v == undefined) {

@@ -231,4 +358,4 @@ return v;

function complementError(rule, source) {
return oe => {
let fieldValue;
return function (oe) {
var fieldValue;

@@ -249,3 +376,3 @@ if (rule.fullFields) {

message: typeof oe === 'function' ? oe() : oe,
fieldValue,
fieldValue: fieldValue,
field: oe.field || rule.fullField

@@ -257,10 +384,8 @@ };

if (source) {
for (const s in source) {
for (var s in source) {
if (source.hasOwnProperty(s)) {
const value = source[s];
var value = source[s];
if (typeof value === 'object' && typeof target[s] === 'object') {
target[s] = { ...target[s],
...value
};
target[s] = _extends({}, target[s], value);
} else {

@@ -276,3 +401,3 @@ target[s] = value;

const required$1 = (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))) {

@@ -295,3 +420,3 @@ errors.push(format(options.messages.required, rule.fullField));

const whitespace = (rule, value, source, errors, options) => {
var whitespace = function whitespace(rule, value, source, errors, options) {
if (/^\s+$/.test(value) || value === '') {

@@ -304,3 +429,3 @@ errors.push(format(options.messages.whitespace, rule.fullField));

const pattern$2 = {
var pattern$2 = {
// http://emailregex.com/

@@ -313,16 +438,13 @@ email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+\.)+[a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}))$/,

};
const types = {
integer(value) {
var types = {
integer: function integer(value) {
return types.number(value) && parseInt(value, 10) === value;
},
float(value) {
"float": function float(value) {
return types.number(value) && !types.integer(value);
},
array(value) {
array: function array(value) {
return Array.isArray(value);
},
regexp(value) {
regexp: function regexp(value) {
if (value instanceof RegExp) {

@@ -338,8 +460,6 @@ return true;

},
date(value) {
date: function date(value) {
return typeof value.getTime === 'function' && typeof value.getMonth === 'function' && typeof value.getYear === 'function' && !isNaN(value.getTime());
},
number(value) {
number: function number(value) {
if (isNaN(value)) {

@@ -351,26 +471,20 @@ return false;

},
object(value) {
object: function object(value) {
return typeof value === 'object' && !types.array(value);
},
method(value) {
method: function method(value) {
return typeof value === 'function';
},
email(value) {
email: function email(value) {
return typeof value === 'string' && value.length <= 320 && !!value.match(pattern$2.email);
},
url(value) {
url: function url(value) {
return typeof value === 'string' && value.length <= 2048 && !!value.match(pattern$2.url);
},
hex(value) {
hex: function hex(value) {
return typeof value === 'string' && !!value.match(pattern$2.hex);
}
};
const type$1 = (rule, value, source, errors, options) => {
var type$1 = function type(rule, value, source, errors, options) {
if (rule.required && value === undefined) {

@@ -381,4 +495,4 @@ required$1(rule, value, source, errors, options);

const custom = ['integer', 'float', 'array', 'regexp', 'object', 'method', 'email', 'number', 'date', 'url', 'hex'];
const ruleType = rule.type;
var custom = ['integer', 'float', 'array', 'regexp', 'object', 'method', 'email', 'number', 'date', 'url', 'hex'];
var ruleType = rule.type;

@@ -395,13 +509,13 @@ if (custom.indexOf(ruleType) > -1) {

const range = (rule, value, source, errors, options) => {
const len = typeof rule.len === 'number';
const min = typeof rule.min === 'number';
const max = typeof rule.max === 'number'; // 正则匹配码点范围从U+010000一直到U+10FFFF的文字(补充平面Supplementary Plane)
var range = function range(rule, value, source, errors, options) {
var len = typeof rule.len === 'number';
var min = typeof rule.min === 'number';
var max = typeof rule.max === 'number'; // 正则匹配码点范围从U+010000一直到U+10FFFF的文字(补充平面Supplementary Plane)
const spRegexp = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
let val = value;
let key = null;
const num = typeof value === 'number';
const str = typeof value === 'string';
const arr = Array.isArray(value);
var spRegexp = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
var val = value;
var key = null;
var num = typeof value === 'number';
var str = typeof value === 'string';
var arr = Array.isArray(value);

@@ -445,5 +559,5 @@ if (num) {

const ENUM$1 = 'enum';
var ENUM$1 = 'enum';
const enumerable$1 = (rule, value, source, errors, options) => {
var enumerable$1 = function enumerable(rule, value, source, errors, options) {
rule[ENUM$1] = Array.isArray(rule[ENUM$1]) ? rule[ENUM$1] : [];

@@ -456,3 +570,3 @@

const pattern$1 = (rule, value, source, errors, options) => {
var pattern$1 = function pattern(rule, value, source, errors, options) {
if (rule.pattern) {

@@ -469,3 +583,3 @@ if (rule.pattern instanceof RegExp) {

} else if (typeof rule.pattern === 'string') {
const _pattern = new RegExp(rule.pattern);
var _pattern = new RegExp(rule.pattern);

@@ -481,12 +595,12 @@ if (!_pattern.test(value)) {

required: required$1,
whitespace,
whitespace: whitespace,
type: type$1,
range,
enum: enumerable$1,
range: range,
"enum": enumerable$1,
pattern: pattern$1
};
const string = (rule, value, callback, source, options) => {
const errors = [];
const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
var string = function string(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);

@@ -514,5 +628,5 @@ if (validate) {

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

@@ -534,5 +648,5 @@ if (validate) {

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

@@ -559,5 +673,5 @@ if (validate) {

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

@@ -579,5 +693,5 @@ if (validate) {

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

@@ -599,5 +713,5 @@ if (validate) {

const integer = (rule, value, callback, source, options) => {
const errors = [];
const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
var integer = function integer(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);

@@ -620,5 +734,5 @@ if (validate) {

const floatFn = (rule, value, callback, source, options) => {
const errors = [];
const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
var floatFn = function floatFn(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);

@@ -641,5 +755,5 @@ if (validate) {

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

@@ -662,5 +776,5 @@ if (validate) {

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

@@ -682,7 +796,7 @@ if (validate) {

const ENUM = 'enum';
var ENUM = 'enum';
const enumerable = (rule, value, callback, source, options) => {
const errors = [];
const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
var enumerable = function enumerable(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);

@@ -704,5 +818,5 @@ if (validate) {

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

@@ -724,6 +838,6 @@ if (validate) {

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

@@ -738,3 +852,3 @@ if (validate) {

if (!isEmptyValue(value, 'date')) {
let dateObject;
var dateObject;

@@ -758,5 +872,5 @@ if (value instanceof Date) {

const required = (rule, value, callback, source, options) => {
const errors = [];
const type = Array.isArray(value) ? 'array' : typeof value;
var required = function required(rule, value, callback, source, options) {
var errors = [];
var type = Array.isArray(value) ? 'array' : typeof value;
rules.required(rule, value, source, errors, options, type);

@@ -766,6 +880,6 @@ callback(errors);

const type = (rule, value, callback, source, options) => {
const ruleType = rule.type;
const errors = [];
const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
var type = function type(rule, value, callback, source, options) {
var ruleType = rule.type;
var errors = [];
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);

@@ -787,5 +901,5 @@ if (validate) {

const any = (rule, value, callback, source, options) => {
const errors = [];
const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
var any = function any(rule, value, callback, source, options) {
var errors = [];
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);

@@ -804,19 +918,19 @@ if (validate) {

var validators = {
string,
method,
number,
boolean,
regexp,
integer,
float: floatFn,
array,
object,
enum: enumerable,
pattern,
date,
string: string,
method: method,
number: number,
"boolean": _boolean,
regexp: regexp,
integer: integer,
"float": floatFn,
array: array,
object: object,
"enum": enumerable,
pattern: pattern,
date: date,
url: type,
hex: type,
email: type,
required,
any
required: required,
any: any
};

@@ -826,5 +940,5 @@

return {
default: 'Validation error on field %s',
"default": 'Validation error on field %s',
required: '%s is required',
enum: '%s must be one of %s',
"enum": '%s must be one of %s',
whitespace: '%s cannot be empty',

@@ -843,5 +957,5 @@ date: {

date: '%s is not a %s',
boolean: '%s is not a %s',
"boolean": '%s is not a %s',
integer: '%s is not an %s',
float: '%s is not a %s',
"float": '%s is not a %s',
regexp: '%s is not a valid %s',

@@ -873,12 +987,10 @@ email: '%s is not a valid %s',

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

@@ -892,23 +1004,16 @@ /**

class Schema {
var Schema = /*#__PURE__*/function () {
// ========================= Static =========================
static register = function register(type, validator) {
if (typeof validator !== 'function') {
throw new Error('Cannot register a validator by type, validator is not a function');
}
// ======================== Instance ========================
function Schema(descriptor) {
this.rules = null;
this._messages = messages;
this.define(descriptor);
}
validators[type] = validator;
};
static warning = warning;
static messages = messages;
static validators = validators; // ======================== Instance ========================
var _proto = Schema.prototype;
rules = null;
_messages = messages;
_proto.define = function define(rules) {
var _this = this;
constructor(descriptor) {
this.define(descriptor);
}
define(rules) {
if (!rules) {

@@ -923,21 +1028,31 @@ throw new Error('Cannot configure a schema with no rules');

this.rules = {};
Object.keys(rules).forEach(name => {
const item = rules[name];
this.rules[name] = Array.isArray(item) ? item : [item];
Object.keys(rules).forEach(function (name) {
var item = rules[name];
_this.rules[name] = Array.isArray(item) ? item : [item];
});
}
};
messages(messages) {
if (messages) {
this._messages = deepMerge(newMessages(), messages);
_proto.messages = function messages(_messages) {
if (_messages) {
this._messages = deepMerge(newMessages(), _messages);
}
return this._messages;
}
};
validate(source_, o = {}, oc = () => {}) {
let source = source_;
let options = o;
let callback = oc;
_proto.validate = function validate(source_, o, oc) {
var _this2 = this;
if (o === void 0) {
o = {};
}
if (oc === void 0) {
oc = function oc() {};
}
var source = source_;
var options = o;
var callback = oc;
if (typeof options === 'function') {

@@ -957,8 +1072,10 @@ callback = options;

function complete(results) {
let errors = [];
let fields = {};
var errors = [];
var fields = {};
function add(e) {
if (Array.isArray(e)) {
errors = errors.concat(...e);
var _errors;
errors = (_errors = errors).concat.apply(_errors, e);
} else {

@@ -969,3 +1086,3 @@ errors.push(e);

for (let i = 0; i < results.length; i++) {
for (var i = 0; i < results.length; i++) {
add(results[i]);

@@ -983,3 +1100,3 @@ }

if (options.messages) {
let messages$1 = this.messages();
var messages$1 = this.messages();

@@ -996,14 +1113,13 @@ if (messages$1 === messages) {

const series = {};
const keys = options.keys || Object.keys(this.rules);
keys.forEach(z => {
const arr = this.rules[z];
let value = source[z];
arr.forEach(r => {
let rule = r;
var series = {};
var keys = options.keys || Object.keys(this.rules);
keys.forEach(function (z) {
var arr = _this2.rules[z];
var value = source[z];
arr.forEach(function (r) {
var rule = r;
if (typeof rule.transform === 'function') {
if (source === source_) {
source = { ...source
};
source = _extends({}, source);
}

@@ -1019,8 +1135,7 @@

} else {
rule = { ...rule
};
rule = _extends({}, rule);
} // Fill validator. Skip if nothing need to validate
rule.validator = this.getValidationMethod(rule);
rule.validator = _this2.getValidationMethod(rule);

@@ -1033,8 +1148,8 @@ if (!rule.validator) {

rule.fullField = rule.fullField || z;
rule.type = this.getType(rule);
rule.type = _this2.getType(rule);
series[z] = series[z] || [];
series[z].push({
rule,
value,
source,
rule: rule,
value: value,
source: source,
field: z

@@ -1044,6 +1159,6 @@ });

});
const errorFields = {};
return asyncMap(series, options, (data, doIt) => {
const rule = data.rule;
let deep = (rule.type === 'object' || rule.type === 'array') && (typeof rule.fields === 'object' || typeof rule.defaultField === 'object');
var errorFields = {};
return asyncMap(series, options, function (data, doIt) {
var rule = data.rule;
var deep = (rule.type === 'object' || rule.type === 'array') && (typeof rule.fields === 'object' || typeof rule.defaultField === 'object');
deep = deep && (rule.required || !rule.required && data.value);

@@ -1053,11 +1168,15 @@ rule.field = data.field;

function addFullField(key, schema) {
return { ...schema,
fullField: `${rule.fullField}.${key}`,
fullFields: rule.fullFields ? [...rule.fullFields, key] : [key]
};
return _extends({}, schema, {
fullField: rule.fullField + "." + key,
fullFields: rule.fullFields ? [].concat(rule.fullFields, [key]) : [key]
});
}
function cb(e = []) {
let errorList = Array.isArray(e) ? e : [e];
function cb(e) {
if (e === void 0) {
e = [];
}
var errorList = Array.isArray(e) ? e : [e];
if (!options.suppressWarning && errorList.length) {

@@ -1072,3 +1191,3 @@ Schema.warning('async-validator:', errorList);

let filledErrors = errorList.map(complementError(rule, source));
var filledErrors = errorList.map(complementError(rule, source));

@@ -1096,6 +1215,6 @@ if (options.first && filledErrors.length) {

let fieldsSchema = {};
var fieldsSchema = {};
if (rule.defaultField) {
Object.keys(data.value).map(key => {
Object.keys(data.value).map(function (key) {
fieldsSchema[key] = rule.defaultField;

@@ -1105,12 +1224,10 @@ });

fieldsSchema = { ...fieldsSchema,
...data.rule.fields
};
const paredFieldsSchema = {};
Object.keys(fieldsSchema).forEach(field => {
const fieldSchema = fieldsSchema[field];
const fieldSchemaList = Array.isArray(fieldSchema) ? fieldSchema : [fieldSchema];
fieldsSchema = _extends({}, fieldsSchema, data.rule.fields);
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));
});
const schema = new Schema(paredFieldsSchema);
var schema = new Schema(paredFieldsSchema);
schema.messages(options.messages);

@@ -1123,11 +1240,11 @@

schema.validate(data.value, data.rule.options || options, errs => {
const finalErrors = [];
schema.validate(data.value, data.rule.options || options, function (errs) {
var finalErrors = [];
if (filledErrors && filledErrors.length) {
finalErrors.push(...filledErrors);
finalErrors.push.apply(finalErrors, filledErrors);
}
if (errs && errs.length) {
finalErrors.push(...errs);
finalErrors.push.apply(finalErrors, errs);
}

@@ -1140,3 +1257,3 @@

let res;
var res;

@@ -1151,3 +1268,3 @@ if (rule.asyncValidator) {

} else if (res === false) {
cb(typeof rule.message === 'function' ? rule.message(rule.fullField || rule.field) : rule.message || `${rule.fullField || 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) {

@@ -1161,10 +1278,14 @@ cb(res);

if (res && res.then) {
res.then(() => cb(), e => cb(e));
res.then(function () {
return cb();
}, function (e) {
return cb(e);
});
}
}, results => {
}, function (results) {
complete(results);
}, source);
}
};
getType(rule) {
_proto.getType = function getType(rule) {
if (rule.type === undefined && rule.pattern instanceof RegExp) {

@@ -1179,5 +1300,5 @@ rule.type = 'pattern';

return rule.type || 'string';
}
};
getValidationMethod(rule) {
_proto.getValidationMethod = function getValidationMethod(rule) {
if (typeof rule.validator === 'function') {

@@ -1187,4 +1308,4 @@ return rule.validator;

const keys = Object.keys(rule);
const messageIndex = keys.indexOf('message');
var keys = Object.keys(rule);
var messageIndex = keys.indexOf('message');

@@ -1200,7 +1321,20 @@ if (messageIndex !== -1) {

return validators[this.getType(rule)] || undefined;
};
return Schema;
}();
Schema.register = function register(type, validator) {
if (typeof validator !== 'function') {
throw new Error('Cannot register a validator by type, validator is not a function');
}
}
validators[type] = validator;
};
Schema.warning = warning;
Schema.messages = messages;
Schema.validators = validators;
export { Schema as default };
//# sourceMappingURL=index.js.map
{
"name": "async-validator",
"description": "validate form asynchronous",
"version": "4.0.5",
"version": "4.0.6",
"license": "MIT",

@@ -6,0 +6,0 @@ "files": [

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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