Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

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
3.5.2
to
4.0.0
+1
dist-types/__tests__/any.spec.d.ts
export {};
import { InternalRuleItem, InternalValidateMessages, RuleItem, Rules, ValidateCallback, ValidateMessages, ValidateOption, Values, SyncErrorType } from './interface';
export * from './interface';
/**
* Encapsulates a validation schema.
*
* @param descriptor An object declaring validation rules
* for this schema.
*/
declare class Schema {
static register: (type: string, validator: any) => void;
static warning: (type: string, errors: SyncErrorType[]) => void;
static messages: InternalValidateMessages;
static validators: {
string: import("./interface").ExecuteValidator;
method: import("./interface").ExecuteValidator;
number: import("./interface").ExecuteValidator;
boolean: import("./interface").ExecuteValidator;
regexp: import("./interface").ExecuteValidator;
integer: import("./interface").ExecuteValidator;
float: import("./interface").ExecuteValidator;
array: import("./interface").ExecuteValidator;
object: import("./interface").ExecuteValidator;
enum: import("./interface").ExecuteValidator;
pattern: import("./interface").ExecuteValidator;
date: import("./interface").ExecuteValidator;
url: import("./interface").ExecuteValidator;
hex: import("./interface").ExecuteValidator;
email: import("./interface").ExecuteValidator;
required: import("./interface").ExecuteValidator;
any: import("./interface").ExecuteValidator;
};
rules: Record<string, RuleItem[]>;
_messages: InternalValidateMessages;
constructor(descriptor: Rules);
define(rules: Rules): void;
messages(messages?: ValidateMessages): InternalValidateMessages;
validate(source: Values, option?: ValidateOption, callback?: ValidateCallback): Promise<Values>;
validate(source: Values, callback: ValidateCallback): Promise<Values>;
validate(source: Values): Promise<Values>;
getType(rule: InternalRuleItem): import("./interface").RuleType;
getValidationMethod(rule: InternalRuleItem): ((rule: InternalRuleItem, value: any, callback: (error?: string | Error) => void, source: Values, options: ValidateOption) => void | import("./interface").SyncValidateResult) | import("./interface").ExecuteValidator;
}
export default Schema;
export declare type RuleType = 'string' | 'number' | 'boolean' | 'method' | 'regexp' | 'integer' | 'float' | 'array' | 'object' | 'enum' | 'date' | 'url' | 'hex' | 'email' | 'pattern' | 'any';
export interface ValidateOption {
suppressWarning?: boolean;
first?: boolean;
firstFields?: boolean | string[];
messages?: Partial<ValidateMessages>;
/** The name of rules need to be trigger. Will validate all rules if leave empty */
keys?: string[];
error?: (rule: InternalRuleItem, message: string) => ValidateError;
}
export declare type SyncErrorType = Error | string;
export declare type SyncValidateResult = boolean | SyncErrorType | SyncErrorType[];
export declare type ValidateResult = void | Promise<void> | SyncValidateResult;
export interface RuleItem {
type?: RuleType;
required?: boolean;
pattern?: RegExp | string;
min?: number;
max?: number;
len?: number;
enum?: Array<string | number | boolean | null | undefined>;
whitespace?: boolean;
fields?: Record<string, Rule>;
options?: ValidateOption;
defaultField?: Rule;
transform?: (value: Value) => Value;
message?: string | ((a?: string) => string);
asyncValidator?: (rule: InternalRuleItem, value: Value, callback: (error?: string | Error) => void, source: Values, options: ValidateOption) => void | Promise<void>;
validator?: (rule: InternalRuleItem, value: Value, callback: (error?: string | Error) => void, source: Values, options: ValidateOption) => SyncValidateResult | void;
}
export declare type Rule = RuleItem | RuleItem[];
export declare type Rules = Record<string, Rule>;
/**
* 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.
* @param type Rule type
*/
export declare type ExecuteRule = (rule: InternalRuleItem, value: Value, source: Values, errors: string[], options: ValidateOption, type?: string) => void;
/**
* 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.
*/
export declare type ExecuteValidator = (rule: InternalRuleItem, value: Value, callback: (error?: string[]) => void, source: Values, options: ValidateOption) => void;
declare type ValidateMessage<T extends any[] = unknown[]> = string | ((...args: T) => string);
declare type FullField = string | undefined;
declare type EnumString = string | undefined;
declare type Pattern = string | RegExp | undefined;
declare type Range = number | undefined;
declare type Type = string | undefined;
export interface ValidateMessages {
default?: ValidateMessage;
required?: ValidateMessage<[FullField]>;
enum?: ValidateMessage<[FullField, EnumString]>;
whitespace?: ValidateMessage<[FullField]>;
date?: {
format?: ValidateMessage;
parse?: ValidateMessage;
invalid?: ValidateMessage;
};
types?: {
string?: ValidateMessage<[FullField, Type]>;
method?: ValidateMessage<[FullField, Type]>;
array?: ValidateMessage<[FullField, Type]>;
object?: ValidateMessage<[FullField, Type]>;
number?: ValidateMessage<[FullField, Type]>;
date?: ValidateMessage<[FullField, Type]>;
boolean?: ValidateMessage<[FullField, Type]>;
integer?: ValidateMessage<[FullField, Type]>;
float?: ValidateMessage<[FullField, Type]>;
regexp?: ValidateMessage<[FullField, Type]>;
email?: ValidateMessage<[FullField, Type]>;
url?: ValidateMessage<[FullField, Type]>;
hex?: ValidateMessage<[FullField, Type]>;
};
string?: {
len?: ValidateMessage<[FullField, Range]>;
min?: ValidateMessage<[FullField, Range]>;
max?: ValidateMessage<[FullField, Range]>;
range?: ValidateMessage<[FullField, Range, Range]>;
};
number?: {
len?: ValidateMessage<[FullField, Range]>;
min?: ValidateMessage<[FullField, Range]>;
max?: ValidateMessage<[FullField, Range]>;
range?: ValidateMessage<[FullField, Range, Range]>;
};
array?: {
len?: ValidateMessage<[FullField, Range]>;
min?: ValidateMessage<[FullField, Range]>;
max?: ValidateMessage<[FullField, Range]>;
range?: ValidateMessage<[FullField, Range, Range]>;
};
pattern?: {
mismatch?: ValidateMessage<[FullField, Value, Pattern]>;
};
}
export interface InternalValidateMessages extends ValidateMessages {
clone: () => InternalValidateMessages;
}
export declare type Value = any;
export declare type Values = Record<string, Value>;
export interface ValidateError {
message?: string;
fieldValue?: Value;
field?: string;
}
export declare type ValidateFieldsError = Record<string, ValidateError[]>;
export declare type ValidateCallback = (errors: ValidateError[] | null, fields: ValidateFieldsError | Values) => void;
export interface RuleValuePackage {
rule: InternalRuleItem;
value: Value;
source: Values;
field: string;
}
export interface InternalRuleItem extends Omit<RuleItem, 'validator'> {
field?: string;
fullField?: string;
fullFields?: string[];
validator?: RuleItem['validator'] | ExecuteValidator;
}
export {};
import { InternalValidateMessages } from './interface';
export declare function newMessages(): InternalValidateMessages;
export declare const messages: InternalValidateMessages;
import { ExecuteRule } from '../interface';
declare const enumerable: ExecuteRule;
export default enumerable;
declare const _default: {
required: import("..").ExecuteRule;
whitespace: import("..").ExecuteRule;
type: import("..").ExecuteRule;
range: import("..").ExecuteRule;
enum: import("..").ExecuteRule;
pattern: import("..").ExecuteRule;
};
export default _default;
import { ExecuteRule } from '../interface';
declare const pattern: ExecuteRule;
export default pattern;
import { ExecuteRule } from '../interface';
declare const range: ExecuteRule;
export default range;
import { ExecuteRule } from '../interface';
declare const required: ExecuteRule;
export default required;
import { ExecuteRule } from '../interface';
declare const type: ExecuteRule;
export default type;
import { ExecuteRule } from '../interface';
/**
* Rule for validating whitespace.
*
* @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.
*/
declare const whitespace: ExecuteRule;
export default whitespace;
import { ValidateError, ValidateOption, RuleValuePackage, InternalRuleItem, SyncErrorType, Value, Values } from './interface';
export declare let warning: (type: string, errors: SyncErrorType[]) => void;
export declare function convertFieldsError(errors: ValidateError[]): Record<string, ValidateError[]>;
export declare function format(template: ((...args: any[]) => string) | string, ...args: any[]): string;
export declare function isEmptyValue(value: Value, type?: string): boolean;
export declare function isEmptyObject(obj: object): boolean;
export declare class AsyncValidationError extends Error {
errors: ValidateError[];
fields: Record<string, ValidateError[]>;
constructor(errors: ValidateError[], fields: Record<string, ValidateError[]>);
}
declare type ValidateFunc = (data: RuleValuePackage, doIt: (errors: ValidateError[]) => void) => void;
export declare function asyncMap(objArr: Record<string, RuleValuePackage[]>, option: ValidateOption, func: ValidateFunc, callback: (errors: ValidateError[]) => void, source: Values): Promise<Values>;
export declare function complementError(rule: InternalRuleItem, source: Values): (oe: ValidateError | (() => string) | string) => ValidateError;
export declare function deepMerge<T extends object>(target: T, source: Partial<T>): T;
export {};
import { ExecuteValidator } from '../interface';
declare const any: ExecuteValidator;
export default any;
import { ExecuteValidator } from '../interface';
declare const array: ExecuteValidator;
export default array;
import { ExecuteValidator } from '../interface';
declare const boolean: ExecuteValidator;
export default boolean;
import { ExecuteValidator } from '../interface';
declare const date: ExecuteValidator;
export default date;
import { ExecuteValidator } from '../interface';
declare const enumerable: ExecuteValidator;
export default enumerable;
import { ExecuteValidator } from '../interface';
declare const floatFn: ExecuteValidator;
export default floatFn;
declare const _default: {
string: import("..").ExecuteValidator;
method: import("..").ExecuteValidator;
number: import("..").ExecuteValidator;
boolean: import("..").ExecuteValidator;
regexp: import("..").ExecuteValidator;
integer: import("..").ExecuteValidator;
float: import("..").ExecuteValidator;
array: import("..").ExecuteValidator;
object: import("..").ExecuteValidator;
enum: import("..").ExecuteValidator;
pattern: import("..").ExecuteValidator;
date: import("..").ExecuteValidator;
url: import("..").ExecuteValidator;
hex: import("..").ExecuteValidator;
email: import("..").ExecuteValidator;
required: import("..").ExecuteValidator;
any: import("..").ExecuteValidator;
};
export default _default;
import { ExecuteValidator } from '../interface';
declare const integer: ExecuteValidator;
export default integer;
import { ExecuteValidator } from '../interface';
declare const method: ExecuteValidator;
export default method;
import { ExecuteValidator } from '../interface';
declare const number: ExecuteValidator;
export default number;
import { ExecuteValidator } from '../interface';
declare const object: ExecuteValidator;
export default object;
import { ExecuteValidator } from '../interface';
declare const pattern: ExecuteValidator;
export default pattern;
import { ExecuteValidator } from '../interface';
declare const regexp: ExecuteValidator;
export default regexp;
import { ExecuteValidator } from '../interface';
declare const required: ExecuteValidator;
export default required;
import { ExecuteValidator } from '../interface';
declare const string: ExecuteValidator;
export default string;
import { ExecuteValidator } from '../interface';
declare const type: ExecuteValidator;
export default type;
+190
-362

