Series of articles:
1、async-validator source code study (I): documentation translation
2、async-validator source code study notes (II): directory structure
The main implementation of rule is the checksum rule, the file structure is the following figure:
I. Introduction to Rule Catalog Files
Among the documents:
-
declare const _default: {
-
required: import("..").ExecuteRule;
-
whitespace: import("..").ExecuteRule;
-
type: import("..").ExecuteRule;
-
range: import("..").ExecuteRule;
-
enum: import("..").ExecuteRule;
-
pattern: import("..").ExecuteRule;
-
};
-
export default _default;
is a unified export manager for rule directories, mainly for errorsThe array addsThe corresponding error.
Documentation:
-
import { ExecuteRule } from '../interface';
-
declare const required: ExecuteRule;
-
export default required;
The main function is to check the rules for required fields.
where ExecuteRule is from the file
-
// Excerpts from some of them
-
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 The validation messages.
-
*/
ExecuteRule is a uniformly definedfunction class (math.)Type aliases that unify the types of parameters and return values passed to a function. Equivalent:
declare const required(rule, value, source, errors, options, type)
The parameters within the method and their significance are listed below:
- @param rule The rule to verify
- @param value The current value of the field to be checked.
- @param source Fields to be verified
- @param errors An array of errors to add to this check.
- @param options Checksum options
- @param verified messages
-
import { ExecuteRule } from '../interface';
-
declare const type: ExecuteRule;
-
export default type;
The type of the checksum value, the possible types are: integer, float,array、regexp、object、method、email、number、data、url、hex
-
import { ExecuteRule } from '../interface';
-
declare const range: ExecuteRule;
-
export default range;
Check that the rules for the maximum and minimum reasonable intervals are satisfied
-
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 The validation messages.
-
*/
-
declare const whitespace: ExecuteRule;
-
export default whitespace;
Rules for checking whitespace characters
-
import { ExecuteRule } from '../interface';
-
declare const enumerable: ExecuteRule;
-
export default enumerable;
Whether the checksum value existsenumerated valueRules in the list
-
import { ExecuteRule } from '../interface';
-
declare const pattern: ExecuteRule;
-
export default pattern;
Rules for calibrating regular expressions
II. RULE APPLICATIONS
Define the rule unit format in
-
export interface RuleItem {
-
type?: RuleType; //typology
-
required?: boolean; //Whether or not it is empty
-
pattern?: RegExp | string; //regular (figure in geometry)
-
min?: number; // Minimum value or length
-
max?: number; //Maximum value or length
-
len?: number; // lengths
-
enum?: Array<string | number | boolean | null | undefined>; //Rules for checking whether a value exists in a list of enumerated values
-
whitespace?: boolean; //Blank or not
-
fields?: Record<string, Rule>;//Deep Listening Properties and Rules
-
options?: ValidateOption;//options (as in computer software settings)
-
defaultField?: Rule; //Validating the internal value of an attribute
-
transform?: (value: Value) => Value; //pre-check conversion
-
message?: string | ((a?: string) => string);//Information Alerts
-
//asynchronous calibration
-
asyncValidator?: (rule: InternalRuleItem, value: Value, callback: (error?: string | Error) => void, source: Values, options: ValidateOption) => void | Promise<void>;
-
//synchronized verification
-
validator?: (rule: InternalRuleItem, value: Value, callback: (error?: string | Error) => void, source: Values, options: ValidateOption) => SyncValidateResult | void;
-
}
-
// A Rule can be an object or an array of such objects.
-
export declare type Rule = RuleItem | RuleItem[];
rule is the checksum rule corresponding to this field:
-
{
-
field: "name",
-
fullField: "name",
-
message: "Name is required.",
-
required: false,
-
type: "string",
-
validator: ƒ required$1(rule, value, callback, source, options)
-
}
value is the value of this field: e.g. Xiaoming
source is the entire source object to be verified:
-
{
-
name: 'Little Ming',
-
info: {
-
age: 17,
-
}
-
}
errors is the array of errors to be added to this check, assuming there are no previous errors, then errors is [], if there are already some previous errors, then the format is as follows:
-
[
-
{
-
message: 'Age out of range',
-
field: '',
-
}
-
]
options are the options for this field when it is validated, in the following format when the message attribute is the default value:
-
{
-
firstFields: true,
-
messages: {
-
array: {len: "%s must be exactly %s in length", min: "%s cannot be less than %s in length", max: "%s cannot be greater than %s in length", range: "%s must be between %s and %s in length"},
-
clone: ƒ clone(),
-
date: {format: "%s date %s is invalid for format %s", parse: "%s date could not be parsed, %s is invalid ", invalid: "%s date %s is invalid"},
-
default: "Validation error on field %s",
-
enum: "%s must be one of %s",
-
number: {len: "%s must equal %s", min: "%s cannot be less than %s", max: "%s cannot be greater than %s", range: "%s must be between %s and %s"},
-
pattern: {mismatch: "%s value %s does not match pattern %s"},
-
required: "%s is required",
-
string: {len: "%s must be exactly %s characters", min: "%s must be at least %s characters", max: "%s cannot be longer than %s characters", range: "%s must be between %s and %s characters"},
-
types: {string: "%s is not a %s", method: "%s is not a %s (function)", array: "%s is not an %s", object: "%s is not an %s", number: "%s is not a %s", …},
-
whitespace: "%s cannot be empty",
-
}
-
}
III. Project development applications
Ways to write validation rule rule in real project development:
-
const rules = {
-
// deep calibration1
-
address: {
-
type: 'object',
-
required: true,
-
fields: {
-
//Deep-checking the street attribute
-
street: { type: 'string', required: true },
-
city: { type: 'string', required: true },
-
zip: {
-
type: 'string',
-
required: true,
-
len: 8,
-
message: 'invalid zip',
-
},
-
},
-
},
-
//calibration2 array form
-
username: [
-
{
-
type: 'string',
-
required: true,
-
whitespace: true,
-
transform(value) {
-
return value.trim()
-
},
-
message: 'Username cannot be a space',
-
// asynchronous calibration
-
asyncValidator: (rule, value) => {
-
return new Promise((resolve, reject) => {
-
setTimeout(() => {
-
if (value != '') {
-
resolve()
-
} else {
-
reject('error')
-
}
-
}, 2000)
-
})
-
},
-
},
-
{
-
type: 'string',
-
min: 3,
-
max: 20,
-
message: 'Length 3-20 bits',
-
},
-
],
-
}