SmartStorage/uni_modules/uview-ui/libs/util/async-validator.js

1499 lines
42 KiB
JavaScript
Raw Normal View History

2023-12-20 15:15:23 +08:00
function _extends() {
2024-08-30 17:45:46 +08:00
_extends =
Object.assign ||
function (target) {
for (let i = 1; i < arguments.length; i++) {
const source = arguments[i];
for (const key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
return target;
};
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
return _extends.apply(this, arguments);
2023-12-20 15:15:23 +08:00
}
/* eslint no-console:0 */
2024-08-30 17:45:46 +08:00
const formatRegExp = /%[sdj%]/g;
let 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"
) {
2023-12-20 15:15:23 +08:00
warning = function warning(type, errors) {
2024-08-30 17:45:46 +08:00
if (typeof console !== "undefined" && console.warn) {
if (errors.every((e) => typeof e === "string")) {
// console.warn(type, errors)
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
};
2023-12-20 15:15:23 +08:00
}
function convertFieldsError(errors) {
2024-08-30 17:45:46 +08:00
if (!errors || !errors.length) return null;
const fields = {};
2023-12-20 15:15:23 +08:00
errors.forEach((error) => {
2024-08-30 17:45:46 +08:00
const { field } = error;
fields[field] = fields[field] || [];
fields[field].push(error);
});
return fields;
2023-12-20 15:15:23 +08:00
}
function format() {
2024-08-30 17:45:46 +08:00
for (
var _len = arguments.length, args = new Array(_len), _key = 0;
_key < _len;
_key++
) {
args[_key] = arguments[_key];
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
let i = 1;
const f = args[0];
const len = args.length;
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (typeof f === "function") {
return f.apply(null, args.slice(1));
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
if (typeof f === "string") {
2023-12-20 15:15:23 +08:00
let str = String(f).replace(formatRegExp, (x) => {
2024-08-30 17:45:46 +08:00
if (x === "%%") {
return "%";
2023-12-20 15:15:23 +08:00
}
if (i >= len) {
2024-08-30 17:45:46 +08:00
return x;
2023-12-20 15:15:23 +08:00
}
switch (x) {
2024-08-30 17:45:46 +08:00
case "%s":
return String(args[i++]);
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
case "%d":
return Number(args[i++]);
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
case "%j":
try {
return JSON.stringify(args[i++]);
} catch (_) {
return "[Circular]";
}
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
default:
return x;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
});
2023-12-20 15:15:23 +08:00
for (let arg = args[i]; i < len; arg = args[++i]) {
2024-08-30 17:45:46 +08:00
str += ` ${arg}`;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
return str;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
return f;
2023-12-20 15:15:23 +08:00
}
function isNativeStringType(type) {
2024-08-30 17:45:46 +08:00
return (
type === "string" ||
type === "url" ||
type === "hex" ||
type === "email" ||
type === "pattern"
);
2023-12-20 15:15:23 +08:00
}
function isEmptyValue(value, type) {
if (value === undefined || value === null) {
2024-08-30 17:45:46 +08:00
return true;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
if (type === "array" && Array.isArray(value) && !value.length) {
return true;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
if (isNativeStringType(type) && typeof value === "string" && !value) {
return true;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
return false;
2023-12-20 15:15:23 +08:00
}
function asyncParallelArray(arr, func, callback) {
2024-08-30 17:45:46 +08:00
const results = [];
let total = 0;
const arrLength = arr.length;
2023-12-20 15:15:23 +08:00
function count(errors) {
2024-08-30 17:45:46 +08:00
results.push.apply(results, errors);
total++;
2023-12-20 15:15:23 +08:00
if (total === arrLength) {
2024-08-30 17:45:46 +08:00
callback(results);
2023-12-20 15:15:23 +08:00
}
}
arr.forEach((a) => {
2024-08-30 17:45:46 +08:00
func(a, count);
});
2023-12-20 15:15:23 +08:00
}
function asyncSerialArray(arr, func, callback) {
2024-08-30 17:45:46 +08:00
let index = 0;
const arrLength = arr.length;
2023-12-20 15:15:23 +08:00
function next(errors) {
if (errors && errors.length) {
2024-08-30 17:45:46 +08:00
callback(errors);
return;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
const original = index;
index += 1;
2023-12-20 15:15:23 +08:00
if (original < arrLength) {
2024-08-30 17:45:46 +08:00
func(arr[original], next);
2023-12-20 15:15:23 +08:00
} else {
2024-08-30 17:45:46 +08:00
callback([]);
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
next([]);
2023-12-20 15:15:23 +08:00
}
function flattenObjArr(objArr) {
2024-08-30 17:45:46 +08:00
const ret = [];
2023-12-20 15:15:23 +08:00
Object.keys(objArr).forEach((k) => {
2024-08-30 17:45:46 +08:00
ret.push.apply(ret, objArr[k]);
});
return ret;
2023-12-20 15:15:23 +08:00
}
function asyncMap(objArr, option, func, callback) {
if (option.first) {
const _pending = new Promise((resolve, reject) => {
const next = function next(errors) {
2024-08-30 17:45:46 +08:00
callback(errors);
return errors.length
? reject({
errors,
fields: convertFieldsError(errors),
})
: resolve();
};
const flattenArr = flattenObjArr(objArr);
asyncSerialArray(flattenArr, func, next);
});
_pending.catch((e) => e);
return _pending;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
let firstFields = option.firstFields || [];
2023-12-20 15:15:23 +08:00
if (firstFields === true) {
2024-08-30 17:45:46 +08:00
firstFields = Object.keys(objArr);
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
const objArrKeys = Object.keys(objArr);
const objArrLength = objArrKeys.length;
let total = 0;
const results = [];
2023-12-20 15:15:23 +08:00
const pending = new Promise((resolve, reject) => {
const next = function next(errors) {
2024-08-30 17:45:46 +08:00
results.push.apply(results, errors);
total++;
2023-12-20 15:15:23 +08:00
if (total === objArrLength) {
2024-08-30 17:45:46 +08:00
callback(results);
return results.length
? reject({
errors: results,
fields: convertFieldsError(results),
})
: resolve();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
};
2023-12-20 15:15:23 +08:00
if (!objArrKeys.length) {
2024-08-30 17:45:46 +08:00
callback(results);
resolve();
2023-12-20 15:15:23 +08:00
}
objArrKeys.forEach((key) => {
2024-08-30 17:45:46 +08:00
const arr = objArr[key];
2023-12-20 15:15:23 +08:00
if (firstFields.indexOf(key) !== -1) {
2024-08-30 17:45:46 +08:00
asyncSerialArray(arr, func, next);
2023-12-20 15:15:23 +08:00
} else {
2024-08-30 17:45:46 +08:00
asyncParallelArray(arr, func, next);
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
});
});
pending.catch((e) => e);
return pending;
2023-12-20 15:15:23 +08:00
}
function complementError(rule) {
return function (oe) {
if (oe && oe.message) {
2024-08-30 17:45:46 +08:00
oe.field = oe.field || rule.fullField;
return oe;
2023-12-20 15:15:23 +08:00
}
return {
2024-08-30 17:45:46 +08:00
message: typeof oe === "function" ? oe() : oe,
field: oe.field || rule.fullField,
};
};
2023-12-20 15:15:23 +08:00
}
function deepMerge(target, source) {
if (source) {
for (const s in source) {
if (source.hasOwnProperty(s)) {
2024-08-30 17:45:46 +08:00
const value = source[s];
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (
typeof value === "object" &&
typeof target[s] === "object"
) {
target[s] = { ...target[s], ...value };
2023-12-20 15:15:23 +08:00
} else {
2024-08-30 17:45:46 +08:00
target[s] = value;
2023-12-20 15:15:23 +08:00
}
}
}
}
2024-08-30 17:45:46 +08:00
return target;
2023-12-20 15:15:23 +08:00
}
/**
* 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) {
2024-08-30 17:45:46 +08:00
if (
rule.required &&
(!source.hasOwnProperty(rule.field) ||
isEmptyValue(value, type || rule.type))
) {
errors.push(format(options.messages.required, rule.fullField));
2023-12-20 15:15:23 +08:00
}
}
/**
* 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.
*/
function whitespace(rule, value, source, errors, options) {
2024-08-30 17:45:46 +08:00
if (/^\s+$/.test(value) || value === "") {
errors.push(format(options.messages.whitespace, rule.fullField));
2023-12-20 15:15:23 +08:00
}
}
/* eslint max-len:0 */
const pattern = {
// http://emailregex.com/
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,}))$/,
url: new RegExp(
2024-08-30 17:45:46 +08:00
"^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$",
"i"
2023-12-20 15:15:23 +08:00
),
2024-08-30 17:45:46 +08:00
hex: /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i,
};
2023-12-20 15:15:23 +08:00
var types = {
integer: function integer(value) {
return /^(-)?\d+$/.test(value);
},
float: function float(value) {
return /^(-)?\d+(\.\d+)?$/.test(value);
},
array: function array(value) {
2024-08-30 17:45:46 +08:00
return Array.isArray(value);
2023-12-20 15:15:23 +08:00
},
regexp: function regexp(value) {
if (value instanceof RegExp) {
2024-08-30 17:45:46 +08:00
return true;
2023-12-20 15:15:23 +08:00
}
try {
2024-08-30 17:45:46 +08:00
return !!new RegExp(value);
2023-12-20 15:15:23 +08:00
} catch (e) {
2024-08-30 17:45:46 +08:00
return false;
2023-12-20 15:15:23 +08:00
}
},
date: function date(value) {
2024-08-30 17:45:46 +08:00
return (
typeof value.getTime === "function" &&
typeof value.getMonth === "function" &&
typeof value.getYear === "function"
);
2023-12-20 15:15:23 +08:00
},
number: function number(value) {
if (isNaN(value)) {
2024-08-30 17:45:46 +08:00
return false;
2023-12-20 15:15:23 +08:00
}
// 修改源码,将字符串数值先转为数值
2024-08-30 17:45:46 +08:00
return typeof +value === "number";
2023-12-20 15:15:23 +08:00
},
object: function object(value) {
2024-08-30 17:45:46 +08:00
return typeof value === "object" && !types.array(value);
2023-12-20 15:15:23 +08:00
},
method: function method(value) {
2024-08-30 17:45:46 +08:00
return typeof value === "function";
2023-12-20 15:15:23 +08:00
},
email: function email(value) {
2024-08-30 17:45:46 +08:00
return (
typeof value === "string" &&
!!value.match(pattern.email) &&
value.length < 255
);
2023-12-20 15:15:23 +08:00
},
url: function url(value) {
2024-08-30 17:45:46 +08:00
return typeof value === "string" && !!value.match(pattern.url);
2023-12-20 15:15:23 +08:00
},
hex: function hex(value) {
2024-08-30 17:45:46 +08:00
return typeof value === "string" && !!value.match(pattern.hex);
},
};
2023-12-20 15:15:23 +08:00
/**
* 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) {
if (rule.required && value === undefined) {
2024-08-30 17:45:46 +08:00
required(rule, value, source, errors, options);
return;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
const custom = [
"integer",
"float",
"array",
"regexp",
"object",
"method",
"email",
"number",
"date",
"url",
"hex",
];
const ruleType = rule.type;
2023-12-20 15:15:23 +08:00
if (custom.indexOf(ruleType) > -1) {
if (!types[ruleType](value)) {
2024-08-30 17:45:46 +08:00
errors.push(
format(
options.messages.types[ruleType],
rule.fullField,
rule.type
)
);
2023-12-20 15:15:23 +08:00
} // straight typeof check
} else if (ruleType && typeof value !== rule.type) {
2024-08-30 17:45:46 +08:00
errors.push(
format(options.messages.types[ruleType], rule.fullField, rule.type)
);
2023-12-20 15:15:23 +08:00
}
}
/**
* 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) {
2024-08-30 17:45:46 +08:00
const len = typeof rule.len === "number";
const min = typeof rule.min === "number";
const max = typeof rule.max === "number"; // 正则匹配码点范围从U+010000一直到U+10FFFF的文字补充平面Supplementary Plane
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
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);
2023-12-20 15:15:23 +08:00
if (num) {
2024-08-30 17:45:46 +08:00
key = "number";
2023-12-20 15:15:23 +08:00
} else if (str) {
2024-08-30 17:45:46 +08:00
key = "string";
2023-12-20 15:15:23 +08:00
} else if (arr) {
2024-08-30 17:45:46 +08:00
key = "array";
2023-12-20 15:15:23 +08:00
} // if the value is not of a supported type for range validation
// the validation rule rule should use the
// type property to also test for a particular type
if (!key) {
2024-08-30 17:45:46 +08:00
return false;
2023-12-20 15:15:23 +08:00
}
if (arr) {
2024-08-30 17:45:46 +08:00
val = value.length;
2023-12-20 15:15:23 +08:00
}
if (str) {
// 处理码点大于U+010000的文字length属性不准确的bug如"𠮷𠮷𠮷".lenght !== 3
2024-08-30 17:45:46 +08:00
val = value.replace(spRegexp, "_").length;
2023-12-20 15:15:23 +08:00
}
if (len) {
if (val !== rule.len) {
2024-08-30 17:45:46 +08:00
errors.push(
format(options.messages[key].len, rule.fullField, rule.len)
);
2023-12-20 15:15:23 +08:00
}
} else if (min && !max && val < rule.min) {
2024-08-30 17:45:46 +08:00
errors.push(
format(options.messages[key].min, rule.fullField, rule.min)
);
2023-12-20 15:15:23 +08:00
} else if (max && !min && val > rule.max) {
2024-08-30 17:45:46 +08:00
errors.push(
format(options.messages[key].max, rule.fullField, rule.max)
);
2023-12-20 15:15:23 +08:00
} else if (min && max && (val < rule.min || val > rule.max)) {
2024-08-30 17:45:46 +08:00
errors.push(
format(
options.messages[key].range,
rule.fullField,
rule.min,
rule.max
)
);
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
const ENUM = "enum";
2023-12-20 15:15:23 +08:00
/**
* 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.
*/
function enumerable(rule, value, source, errors, options) {
2024-08-30 17:45:46 +08:00
rule[ENUM] = Array.isArray(rule[ENUM]) ? rule[ENUM] : [];
2023-12-20 15:15:23 +08:00
if (rule[ENUM].indexOf(value) === -1) {
2024-08-30 17:45:46 +08:00
errors.push(
format(
options.messages[ENUM],
rule.fullField,
rule[ENUM].join(", ")
)
);
2023-12-20 15:15:23 +08:00
}
}
/**
* 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) {
if (rule.pattern) {
if (rule.pattern instanceof RegExp) {
// if a RegExp instance is passed, reset `lastIndex` in case its `global`
// flag is accidentally set to `true`, which in a validation scenario
// is not necessary and the result might be misleading
2024-08-30 17:45:46 +08:00
rule.pattern.lastIndex = 0;
2023-12-20 15:15:23 +08:00
if (!rule.pattern.test(value)) {
2024-08-30 17:45:46 +08:00
errors.push(
format(
options.messages.pattern.mismatch,
rule.fullField,
value,
rule.pattern
)
);
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
} else if (typeof rule.pattern === "string") {
const _pattern = new RegExp(rule.pattern);
2023-12-20 15:15:23 +08:00
if (!_pattern.test(value)) {
2024-08-30 17:45:46 +08:00
errors.push(
format(
options.messages.pattern.mismatch,
rule.fullField,
value,
rule.pattern
)
);
2023-12-20 15:15:23 +08:00
}
}
}
}
const rules = {
required,
whitespace,
type,
range,
enum: enumerable,
2024-08-30 17:45:46 +08:00
pattern: pattern$1,
};
2023-12-20 15:15:23 +08:00
/**
* 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) {
2024-08-30 17:45:46 +08:00
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
2024-08-30 17:45:46 +08:00
if (isEmptyValue(value, "string") && !rule.required) {
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options, "string");
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (!isEmptyValue(value, "string")) {
rules.type(rule, value, source, errors, options);
rules.range(rule, value, source, errors, options);
rules.pattern(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
if (rule.whitespace === true) {
2024-08-30 17:45:46 +08:00
rules.whitespace(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
}
}
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
/**
* 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) {
2024-08-30 17:45:46 +08:00
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
if (isEmptyValue(value) && !rule.required) {
2024-08-30 17:45:46 +08:00
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
if (value !== undefined) {
2024-08-30 17:45:46 +08:00
rules.type(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
/**
* 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) {
2024-08-30 17:45:46 +08:00
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
2024-08-30 17:45:46 +08:00
if (value === "") {
value = undefined;
2023-12-20 15:15:23 +08:00
}
if (isEmptyValue(value) && !rule.required) {
2024-08-30 17:45:46 +08:00
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
if (value !== undefined) {
2024-08-30 17:45:46 +08:00
rules.type(rule, value, source, errors, options);
rules.range(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
/**
* 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) {
2024-08-30 17:45:46 +08:00
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
if (isEmptyValue(value) && !rule.required) {
2024-08-30 17:45:46 +08:00
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
if (value !== undefined) {
2024-08-30 17:45:46 +08:00
rules.type(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
/**
* 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) {
2024-08-30 17:45:46 +08:00
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
if (isEmptyValue(value) && !rule.required) {
2024-08-30 17:45:46 +08:00
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
if (!isEmptyValue(value)) {
2024-08-30 17:45:46 +08:00
rules.type(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
/**
* 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) {
2024-08-30 17:45:46 +08:00
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
if (isEmptyValue(value) && !rule.required) {
2024-08-30 17:45:46 +08:00
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
if (value !== undefined) {
2024-08-30 17:45:46 +08:00
rules.type(rule, value, source, errors, options);
rules.range(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
/**
* 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) {
2024-08-30 17:45:46 +08:00
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
if (isEmptyValue(value) && !rule.required) {
2024-08-30 17:45:46 +08:00
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
if (value !== undefined) {
2024-08-30 17:45:46 +08:00
rules.type(rule, value, source, errors, options);
rules.range(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
/**
* 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) {
2024-08-30 17:45:46 +08:00
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
2024-08-30 17:45:46 +08:00
if (isEmptyValue(value, "array") && !rule.required) {
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options, "array");
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (!isEmptyValue(value, "array")) {
rules.type(rule, value, source, errors, options);
rules.range(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
/**
* 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) {
2024-08-30 17:45:46 +08:00
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
if (isEmptyValue(value) && !rule.required) {
2024-08-30 17:45:46 +08:00
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
if (value !== undefined) {
2024-08-30 17:45:46 +08:00
rules.type(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
const ENUM$1 = "enum";
2023-12-20 15:15:23 +08:00
/**
* 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.
*/
function enumerable$1(rule, value, callback, source, options) {
2024-08-30 17:45:46 +08:00
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
if (isEmptyValue(value) && !rule.required) {
2024-08-30 17:45:46 +08:00
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
if (value !== undefined) {
2024-08-30 17:45:46 +08:00
rules[ENUM$1](rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
/**
* 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) {
2024-08-30 17:45:46 +08:00
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
2024-08-30 17:45:46 +08:00
if (isEmptyValue(value, "string") && !rule.required) {
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (!isEmptyValue(value, "string")) {
rules.pattern(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
function date(rule, value, callback, source, options) {
2024-08-30 17:45:46 +08:00
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
if (isEmptyValue(value) && !rule.required) {
2024-08-30 17:45:46 +08:00
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
if (!isEmptyValue(value)) {
2024-08-30 17:45:46 +08:00
let dateObject;
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (typeof value === "number") {
dateObject = new Date(value);
2023-12-20 15:15:23 +08:00
} else {
2024-08-30 17:45:46 +08:00
dateObject = value;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.type(rule, dateObject, source, errors, options);
2023-12-20 15:15:23 +08:00
if (dateObject) {
2024-08-30 17:45:46 +08:00
rules.range(
rule,
dateObject.getTime(),
source,
errors,
options
);
2023-12-20 15:15:23 +08:00
}
}
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
function required$1(rule, value, callback, source, options) {
2024-08-30 17:45:46 +08:00
const errors = [];
const type = Array.isArray(value) ? "array" : typeof value;
rules.required(rule, value, source, errors, options, type);
callback(errors);
2023-12-20 15:15:23 +08:00
}
function type$1(rule, value, callback, source, options) {
2024-08-30 17:45:46 +08:00
const ruleType = rule.type;
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
if (isEmptyValue(value, ruleType) && !rule.required) {
2024-08-30 17:45:46 +08:00
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options, ruleType);
2023-12-20 15:15:23 +08:00
if (!isEmptyValue(value, ruleType)) {
2024-08-30 17:45:46 +08:00
rules.type(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
}
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
/**
* 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) {
2024-08-30 17:45:46 +08:00
const errors = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
2023-12-20 15:15:23 +08:00
if (validate) {
if (isEmptyValue(value) && !rule.required) {
2024-08-30 17:45:46 +08:00
return callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rules.required(rule, value, source, errors, options);
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
callback(errors);
2023-12-20 15:15:23 +08:00
}
const validators = {
string,
method,
number,
boolean: _boolean,
regexp,
integer,
float: floatFn,
array,
object,
enum: enumerable$1,
pattern: pattern$2,
date,
url: type$1,
hex: type$1,
email: type$1,
required: required$1,
2024-08-30 17:45:46 +08:00
any,
};
2023-12-20 15:15:23 +08:00
function newMessages() {
return {
2024-08-30 17:45:46 +08:00
default: "Validation error on field %s",
required: "%s is required",
enum: "%s must be one of %s",
whitespace: "%s cannot be empty",
2023-12-20 15:15:23 +08:00
date: {
2024-08-30 17:45:46 +08:00
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",
2023-12-20 15:15:23 +08:00
},
types: {
2024-08-30 17:45:46 +08:00
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",
date: "%s is not a %s",
boolean: "%s is not a %s",
integer: "%s is not an %s",
float: "%s is not a %s",
regexp: "%s is not a valid %s",
email: "%s is not a valid %s",
url: "%s is not a valid %s",
hex: "%s is not a valid %s",
2023-12-20 15:15:23 +08:00
},
string: {
2024-08-30 17:45:46 +08:00
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",
2023-12-20 15:15:23 +08:00
},
number: {
2024-08-30 17:45:46 +08:00
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",
2023-12-20 15:15:23 +08:00
},
array: {
2024-08-30 17:45:46 +08:00
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",
2023-12-20 15:15:23 +08:00
},
pattern: {
2024-08-30 17:45:46 +08:00
mismatch: "%s value %s does not match pattern %s",
2023-12-20 15:15:23 +08:00
},
clone: function clone() {
2024-08-30 17:45:46 +08:00
const cloned = JSON.parse(JSON.stringify(this));
cloned.clone = this.clone;
return cloned;
},
};
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
const messages = newMessages();
2023-12-20 15:15:23 +08:00
/**
* Encapsulates a validation schema.
*
* @param descriptor An object declaring validation rules
* for this schema.
*/
function Schema(descriptor) {
2024-08-30 17:45:46 +08:00
this.rules = null;
this._messages = messages;
this.define(descriptor);
2023-12-20 15:15:23 +08:00
}
Schema.prototype = {
messages: function messages(_messages) {
if (_messages) {
2024-08-30 17:45:46 +08:00
this._messages = deepMerge(newMessages(), _messages);
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
return this._messages;
2023-12-20 15:15:23 +08:00
},
define: function define(rules) {
if (!rules) {
2024-08-30 17:45:46 +08:00
throw new Error("Cannot configure a schema with no rules");
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
if (typeof rules !== "object" || Array.isArray(rules)) {
throw new Error("Rules must be an object");
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
this.rules = {};
let z;
let item;
2023-12-20 15:15:23 +08:00
for (z in rules) {
if (rules.hasOwnProperty(z)) {
2024-08-30 17:45:46 +08:00
item = rules[z];
this.rules[z] = Array.isArray(item) ? item : [item];
2023-12-20 15:15:23 +08:00
}
}
},
validate: function validate(source_, o, oc) {
2024-08-30 17:45:46 +08:00
const _this = this;
2023-12-20 15:15:23 +08:00
if (o === void 0) {
2024-08-30 17:45:46 +08:00
o = {};
2023-12-20 15:15:23 +08:00
}
if (oc === void 0) {
2024-08-30 17:45:46 +08:00
oc = function oc() {};
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
let source = source_;
let options = o;
let callback = oc;
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (typeof options === "function") {
callback = options;
options = {};
2023-12-20 15:15:23 +08:00
}
if (!this.rules || Object.keys(this.rules).length === 0) {
if (callback) {
2024-08-30 17:45:46 +08:00
callback();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
return Promise.resolve();
2023-12-20 15:15:23 +08:00
}
function complete(results) {
2024-08-30 17:45:46 +08:00
let i;
let errors = [];
let fields = {};
2023-12-20 15:15:23 +08:00
function add(e) {
if (Array.isArray(e)) {
2024-08-30 17:45:46 +08:00
let _errors;
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
errors = (_errors = errors).concat.apply(_errors, e);
2023-12-20 15:15:23 +08:00
} else {
2024-08-30 17:45:46 +08:00
errors.push(e);
2023-12-20 15:15:23 +08:00
}
}
for (i = 0; i < results.length; i++) {
2024-08-30 17:45:46 +08:00
add(results[i]);
2023-12-20 15:15:23 +08:00
}
if (!errors.length) {
2024-08-30 17:45:46 +08:00
errors = null;
fields = null;
2023-12-20 15:15:23 +08:00
} else {
2024-08-30 17:45:46 +08:00
fields = convertFieldsError(errors);
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
callback(errors, fields);
2023-12-20 15:15:23 +08:00
}
if (options.messages) {
2024-08-30 17:45:46 +08:00
let messages$1 = this.messages();
2023-12-20 15:15:23 +08:00
if (messages$1 === messages) {
2024-08-30 17:45:46 +08:00
messages$1 = newMessages();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
deepMerge(messages$1, options.messages);
options.messages = messages$1;
2023-12-20 15:15:23 +08:00
} else {
2024-08-30 17:45:46 +08:00
options.messages = this.messages();
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
let arr;
let value;
const series = {};
const keys = options.keys || Object.keys(this.rules);
2023-12-20 15:15:23 +08:00
keys.forEach((z) => {
2024-08-30 17:45:46 +08:00
arr = _this.rules[z];
value = source[z];
2023-12-20 15:15:23 +08:00
arr.forEach((r) => {
2024-08-30 17:45:46 +08:00
let rule = r;
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (typeof rule.transform === "function") {
2023-12-20 15:15:23 +08:00
if (source === source_) {
2024-08-30 17:45:46 +08:00
source = { ...source };
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
value = source[z] = rule.transform(value);
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
if (typeof rule === "function") {
2023-12-20 15:15:23 +08:00
rule = {
2024-08-30 17:45:46 +08:00
validator: rule,
};
2023-12-20 15:15:23 +08:00
} else {
2024-08-30 17:45:46 +08:00
rule = { ...rule };
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
rule.validator = _this.getValidationMethod(rule);
rule.field = z;
rule.fullField = rule.fullField || z;
rule.type = _this.getType(rule);
2023-12-20 15:15:23 +08:00
if (!rule.validator) {
2024-08-30 17:45:46 +08:00
return;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
series[z] = series[z] || [];
2023-12-20 15:15:23 +08:00
series[z].push({
rule,
value,
source,
2024-08-30 17:45:46 +08:00
field: z,
});
});
});
const errorFields = {};
return asyncMap(
series,
options,
(data, doIt) => {
const { rule } = data;
let deep =
(rule.type === "object" || rule.type === "array") &&
(typeof rule.fields === "object" ||
typeof rule.defaultField === "object");
deep =
deep && (rule.required || (!rule.required && data.value));
rule.field = data.field;
function addFullfield(key, schema) {
return { ...schema, fullField: `${rule.fullField}.${key}` };
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
function cb(e) {
if (e === void 0) {
e = [];
}
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
let errors = e;
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (!Array.isArray(errors)) {
errors = [errors];
}
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (!options.suppressWarning && errors.length) {
Schema.warning("async-validator:", errors);
}
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (errors.length && rule.message) {
errors = [].concat(rule.message);
}
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
errors = errors.map(complementError(rule));
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (options.first && errors.length) {
errorFields[rule.field] = 1;
return doIt(errors);
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
if (!deep) {
doIt(errors);
} else {
// if rule is required but the target object
// does not exist fail at the rule level and don't
// go deeper
if (rule.required && !data.value) {
if (rule.message) {
errors = []
.concat(rule.message)
.map(complementError(rule));
} else if (options.error) {
errors = [
options.error(
rule,
format(
options.messages.required,
rule.field
)
),
];
} else {
errors = [];
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
return doIt(errors);
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
let fieldsSchema = {};
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (rule.defaultField) {
for (const k in data.value) {
if (data.value.hasOwnProperty(k)) {
fieldsSchema[k] = rule.defaultField;
}
}
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
fieldsSchema = { ...fieldsSchema, ...data.rule.fields };
for (const f in fieldsSchema) {
if (fieldsSchema.hasOwnProperty(f)) {
const fieldSchema = Array.isArray(
fieldsSchema[f]
)
? fieldsSchema[f]
: [fieldsSchema[f]];
fieldsSchema[f] = fieldSchema.map(
addFullfield.bind(null, f)
);
}
}
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
const schema = new Schema(fieldsSchema);
schema.messages(options.messages);
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (data.rule.options) {
data.rule.options.messages = options.messages;
data.rule.options.error = options.error;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
schema.validate(
data.value,
data.rule.options || options,
(errs) => {
const finalErrors = [];
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
if (errors && errors.length) {
finalErrors.push.apply(finalErrors, errors);
}
if (errs && errs.length) {
finalErrors.push.apply(finalErrors, errs);
}
doIt(finalErrors.length ? finalErrors : null);
}
);
}
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
let res;
if (rule.asyncValidator) {
res = rule.asyncValidator(
rule,
data.value,
cb,
data.source,
options
);
} else if (rule.validator) {
res = rule.validator(
rule,
data.value,
cb,
data.source,
options
);
if (res === true) {
cb();
} else if (res === false) {
cb(rule.message || `${rule.field} fails`);
} else if (res instanceof Array) {
cb(res);
} else if (res instanceof Error) {
cb(res.message);
}
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
if (res && res.then) {
res.then(
() => cb(),
(e) => cb(e)
);
}
},
(results) => {
complete(results);
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
);
2023-12-20 15:15:23 +08:00
},
getType: function getType(rule) {
if (rule.type === undefined && rule.pattern instanceof RegExp) {
2024-08-30 17:45:46 +08:00
rule.type = "pattern";
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
if (
typeof rule.validator !== "function" &&
rule.type &&
!validators.hasOwnProperty(rule.type)
) {
throw new Error(format("Unknown rule type %s", rule.type));
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
return rule.type || "string";
2023-12-20 15:15:23 +08:00
},
getValidationMethod: function getValidationMethod(rule) {
2024-08-30 17:45:46 +08:00
if (typeof rule.validator === "function") {
return rule.validator;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
const keys = Object.keys(rule);
const messageIndex = keys.indexOf("message");
2023-12-20 15:15:23 +08:00
if (messageIndex !== -1) {
2024-08-30 17:45:46 +08:00
keys.splice(messageIndex, 1);
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
if (keys.length === 1 && keys[0] === "required") {
return validators.required;
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
return validators[this.getType(rule)] || false;
},
};
2023-12-20 15:15:23 +08:00
Schema.register = function register(type, validator) {
2024-08-30 17:45:46 +08:00
if (typeof validator !== "function") {
throw new Error(
"Cannot register a validator by type, validator is not a function"
);
2023-12-20 15:15:23 +08:00
}
2024-08-30 17:45:46 +08:00
validators[type] = validator;
};
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
Schema.warning = warning;
Schema.messages = messages;
2023-12-20 15:15:23 +08:00
2024-08-30 17:45:46 +08:00
export default Schema;
2023-12-20 15:15:23 +08:00
// # sourceMappingURL=index.js.map