@@ -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 @@

// Type definitions for async-validator 3.0.4
// Project: http://github.com/yiminghe/async-validator
// Definitions by: iamdhj <https://github.com/iamdhj>
// TypeScript Version: 3.6.2
export default class {
constructor(rule: Rules);
/**
* Validate source
* @param source The object to validate (required)
* @param options An object describing processing options for the validation
* @param callback A callback function to invoke when validation completes
* @returns Promise
*/
validate(
source: ValidateSource,
options?: ValidateOption,
callback?: (errors: ErrorList, fields: FieldErrorList) => void,
): Promise<void>;
}
export type RuleType =
| 'string'
| 'number'
| 'boolean'
| 'method'
| 'regexp'
| 'integer'
| 'float'
| 'array'
| 'object'
| 'enum'
| 'date'
| 'url'
| 'hex'
| 'email'
| 'any';
export interface RuleItem {
type?: RuleType; // default type is 'string'
required?: boolean;
pattern?: RegExp | string;
min?: number; // Range of type 'string' and 'array'
max?: number; // Range of type 'string' and 'array'
len?: number; // Length of type 'string' and 'array'
enum?: Array<string | number | boolean | null | undefined>; // possible values of type 'enum'
whitespace?: boolean;
fields?: Rules; // ignore when without required
options?: ValidateOption;
defaultField?: RuleItem; // 'object' or 'array' containing validation rules
transform?: (value: any) => any;
message?: string | (() => string);
asyncValidator?: (
rule: Rules,
value: any,
callback: (error: string | string[] | void) => void,
source: ValidateSource,
options: ValidateOption,
) => void | Promise<void>;
validator?: (
rule: Rules,
value: any,
callback: (error: string | string[] | void) => void,
source: ValidateSource,
options: ValidateOption,
) => boolean | Error | Error[];
}
export interface Rules {
[field: string]: RuleItem | RuleItem[];
}
export interface ValidateSource {
[field: string]: any;
}
export interface ValidateOption {
// whether to suppress internal warning
suppressWarning?: boolean;
// when the first validation rule generates an error stop processed
first?: boolean;
// when the first validation rule of the specified field generates an error stop the field processed, 'true' means all fields.
firstFields?: boolean | string[];
}
export interface ValidateError {
message: string;
field: string;
}
export type ErrorList = ValidateError[];
export interface FieldErrorList {
[field: string]: ValidateError[];
}

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display