2020-07-13 15:55:46 +00:00
|
|
|
import RawAsyncValidator from 'async-validator';
|
|
|
|
import { cloneVNode } from 'vue';
|
|
|
|
import { warning } from '../../vc-util/warning';
|
|
|
|
|
|
|
|
import { setValues } from './valueUtil';
|
|
|
|
import { defaultValidateMessages } from './messages';
|
|
|
|
import { isValidElement } from '../../_util/props-util';
|
2021-06-28 03:21:23 +00:00
|
|
|
import type { InternalNamePath, RuleError, RuleObject, ValidateOptions } from '../interface';
|
2020-07-13 15:55:46 +00:00
|
|
|
|
|
|
|
// Remove incorrect original ts define
|
2020-10-15 09:58:05 +00:00
|
|
|
const AsyncValidator: any = RawAsyncValidator;
|
2020-07-13 15:55:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace with template.
|
|
|
|
* `I'm ${name}` + { name: 'bamboo' } = I'm bamboo
|
|
|
|
*/
|
2020-10-15 09:58:05 +00:00
|
|
|
function replaceMessage(template: string, kv: Record<string, string>): string {
|
2021-06-28 03:21:23 +00:00
|
|
|
return template.replace(/\$\{\w+\}/g, (str: string) => {
|
2020-07-13 15:55:46 +00:00
|
|
|
const key = str.slice(2, -1);
|
|
|
|
return kv[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-15 09:58:05 +00:00
|
|
|
async function validateRule(
|
|
|
|
name: string,
|
|
|
|
value: any,
|
|
|
|
rule: RuleObject,
|
|
|
|
options: ValidateOptions,
|
|
|
|
messageVariables?: Record<string, string>,
|
|
|
|
): Promise<string[]> {
|
2020-07-13 15:55:46 +00:00
|
|
|
const cloneRule = { ...rule };
|
2021-06-28 03:21:23 +00:00
|
|
|
|
|
|
|
// Bug of `async-validator`
|
|
|
|
delete (cloneRule as any).ruleIndex;
|
|
|
|
|
2020-07-13 15:55:46 +00:00
|
|
|
// We should special handle array validate
|
2020-10-15 09:58:05 +00:00
|
|
|
let subRuleField: RuleObject = null;
|
2020-07-13 15:55:46 +00:00
|
|
|
if (cloneRule && cloneRule.type === 'array' && cloneRule.defaultField) {
|
|
|
|
subRuleField = cloneRule.defaultField;
|
|
|
|
delete cloneRule.defaultField;
|
|
|
|
}
|
|
|
|
|
|
|
|
const validator = new AsyncValidator({
|
|
|
|
[name]: [cloneRule],
|
|
|
|
});
|
|
|
|
|
2021-06-28 03:21:23 +00:00
|
|
|
const messages = setValues({}, defaultValidateMessages, options.validateMessages);
|
2020-07-13 15:55:46 +00:00
|
|
|
validator.messages(messages);
|
|
|
|
|
|
|
|
let result = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Promise.resolve(validator.validate({ [name]: value }, { ...options }));
|
|
|
|
} catch (errObj) {
|
|
|
|
if (errObj.errors) {
|
2020-10-15 09:58:05 +00:00
|
|
|
result = errObj.errors.map(({ message }, index: number) =>
|
2020-07-13 15:55:46 +00:00
|
|
|
// Wrap VueNode with `key`
|
|
|
|
isValidElement(message) ? cloneVNode(message, { key: `error_${index}` }) : message,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.error(errObj);
|
2020-10-15 09:58:05 +00:00
|
|
|
result = [(messages.default as () => string)()];
|
2020-07-13 15:55:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.length && subRuleField) {
|
2020-10-15 09:58:05 +00:00
|
|
|
const subResults: string[][] = await Promise.all(
|
|
|
|
(value as any[]).map((subValue: any, i: number) =>
|
2020-07-13 15:55:46 +00:00
|
|
|
validateRule(`${name}.${i}`, subValue, subRuleField, options, messageVariables),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return subResults.reduce((prev, errors) => [...prev, ...errors], []);
|
|
|
|
}
|
|
|
|
|
2021-06-28 03:21:23 +00:00
|
|
|
// Replace message with variables
|
|
|
|
const kv = {
|
|
|
|
...(rule as Record<string, string | number>),
|
|
|
|
name,
|
|
|
|
enum: (rule.enum || []).join(', '),
|
|
|
|
...messageVariables,
|
|
|
|
};
|
|
|
|
|
|
|
|
const fillVariableResult = result.map(error => {
|
|
|
|
if (typeof error === 'string') {
|
|
|
|
return replaceMessage(error, kv);
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
});
|
|
|
|
|
|
|
|
return fillVariableResult;
|
2020-07-13 15:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We use `async-validator` to validate the value.
|
|
|
|
* But only check one value in a time to avoid namePath validate issue.
|
|
|
|
*/
|
2020-10-15 09:58:05 +00:00
|
|
|
export function validateRules(
|
|
|
|
namePath: InternalNamePath,
|
|
|
|
value: any,
|
|
|
|
rules: RuleObject[],
|
|
|
|
options: ValidateOptions,
|
|
|
|
validateFirst: boolean | 'parallel',
|
|
|
|
messageVariables?: Record<string, string>,
|
|
|
|
) {
|
2020-07-13 15:55:46 +00:00
|
|
|
const name = namePath.join('.');
|
|
|
|
|
|
|
|
// Fill rule with context
|
2021-06-28 03:21:23 +00:00
|
|
|
const filledRules: RuleObject[] = rules
|
|
|
|
.map((currentRule, ruleIndex) => {
|
|
|
|
const originValidatorFunc = currentRule.validator;
|
|
|
|
const cloneRule = {
|
|
|
|
...currentRule,
|
|
|
|
ruleIndex,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Replace validator if needed
|
|
|
|
if (originValidatorFunc) {
|
|
|
|
cloneRule.validator = (rule: RuleObject, val: any, callback: (error?: string) => void) => {
|
|
|
|
let hasPromise = false;
|
|
|
|
|
|
|
|
// Wrap callback only accept when promise not provided
|
|
|
|
const wrappedCallback = (...args: string[]) => {
|
|
|
|
// Wait a tick to make sure return type is a promise
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
warning(
|
|
|
|
!hasPromise,
|
|
|
|
'Your validator function has already return a promise. `callback` will be ignored.',
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!hasPromise) {
|
|
|
|
callback(...args);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get promise
|
|
|
|
const promise = originValidatorFunc(rule, val, wrappedCallback);
|
|
|
|
hasPromise =
|
|
|
|
promise && typeof promise.then === 'function' && typeof promise.catch === 'function';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 1. Use promise as the first priority.
|
|
|
|
* 2. If promise not exist, use callback with warning instead
|
|
|
|
*/
|
|
|
|
warning(hasPromise, '`callback` is deprecated. Please return a promise instead.');
|
|
|
|
|
|
|
|
if (hasPromise) {
|
|
|
|
(promise as Promise<void>)
|
|
|
|
.then(() => {
|
|
|
|
callback();
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
callback(err || ' ');
|
|
|
|
});
|
|
|
|
}
|
2020-07-13 15:55:46 +00:00
|
|
|
};
|
2021-06-28 03:21:23 +00:00
|
|
|
}
|
2020-07-13 15:55:46 +00:00
|
|
|
|
2021-06-28 03:21:23 +00:00
|
|
|
return cloneRule;
|
|
|
|
})
|
|
|
|
.sort(({ warningOnly: w1, ruleIndex: i1 }, { warningOnly: w2, ruleIndex: i2 }) => {
|
|
|
|
if (!!w1 === !!w2) {
|
|
|
|
// Let keep origin order
|
|
|
|
return i1 - i2;
|
|
|
|
}
|
2020-07-13 15:55:46 +00:00
|
|
|
|
2021-06-28 03:21:23 +00:00
|
|
|
if (w1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Do validate rules
|
|
|
|
let summaryPromise: Promise<RuleError[]>;
|
2020-07-13 15:55:46 +00:00
|
|
|
|
|
|
|
if (validateFirst === true) {
|
|
|
|
// >>>>> Validate by serialization
|
2021-06-28 03:21:23 +00:00
|
|
|
summaryPromise = new Promise(async (resolve, reject) => {
|
2020-07-13 15:55:46 +00:00
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
for (let i = 0; i < filledRules.length; i += 1) {
|
2021-06-28 03:21:23 +00:00
|
|
|
const rule = filledRules[i];
|
|
|
|
const errors = await validateRule(name, value, rule, options, messageVariables);
|
2020-07-13 15:55:46 +00:00
|
|
|
if (errors.length) {
|
2021-06-28 03:21:23 +00:00
|
|
|
reject([{ errors, rule }]);
|
2020-07-13 15:55:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* eslint-enable */
|
|
|
|
|
|
|
|
resolve([]);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// >>>>> Validate by parallel
|
2021-06-28 03:21:23 +00:00
|
|
|
const rulePromises: Promise<RuleError>[] = filledRules.map(rule =>
|
|
|
|
validateRule(name, value, rule, options, messageVariables).then(errors => ({ errors, rule })),
|
2020-07-13 15:55:46 +00:00
|
|
|
);
|
|
|
|
|
2021-06-23 15:08:16 +00:00
|
|
|
summaryPromise = (
|
|
|
|
validateFirst ? finishOnFirstFailed(rulePromises) : finishOnAllFailed(rulePromises)
|
2021-06-28 03:21:23 +00:00
|
|
|
).then((errors: RuleError[]): RuleError[] | Promise<RuleError[]> => {
|
|
|
|
// Always change to rejection for Field to catch
|
|
|
|
return Promise.reject<RuleError[]>(errors);
|
2020-07-13 15:55:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal catch error to avoid console error log.
|
|
|
|
summaryPromise.catch(e => e);
|
|
|
|
|
|
|
|
return summaryPromise;
|
|
|
|
}
|
|
|
|
|
2021-06-28 03:21:23 +00:00
|
|
|
async function finishOnAllFailed(rulePromises: Promise<RuleError>[]): Promise<RuleError[]> {
|
|
|
|
return Promise.all(rulePromises).then(
|
|
|
|
(errorsList: RuleError[]): RuleError[] | Promise<RuleError[]> => {
|
|
|
|
const errors: RuleError[] = [].concat(...errorsList);
|
2020-07-13 15:55:46 +00:00
|
|
|
|
2021-06-28 03:21:23 +00:00
|
|
|
return errors;
|
|
|
|
},
|
|
|
|
);
|
2020-07-13 15:55:46 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 03:21:23 +00:00
|
|
|
async function finishOnFirstFailed(rulePromises: Promise<RuleError>[]): Promise<RuleError[]> {
|
2020-07-13 15:55:46 +00:00
|
|
|
let count = 0;
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
rulePromises.forEach(promise => {
|
2021-06-28 03:21:23 +00:00
|
|
|
promise.then(ruleError => {
|
|
|
|
if (ruleError.errors.length) {
|
|
|
|
resolve([ruleError]);
|
2020-07-13 15:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
if (count === rulePromises.length) {
|
|
|
|
resolve([]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|