@rc-component/async-validator
Advanced tools
+232
-250
@@ -1,7 +0,1 @@ | ||
| import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; | ||
| import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; | ||
| import _typeof from "@babel/runtime/helpers/esm/typeof"; | ||
| import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; | ||
| import _createClass from "@babel/runtime/helpers/esm/createClass"; | ||
| import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; | ||
| import { messages as defaultMessages, newMessages } from "./messages"; | ||
@@ -18,276 +12,264 @@ import { asyncMap, complementError, convertFieldsError, deepMerge, format, warning } from "./util"; | ||
| */ | ||
| var Schema = /*#__PURE__*/function () { | ||
| function Schema(descriptor) { | ||
| _classCallCheck(this, Schema); | ||
| // ======================== Instance ======================== | ||
| _defineProperty(this, "rules", null); | ||
| _defineProperty(this, "_messages", defaultMessages); | ||
| class Schema { | ||
| // ========================= Static ========================= | ||
| static register = function register(type, validator) { | ||
| if (typeof validator !== 'function') { | ||
| throw new Error('Cannot register a validator by type, validator is not a function'); | ||
| } | ||
| validators[type] = validator; | ||
| }; | ||
| static warning = warning; | ||
| static messages = defaultMessages; | ||
| static validators = validators; | ||
| // ======================== Instance ======================== | ||
| rules = null; | ||
| _messages = defaultMessages; | ||
| constructor(descriptor) { | ||
| this.define(descriptor); | ||
| } | ||
| _createClass(Schema, [{ | ||
| key: "define", | ||
| value: function define(rules) { | ||
| var _this = this; | ||
| if (!rules) { | ||
| throw new Error('Cannot configure a schema with no rules'); | ||
| } | ||
| if (_typeof(rules) !== 'object' || Array.isArray(rules)) { | ||
| throw new Error('Rules must be an object'); | ||
| } | ||
| this.rules = {}; | ||
| Object.keys(rules).forEach(function (name) { | ||
| var item = rules[name]; | ||
| _this.rules[name] = Array.isArray(item) ? item : [item]; | ||
| }); | ||
| define(rules) { | ||
| if (!rules) { | ||
| throw new Error('Cannot configure a schema with no rules'); | ||
| } | ||
| }, { | ||
| key: "messages", | ||
| value: function messages(_messages) { | ||
| if (_messages) { | ||
| this._messages = deepMerge(newMessages(), _messages); | ||
| if (typeof rules !== 'object' || Array.isArray(rules)) { | ||
| throw new Error('Rules must be an object'); | ||
| } | ||
| this.rules = {}; | ||
| Object.keys(rules).forEach(name => { | ||
| const item = rules[name]; | ||
| this.rules[name] = Array.isArray(item) ? item : [item]; | ||
| }); | ||
| } | ||
| messages(messages) { | ||
| if (messages) { | ||
| this._messages = deepMerge(newMessages(), messages); | ||
| } | ||
| return this._messages; | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/unified-signatures | ||
| validate(source_, o = {}, oc = () => {}) { | ||
| let source = source_; | ||
| let options = o; | ||
| let callback = oc; | ||
| if (typeof options === 'function') { | ||
| callback = options; | ||
| options = {}; | ||
| } | ||
| if (!this.rules || Object.keys(this.rules).length === 0) { | ||
| if (callback) { | ||
| callback(null, source); | ||
| } | ||
| return this._messages; | ||
| return Promise.resolve(source); | ||
| } | ||
| }, { | ||
| key: "validate", | ||
| value: function validate(source_) { | ||
| var _this2 = this; | ||
| var o = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
| var oc = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () {}; | ||
| var source = source_; | ||
| var options = o; | ||
| var callback = oc; | ||
| if (typeof options === 'function') { | ||
| callback = options; | ||
| options = {}; | ||
| } | ||
| if (!this.rules || Object.keys(this.rules).length === 0) { | ||
| if (callback) { | ||
| callback(null, source); | ||
| } | ||
| return Promise.resolve(source); | ||
| } | ||
| function complete(results) { | ||
| var errors = []; | ||
| var fields = {}; | ||
| function add(e) { | ||
| if (Array.isArray(e)) { | ||
| var _errors; | ||
| errors = (_errors = errors).concat.apply(_errors, _toConsumableArray(e)); | ||
| } else { | ||
| errors.push(e); | ||
| } | ||
| } | ||
| for (var i = 0; i < results.length; i++) { | ||
| add(results[i]); | ||
| } | ||
| if (!errors.length) { | ||
| callback(null, source); | ||
| function complete(results) { | ||
| let errors = []; | ||
| let fields = {}; | ||
| function add(e) { | ||
| if (Array.isArray(e)) { | ||
| errors = errors.concat(...e); | ||
| } else { | ||
| fields = convertFieldsError(errors); | ||
| callback(errors, fields); | ||
| errors.push(e); | ||
| } | ||
| } | ||
| if (options.messages) { | ||
| var messages = this.messages(); | ||
| if (messages === defaultMessages) { | ||
| messages = newMessages(); | ||
| } | ||
| deepMerge(messages, options.messages); | ||
| options.messages = messages; | ||
| for (let i = 0; i < results.length; i++) { | ||
| add(results[i]); | ||
| } | ||
| if (!errors.length) { | ||
| callback(null, source); | ||
| } else { | ||
| options.messages = this.messages(); | ||
| fields = convertFieldsError(errors); | ||
| callback(errors, fields); | ||
| } | ||
| var series = {}; | ||
| var keys = options.keys || Object.keys(this.rules); | ||
| keys.forEach(function (z) { | ||
| var arr = _this2.rules[z]; | ||
| var value = source[z]; | ||
| arr.forEach(function (r) { | ||
| var rule = r; | ||
| if (typeof rule.transform === 'function') { | ||
| if (source === source_) { | ||
| source = _objectSpread({}, source); | ||
| } | ||
| value = source[z] = rule.transform(value); | ||
| if (value !== undefined && value !== null) { | ||
| rule.type = rule.type || (Array.isArray(value) ? 'array' : _typeof(value)); | ||
| } | ||
| } | ||
| if (typeof rule === 'function') { | ||
| rule = { | ||
| validator: rule | ||
| } | ||
| if (options.messages) { | ||
| let messages = this.messages(); | ||
| if (messages === defaultMessages) { | ||
| messages = newMessages(); | ||
| } | ||
| deepMerge(messages, options.messages); | ||
| options.messages = messages; | ||
| } else { | ||
| options.messages = this.messages(); | ||
| } | ||
| const series = {}; | ||
| const keys = options.keys || Object.keys(this.rules); | ||
| keys.forEach(z => { | ||
| const arr = this.rules[z]; | ||
| let value = source[z]; | ||
| arr.forEach(r => { | ||
| let rule = r; | ||
| if (typeof rule.transform === 'function') { | ||
| if (source === source_) { | ||
| source = { | ||
| ...source | ||
| }; | ||
| } else { | ||
| rule = _objectSpread({}, rule); | ||
| } | ||
| value = source[z] = rule.transform(value); | ||
| if (value !== undefined && value !== null) { | ||
| rule.type = rule.type || (Array.isArray(value) ? 'array' : typeof value); | ||
| } | ||
| } | ||
| if (typeof rule === 'function') { | ||
| rule = { | ||
| validator: rule | ||
| }; | ||
| } else { | ||
| rule = { | ||
| ...rule | ||
| }; | ||
| } | ||
| // Fill validator. Skip if nothing need to validate | ||
| rule.validator = _this2.getValidationMethod(rule); | ||
| if (!rule.validator) { | ||
| return; | ||
| } | ||
| rule.field = z; | ||
| rule.fullField = rule.fullField || z; | ||
| rule.type = _this2.getType(rule); | ||
| series[z] = series[z] || []; | ||
| series[z].push({ | ||
| rule: rule, | ||
| value: value, | ||
| source: source, | ||
| field: z | ||
| }); | ||
| // Fill validator. Skip if nothing need to validate | ||
| rule.validator = this.getValidationMethod(rule); | ||
| if (!rule.validator) { | ||
| return; | ||
| } | ||
| rule.field = z; | ||
| rule.fullField = rule.fullField || z; | ||
| rule.type = this.getType(rule); | ||
| series[z] = series[z] || []; | ||
| series[z].push({ | ||
| rule, | ||
| value, | ||
| source, | ||
| field: z | ||
| }); | ||
| }); | ||
| var errorFields = {}; | ||
| return asyncMap(series, options, function (data, doIt) { | ||
| var rule = data.rule; | ||
| var deep = (rule.type === 'object' || rule.type === 'array') && (_typeof(rule.fields) === 'object' || _typeof(rule.defaultField) === 'object'); | ||
| deep = deep && (rule.required || !rule.required && data.value); | ||
| rule.field = data.field; | ||
| function addFullField(key, schema) { | ||
| return _objectSpread(_objectSpread({}, schema), {}, { | ||
| fullField: "".concat(rule.fullField, ".").concat(key), | ||
| fullFields: rule.fullFields ? [].concat(_toConsumableArray(rule.fullFields), [key]) : [key] | ||
| }); | ||
| }); | ||
| const errorFields = {}; | ||
| return asyncMap(series, options, (data, doIt) => { | ||
| const rule = data.rule; | ||
| let deep = (rule.type === 'object' || rule.type === 'array') && (typeof rule.fields === 'object' || typeof rule.defaultField === 'object'); | ||
| deep = deep && (rule.required || !rule.required && data.value); | ||
| rule.field = data.field; | ||
| function addFullField(key, schema) { | ||
| return { | ||
| ...schema, | ||
| fullField: `${rule.fullField}.${key}`, | ||
| fullFields: rule.fullFields ? [...rule.fullFields, key] : [key] | ||
| }; | ||
| } | ||
| function cb(e = []) { | ||
| let errorList = Array.isArray(e) ? e : [e]; | ||
| if (!options.suppressWarning && errorList.length) { | ||
| Schema.warning('async-validator:', errorList); | ||
| } | ||
| function cb() { | ||
| var e = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
| var errorList = Array.isArray(e) ? e : [e]; | ||
| if (!options.suppressWarning && errorList.length) { | ||
| Schema.warning('async-validator:', errorList); | ||
| } | ||
| if (errorList.length && rule.message !== undefined && rule.message !== null) { | ||
| errorList = [].concat(rule.message); | ||
| } | ||
| if (errorList.length && rule.message !== undefined && rule.message !== null) { | ||
| errorList = [].concat(rule.message); | ||
| } | ||
| // Fill error info | ||
| var filledErrors = errorList.map(complementError(rule, source)); | ||
| if (options.first && filledErrors.length) { | ||
| errorFields[rule.field] = 1; | ||
| // Fill error info | ||
| let filledErrors = errorList.map(complementError(rule, source)); | ||
| if (options.first && filledErrors.length) { | ||
| errorFields[rule.field] = 1; | ||
| return doIt(filledErrors); | ||
| } | ||
| if (!deep) { | ||
| doIt(filledErrors); | ||
| } 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 !== undefined) { | ||
| filledErrors = [].concat(rule.message).map(complementError(rule, source)); | ||
| } else if (options.error) { | ||
| filledErrors = [options.error(rule, format(options.messages.required, rule.field))]; | ||
| } | ||
| return doIt(filledErrors); | ||
| } | ||
| if (!deep) { | ||
| doIt(filledErrors); | ||
| } 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 !== undefined) { | ||
| filledErrors = [].concat(rule.message).map(complementError(rule, source)); | ||
| } else if (options.error) { | ||
| filledErrors = [options.error(rule, format(options.messages.required, rule.field))]; | ||
| } | ||
| return doIt(filledErrors); | ||
| let fieldsSchema = {}; | ||
| if (rule.defaultField) { | ||
| Object.keys(data.value).map(key => { | ||
| fieldsSchema[key] = rule.defaultField; | ||
| }); | ||
| } | ||
| fieldsSchema = { | ||
| ...fieldsSchema, | ||
| ...data.rule.fields | ||
| }; | ||
| const paredFieldsSchema = {}; | ||
| Object.keys(fieldsSchema).forEach(field => { | ||
| const fieldSchema = fieldsSchema[field]; | ||
| const fieldSchemaList = Array.isArray(fieldSchema) ? fieldSchema : [fieldSchema]; | ||
| paredFieldsSchema[field] = fieldSchemaList.map(addFullField.bind(null, field)); | ||
| }); | ||
| const schema = new Schema(paredFieldsSchema); | ||
| schema.messages(options.messages); | ||
| if (data.rule.options) { | ||
| data.rule.options.messages = options.messages; | ||
| data.rule.options.error = options.error; | ||
| } | ||
| schema.validate(data.value, data.rule.options || options, errs => { | ||
| const finalErrors = []; | ||
| if (filledErrors && filledErrors.length) { | ||
| finalErrors.push(...filledErrors); | ||
| } | ||
| var fieldsSchema = {}; | ||
| if (rule.defaultField) { | ||
| Object.keys(data.value).map(function (key) { | ||
| fieldsSchema[key] = rule.defaultField; | ||
| }); | ||
| if (errs && errs.length) { | ||
| finalErrors.push(...errs); | ||
| } | ||
| fieldsSchema = _objectSpread(_objectSpread({}, fieldsSchema), data.rule.fields); | ||
| var paredFieldsSchema = {}; | ||
| Object.keys(fieldsSchema).forEach(function (field) { | ||
| var fieldSchema = fieldsSchema[field]; | ||
| var fieldSchemaList = Array.isArray(fieldSchema) ? fieldSchema : [fieldSchema]; | ||
| paredFieldsSchema[field] = fieldSchemaList.map(addFullField.bind(null, field)); | ||
| }); | ||
| var schema = new Schema(paredFieldsSchema); | ||
| schema.messages(options.messages); | ||
| if (data.rule.options) { | ||
| data.rule.options.messages = options.messages; | ||
| data.rule.options.error = options.error; | ||
| } | ||
| schema.validate(data.value, data.rule.options || options, function (errs) { | ||
| var finalErrors = []; | ||
| if (filledErrors && filledErrors.length) { | ||
| finalErrors.push.apply(finalErrors, _toConsumableArray(filledErrors)); | ||
| } | ||
| if (errs && errs.length) { | ||
| finalErrors.push.apply(finalErrors, _toConsumableArray(errs)); | ||
| } | ||
| doIt(finalErrors.length ? finalErrors : null); | ||
| }); | ||
| } | ||
| doIt(finalErrors.length ? finalErrors : null); | ||
| }); | ||
| } | ||
| var res; | ||
| if (rule.asyncValidator) { | ||
| res = rule.asyncValidator(rule, data.value, cb, data.source, options); | ||
| } else if (rule.validator) { | ||
| try { | ||
| res = rule.validator(rule, data.value, cb, data.source, options); | ||
| } catch (error) { | ||
| var _console$error, _console; | ||
| (_console$error = (_console = console).error) === null || _console$error === void 0 || _console$error.call(_console, error); | ||
| // rethrow to report error | ||
| if (!options.suppressValidatorError) { | ||
| setTimeout(function () { | ||
| throw error; | ||
| }, 0); | ||
| } | ||
| cb(error.message); | ||
| } | ||
| let res; | ||
| if (rule.asyncValidator) { | ||
| res = rule.asyncValidator(rule, data.value, cb, data.source, options); | ||
| } else if (rule.validator) { | ||
| try { | ||
| res = rule.validator(rule, data.value, cb, data.source, options); | ||
| } catch (error) { | ||
| console.error?.(error); | ||
| // rethrow to report error | ||
| if (!options.suppressValidatorError) { | ||
| setTimeout(() => { | ||
| throw error; | ||
| }, 0); | ||
| } | ||
| if (res === true) { | ||
| cb(); | ||
| } else if (res === false) { | ||
| cb(typeof rule.message === 'function' ? rule.message(rule.fullField || rule.field) : rule.message || "".concat(rule.fullField || rule.field, " fails")); | ||
| } else if (res instanceof Array) { | ||
| cb(res); | ||
| } else if (res instanceof Error) { | ||
| cb(res.message); | ||
| } | ||
| cb(error.message); | ||
| } | ||
| if (res && res.then) { | ||
| res.then(function () { | ||
| return cb(); | ||
| }, function (e) { | ||
| return cb(e); | ||
| }); | ||
| if (res === true) { | ||
| cb(); | ||
| } else if (res === false) { | ||
| cb(typeof rule.message === 'function' ? rule.message(rule.fullField || rule.field) : rule.message || `${rule.fullField || rule.field} fails`); | ||
| } else if (res instanceof Array) { | ||
| cb(res); | ||
| } else if (res instanceof Error) { | ||
| cb(res.message); | ||
| } | ||
| }, function (results) { | ||
| complete(results); | ||
| }, source); | ||
| } | ||
| }, { | ||
| key: "getType", | ||
| value: function getType(rule) { | ||
| if (rule.type === undefined && rule.pattern instanceof RegExp) { | ||
| rule.type = 'pattern'; | ||
| } | ||
| if (typeof rule.validator !== 'function' && rule.type && !validators.hasOwnProperty(rule.type)) { | ||
| throw new Error(format('Unknown rule type %s', rule.type)); | ||
| if (res && res.then) { | ||
| res.then(() => cb(), e => cb(e)); | ||
| } | ||
| return rule.type || 'string'; | ||
| }, results => { | ||
| complete(results); | ||
| }, source); | ||
| } | ||
| getType(rule) { | ||
| if (rule.type === undefined && rule.pattern instanceof RegExp) { | ||
| rule.type = 'pattern'; | ||
| } | ||
| }, { | ||
| key: "getValidationMethod", | ||
| value: function getValidationMethod(rule) { | ||
| if (typeof rule.validator === 'function') { | ||
| return rule.validator; | ||
| } | ||
| var keys = Object.keys(rule); | ||
| var messageIndex = keys.indexOf('message'); | ||
| if (messageIndex !== -1) { | ||
| keys.splice(messageIndex, 1); | ||
| } | ||
| if (keys.length === 1 && keys[0] === 'required') { | ||
| return validators.required; | ||
| } | ||
| return validators[this.getType(rule)] || undefined; | ||
| if (typeof rule.validator !== 'function' && rule.type && !validators.hasOwnProperty(rule.type)) { | ||
| throw new Error(format('Unknown rule type %s', rule.type)); | ||
| } | ||
| }]); | ||
| return Schema; | ||
| }(); | ||
| // ========================= Static ========================= | ||
| _defineProperty(Schema, "register", function register(type, validator) { | ||
| if (typeof validator !== 'function') { | ||
| throw new Error('Cannot register a validator by type, validator is not a function'); | ||
| return rule.type || 'string'; | ||
| } | ||
| validators[type] = validator; | ||
| }); | ||
| _defineProperty(Schema, "warning", warning); | ||
| _defineProperty(Schema, "messages", defaultMessages); | ||
| _defineProperty(Schema, "validators", validators); | ||
| getValidationMethod(rule) { | ||
| if (typeof rule.validator === 'function') { | ||
| return rule.validator; | ||
| } | ||
| const keys = Object.keys(rule); | ||
| const messageIndex = keys.indexOf('message'); | ||
| if (messageIndex !== -1) { | ||
| keys.splice(messageIndex, 1); | ||
| } | ||
| if (keys.length === 1 && keys[0] === 'required') { | ||
| return validators.required; | ||
| } | ||
| return validators[this.getType(rule)] || undefined; | ||
| } | ||
| } | ||
| export default Schema; |
+3
-3
@@ -49,4 +49,4 @@ export function newMessages() { | ||
| }, | ||
| clone: function clone() { | ||
| var cloned = JSON.parse(JSON.stringify(this)); | ||
| clone() { | ||
| const cloned = JSON.parse(JSON.stringify(this)); | ||
| cloned.clone = this.clone; | ||
@@ -57,2 +57,2 @@ return cloned; | ||
| } | ||
| export var messages = newMessages(); | ||
| export const messages = newMessages(); |
+2
-2
| import { format } from "../util"; | ||
| var ENUM = 'enum'; | ||
| var enumerable = function enumerable(rule, value, source, errors, options) { | ||
| const ENUM = 'enum'; | ||
| const enumerable = (rule, value, source, errors, options) => { | ||
| rule[ENUM] = Array.isArray(rule[ENUM]) ? rule[ENUM] : []; | ||
@@ -5,0 +5,0 @@ if (rule[ENUM].indexOf(value) === -1) { |
+5
-5
@@ -8,8 +8,8 @@ import enumRule from "./enum"; | ||
| export default { | ||
| required: required, | ||
| whitespace: whitespace, | ||
| type: type, | ||
| range: range, | ||
| required, | ||
| whitespace, | ||
| type, | ||
| range, | ||
| enum: enumRule, | ||
| pattern: pattern | ||
| pattern | ||
| }; |
| import { format } from "../util"; | ||
| var pattern = function pattern(rule, value, source, errors, options) { | ||
| const pattern = (rule, value, source, errors, options) => { | ||
| if (rule.pattern) { | ||
@@ -13,3 +13,3 @@ if (rule.pattern instanceof RegExp) { | ||
| } else if (typeof rule.pattern === 'string') { | ||
| var _pattern = new RegExp(rule.pattern); | ||
| const _pattern = new RegExp(rule.pattern); | ||
| if (!_pattern.test(value)) { | ||
@@ -16,0 +16,0 @@ errors.push(format(options.messages.pattern.mismatch, rule.fullField, value, rule.pattern)); |
+10
-10
| import { format } from "../util"; | ||
| var range = function range(rule, value, source, errors, options) { | ||
| var len = typeof rule.len === 'number'; | ||
| var min = typeof rule.min === 'number'; | ||
| var max = typeof rule.max === 'number'; | ||
| const range = (rule, value, source, errors, options) => { | ||
| const len = typeof rule.len === 'number'; | ||
| const min = typeof rule.min === 'number'; | ||
| const max = typeof rule.max === 'number'; | ||
| // 正则匹配码点范围从U+010000一直到U+10FFFF的文字(补充平面Supplementary Plane) | ||
| var spRegexp = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g; | ||
| var val = value; | ||
| var key = null; | ||
| var num = typeof value === 'number'; | ||
| var str = typeof value === 'string'; | ||
| var arr = Array.isArray(value); | ||
| 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); | ||
| if (num) { | ||
@@ -14,0 +14,0 @@ key = 'number'; |
| import { format, isEmptyValue } from "../util"; | ||
| var required = function required(rule, value, source, errors, options, type) { | ||
| const required = (rule, value, source, errors, options, type) => { | ||
| if (rule.required && (!source.hasOwnProperty(rule.field) || isEmptyValue(value, type || rule.type))) { | ||
@@ -4,0 +4,0 @@ errors.push(format(options.messages.required, rule.fullField)); |
+19
-20
@@ -1,2 +0,1 @@ | ||
| import _typeof from "@babel/runtime/helpers/esm/typeof"; | ||
| import { format } from "../util"; | ||
@@ -7,3 +6,3 @@ import required from "./required"; | ||
| var pattern = { | ||
| const pattern = { | ||
| // http://emailregex.com/ | ||
@@ -24,13 +23,13 @@ email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+\.)+[a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}))$/, | ||
| }; | ||
| var types = { | ||
| integer: function integer(value) { | ||
| const types = { | ||
| integer(value) { | ||
| return types.number(value) && parseInt(value, 10) === value; | ||
| }, | ||
| float: function float(value) { | ||
| float(value) { | ||
| return types.number(value) && !types.integer(value); | ||
| }, | ||
| array: function array(value) { | ||
| array(value) { | ||
| return Array.isArray(value); | ||
| }, | ||
| regexp: function regexp(value) { | ||
| regexp(value) { | ||
| if (value instanceof RegExp) { | ||
@@ -45,6 +44,6 @@ return true; | ||
| }, | ||
| date: function date(value) { | ||
| date(value) { | ||
| return typeof value.getTime === 'function' && typeof value.getMonth === 'function' && typeof value.getYear === 'function' && !isNaN(value.getTime()); | ||
| }, | ||
| number: function number(value) { | ||
| number(value) { | ||
| if (isNaN(value)) { | ||
@@ -55,22 +54,22 @@ return false; | ||
| }, | ||
| object: function object(value) { | ||
| return _typeof(value) === 'object' && !types.array(value); | ||
| object(value) { | ||
| return typeof value === 'object' && !types.array(value); | ||
| }, | ||
| method: function method(value) { | ||
| method(value) { | ||
| return typeof value === 'function'; | ||
| }, | ||
| email: function email(value) { | ||
| email(value) { | ||
| return typeof value === 'string' && value.length <= 320 && !!value.match(pattern.email); | ||
| }, | ||
| tel: function tel(value) { | ||
| tel(value) { | ||
| return typeof value === 'string' && value.length <= 32 && !!value.match(pattern.tel); | ||
| }, | ||
| url: function url(value) { | ||
| url(value) { | ||
| return typeof value === 'string' && value.length <= 2048 && !!value.match(getUrlRegex()); | ||
| }, | ||
| hex: function hex(value) { | ||
| hex(value) { | ||
| return typeof value === 'string' && !!value.match(pattern.hex); | ||
| } | ||
| }; | ||
| var type = function type(rule, value, source, errors, options) { | ||
| const type = (rule, value, source, errors, options) => { | ||
| if (rule.required && value === undefined) { | ||
@@ -80,4 +79,4 @@ required(rule, value, source, errors, options); | ||
| } | ||
| var custom = ['integer', 'float', 'array', 'regexp', 'object', 'method', 'email', 'tel', 'number', 'date', 'url', 'hex']; | ||
| var ruleType = rule.type; | ||
| const custom = ['integer', 'float', 'array', 'regexp', 'object', 'method', 'email', 'tel', 'number', 'date', 'url', 'hex']; | ||
| const ruleType = rule.type; | ||
| if (custom.indexOf(ruleType) > -1) { | ||
@@ -88,3 +87,3 @@ if (!types[ruleType](value)) { | ||
| // straight typeof check | ||
| } else if (ruleType && _typeof(value) !== rule.type) { | ||
| } else if (ruleType && typeof value !== rule.type) { | ||
| errors.push(format(options.messages.types[ruleType], rule.fullField, rule.type)); | ||
@@ -91,0 +90,0 @@ } |
+40
-41
| // https://github.com/kevva/url-regex/blob/master/index.js | ||
| var urlReg; | ||
| export default (function () { | ||
| let urlReg; | ||
| export default (() => { | ||
| if (urlReg) { | ||
| return urlReg; | ||
| } | ||
| var word = '[a-fA-F\\d:]'; | ||
| var b = function b(options) { | ||
| return options && options.includeBoundaries ? "(?:(?<=\\s|^)(?=".concat(word, ")|(?<=").concat(word, ")(?=\\s|$))") : ''; | ||
| }; | ||
| var v4 = '(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}'; | ||
| var v6seg = '[a-fA-F\\d]{1,4}'; | ||
| var v6List = ["(?:".concat(v6seg, ":){7}(?:").concat(v6seg, "|:)"), // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8 | ||
| "(?:".concat(v6seg, ":){6}(?:").concat(v4, "|:").concat(v6seg, "|:)"), // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6:: | ||
| "(?:".concat(v6seg, ":){5}(?::").concat(v4, "|(?::").concat(v6seg, "){1,2}|:)"), // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5:: | ||
| "(?:".concat(v6seg, ":){4}(?:(?::").concat(v6seg, "){0,1}:").concat(v4, "|(?::").concat(v6seg, "){1,3}|:)"), // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4:: | ||
| "(?:".concat(v6seg, ":){3}(?:(?::").concat(v6seg, "){0,2}:").concat(v4, "|(?::").concat(v6seg, "){1,4}|:)"), // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3:: | ||
| "(?:".concat(v6seg, ":){2}(?:(?::").concat(v6seg, "){0,3}:").concat(v4, "|(?::").concat(v6seg, "){1,5}|:)"), // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2:: | ||
| "(?:".concat(v6seg, ":){1}(?:(?::").concat(v6seg, "){0,4}:").concat(v4, "|(?::").concat(v6seg, "){1,6}|:)"), // 1:: 1::3:4:5:6:7:8 1::8 1:: | ||
| "(?::(?:(?::".concat(v6seg, "){0,5}:").concat(v4, "|(?::").concat(v6seg, "){1,7}|:))") // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 :: | ||
| const word = '[a-fA-F\\d:]'; | ||
| const b = options => options && options.includeBoundaries ? `(?:(?<=\\s|^)(?=${word})|(?<=${word})(?=\\s|$))` : ''; | ||
| const v4 = '(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}'; | ||
| const v6seg = '[a-fA-F\\d]{1,4}'; | ||
| const v6List = [`(?:${v6seg}:){7}(?:${v6seg}|:)`, | ||
| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8 | ||
| `(?:${v6seg}:){6}(?:${v4}|:${v6seg}|:)`, | ||
| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6:: | ||
| `(?:${v6seg}:){5}(?::${v4}|(?::${v6seg}){1,2}|:)`, | ||
| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5:: | ||
| `(?:${v6seg}:){4}(?:(?::${v6seg}){0,1}:${v4}|(?::${v6seg}){1,3}|:)`, | ||
| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4:: | ||
| `(?:${v6seg}:){3}(?:(?::${v6seg}){0,2}:${v4}|(?::${v6seg}){1,4}|:)`, | ||
| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3:: | ||
| `(?:${v6seg}:){2}(?:(?::${v6seg}){0,3}:${v4}|(?::${v6seg}){1,5}|:)`, | ||
| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2:: | ||
| `(?:${v6seg}:){1}(?:(?::${v6seg}){0,4}:${v4}|(?::${v6seg}){1,6}|:)`, | ||
| // 1:: 1::3:4:5:6:7:8 1::8 1:: | ||
| `(?::(?:(?::${v6seg}){0,5}:${v4}|(?::${v6seg}){1,7}|:))` // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 :: | ||
| ]; | ||
| var v6Eth0 = "(?:%[0-9a-zA-Z]{1,})?"; // %eth0 %1 | ||
| const v6Eth0 = `(?:%[0-9a-zA-Z]{1,})?`; // %eth0 %1 | ||
| var v6 = "(?:".concat(v6List.join('|'), ")").concat(v6Eth0); | ||
| const v6 = `(?:${v6List.join('|')})${v6Eth0}`; | ||
| // Pre-compile only the exact regexes because adding a global flag make regexes stateful | ||
| var v46Exact = new RegExp("(?:^".concat(v4, "$)|(?:^").concat(v6, "$)")); | ||
| var v4exact = new RegExp("^".concat(v4, "$")); | ||
| var v6exact = new RegExp("^".concat(v6, "$")); | ||
| var ip = function ip(options) { | ||
| return options && options.exact ? v46Exact : new RegExp("(?:".concat(b(options)).concat(v4).concat(b(options), ")|(?:").concat(b(options)).concat(v6).concat(b(options), ")"), 'g'); | ||
| }; | ||
| ip.v4 = function (options) { | ||
| return options && options.exact ? v4exact : new RegExp("".concat(b(options)).concat(v4).concat(b(options)), 'g'); | ||
| }; | ||
| ip.v6 = function (options) { | ||
| return options && options.exact ? v6exact : new RegExp("".concat(b(options)).concat(v6).concat(b(options)), 'g'); | ||
| }; | ||
| var protocol = "(?:(?:[a-z]+:)?//)"; | ||
| var auth = '(?:\\S+(?::\\S*)?@)?'; | ||
| var ipv4 = ip.v4().source; | ||
| var ipv6 = ip.v6().source; | ||
| var host = "(?:(?:[a-z\\u00a1-\\uffff0-9][-_]*)*[a-z\\u00a1-\\uffff0-9]+)"; | ||
| var domain = "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*"; | ||
| var tld = "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))"; | ||
| var port = '(?::\\d{2,5})?'; | ||
| var path = '(?:[/?#][^\\s"]*)?'; | ||
| var regex = "(?:".concat(protocol, "|www\\.)").concat(auth, "(?:localhost|").concat(ipv4, "|").concat(ipv6, "|").concat(host).concat(domain).concat(tld, ")").concat(port).concat(path); | ||
| urlReg = new RegExp("(?:^".concat(regex, "$)"), 'i'); | ||
| const v46Exact = new RegExp(`(?:^${v4}$)|(?:^${v6}$)`); | ||
| const v4exact = new RegExp(`^${v4}$`); | ||
| const v6exact = new RegExp(`^${v6}$`); | ||
| const ip = options => options && options.exact ? v46Exact : new RegExp(`(?:${b(options)}${v4}${b(options)})|(?:${b(options)}${v6}${b(options)})`, 'g'); | ||
| ip.v4 = options => options && options.exact ? v4exact : new RegExp(`${b(options)}${v4}${b(options)}`, 'g'); | ||
| ip.v6 = options => options && options.exact ? v6exact : new RegExp(`${b(options)}${v6}${b(options)}`, 'g'); | ||
| const protocol = `(?:(?:[a-z]+:)?//)`; | ||
| const auth = '(?:\\S+(?::\\S*)?@)?'; | ||
| const ipv4 = ip.v4().source; | ||
| const ipv6 = ip.v6().source; | ||
| const host = '(?:(?:[a-z\\u00a1-\\uffff0-9][-_]*)*[a-z\\u00a1-\\uffff0-9]+)'; | ||
| const domain = '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*'; | ||
| const tld = `(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))`; | ||
| const port = '(?::\\d{2,5})?'; | ||
| const path = '(?:[/?#][^\\s"]*)?'; | ||
| const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ipv4}|${ipv6}|${host}${domain}${tld})${port}${path}`; | ||
| urlReg = new RegExp(`(?:^${regex}$)`, 'i'); | ||
| return urlReg; | ||
| }); |
@@ -14,3 +14,3 @@ import { format } from "../util"; | ||
| */ | ||
| var whitespace = function whitespace(rule, value, source, errors, options) { | ||
| const whitespace = (rule, value, source, errors, options) => { | ||
| if (/^\s+$/.test(value) || value === '') { | ||
@@ -17,0 +17,0 @@ errors.push(format(options.messages.whitespace, rule.fullField)); |
+57
-79
@@ -1,23 +0,11 @@ | ||
| import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; | ||
| import _typeof from "@babel/runtime/helpers/esm/typeof"; | ||
| import _createClass from "@babel/runtime/helpers/esm/createClass"; | ||
| import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; | ||
| import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized"; | ||
| import _inherits from "@babel/runtime/helpers/esm/inherits"; | ||
| import _createSuper from "@babel/runtime/helpers/esm/createSuper"; | ||
| import _wrapNativeSuper from "@babel/runtime/helpers/esm/wrapNativeSuper"; | ||
| import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; | ||
| import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; | ||
| /* eslint no-console:0 */ | ||
| var formatRegExp = /%[sdj%]/g; | ||
| export var warning = function warning() {}; | ||
| const formatRegExp = /%[sdj%]/g; | ||
| export let warning = () => {}; | ||
| // don't print warning message when in production env or node runtime | ||
| if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV !== 'production' && typeof window !== 'undefined' && typeof document !== 'undefined') { | ||
| warning = function warning(type, errors) { | ||
| warning = (type, errors) => { | ||
| if (typeof console !== 'undefined' && console.warn && typeof ASYNC_VALIDATOR_NO_WARNING === 'undefined') { | ||
| if (errors.every(function (e) { | ||
| return typeof e === 'string'; | ||
| })) { | ||
| if (errors.every(e => typeof e === 'string')) { | ||
| console.warn(type, errors); | ||
@@ -30,5 +18,5 @@ } | ||
| if (!errors || !errors.length) return null; | ||
| var fields = {}; | ||
| errors.forEach(function (error) { | ||
| var field = error.field; | ||
| const fields = {}; | ||
| errors.forEach(error => { | ||
| const field = error.field; | ||
| fields[field] = fields[field] || []; | ||
@@ -39,8 +27,5 @@ fields[field].push(error); | ||
| } | ||
| export function format(template) { | ||
| for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
| args[_key - 1] = arguments[_key]; | ||
| } | ||
| var i = 0; | ||
| var len = args.length; | ||
| export function format(template, ...args) { | ||
| let i = 0; | ||
| const len = args.length; | ||
| if (typeof template === 'function') { | ||
@@ -51,3 +36,3 @@ // eslint-disable-next-line prefer-spread | ||
| if (typeof template === 'string') { | ||
| var str = template.replace(formatRegExp, function (x) { | ||
| const str = template.replace(formatRegExp, x => { | ||
| if (x === '%%') { | ||
@@ -98,7 +83,7 @@ return '%'; | ||
| function asyncParallelArray(arr, func, callback) { | ||
| var results = []; | ||
| var total = 0; | ||
| var arrLength = arr.length; | ||
| const results = []; | ||
| let total = 0; | ||
| const arrLength = arr.length; | ||
| function count(errors) { | ||
| results.push.apply(results, _toConsumableArray(errors || [])); | ||
| results.push(...(errors || [])); | ||
| total++; | ||
@@ -109,3 +94,3 @@ if (total === arrLength) { | ||
| } | ||
| arr.forEach(function (a) { | ||
| arr.forEach(a => { | ||
| func(a, count); | ||
@@ -115,4 +100,4 @@ }); | ||
| function asyncSerialArray(arr, func, callback) { | ||
| var index = 0; | ||
| var arrLength = arr.length; | ||
| let index = 0; | ||
| const arrLength = arr.length; | ||
| function next(errors) { | ||
@@ -123,3 +108,3 @@ if (errors && errors.length) { | ||
| } | ||
| var original = index; | ||
| const original = index; | ||
| index = index + 1; | ||
@@ -135,45 +120,37 @@ if (original < arrLength) { | ||
| function flattenObjArr(objArr) { | ||
| var ret = []; | ||
| Object.keys(objArr).forEach(function (k) { | ||
| ret.push.apply(ret, _toConsumableArray(objArr[k] || [])); | ||
| const ret = []; | ||
| Object.keys(objArr).forEach(k => { | ||
| ret.push(...(objArr[k] || [])); | ||
| }); | ||
| return ret; | ||
| } | ||
| export var AsyncValidationError = /*#__PURE__*/function (_Error) { | ||
| _inherits(AsyncValidationError, _Error); | ||
| var _super = _createSuper(AsyncValidationError); | ||
| function AsyncValidationError(errors, fields) { | ||
| var _this; | ||
| _classCallCheck(this, AsyncValidationError); | ||
| _this = _super.call(this, 'Async Validation Error'); | ||
| _defineProperty(_assertThisInitialized(_this), "errors", void 0); | ||
| _defineProperty(_assertThisInitialized(_this), "fields", void 0); | ||
| _this.errors = errors; | ||
| _this.fields = fields; | ||
| return _this; | ||
| export class AsyncValidationError extends Error { | ||
| errors; | ||
| fields; | ||
| constructor(errors, fields) { | ||
| super('Async Validation Error'); | ||
| this.errors = errors; | ||
| this.fields = fields; | ||
| } | ||
| return _createClass(AsyncValidationError); | ||
| }( /*#__PURE__*/_wrapNativeSuper(Error)); | ||
| } | ||
| export function asyncMap(objArr, option, func, callback, source) { | ||
| if (option.first) { | ||
| var _pending = new Promise(function (resolve, reject) { | ||
| var next = function next(errors) { | ||
| const pending = new Promise((resolve, reject) => { | ||
| const next = errors => { | ||
| callback(errors); | ||
| return errors.length ? reject(new AsyncValidationError(errors, convertFieldsError(errors))) : resolve(source); | ||
| }; | ||
| var flattenArr = flattenObjArr(objArr); | ||
| const flattenArr = flattenObjArr(objArr); | ||
| asyncSerialArray(flattenArr, func, next); | ||
| }); | ||
| _pending.catch(function (e) { | ||
| return e; | ||
| }); | ||
| return _pending; | ||
| pending.catch(e => e); | ||
| return pending; | ||
| } | ||
| var firstFields = option.firstFields === true ? Object.keys(objArr) : option.firstFields || []; | ||
| var objArrKeys = Object.keys(objArr); | ||
| var objArrLength = objArrKeys.length; | ||
| var total = 0; | ||
| var results = []; | ||
| var pending = new Promise(function (resolve, reject) { | ||
| var next = function next(errors) { | ||
| const firstFields = option.firstFields === true ? Object.keys(objArr) : option.firstFields || []; | ||
| const objArrKeys = Object.keys(objArr); | ||
| const objArrLength = objArrKeys.length; | ||
| let total = 0; | ||
| const results = []; | ||
| const pending = new Promise((resolve, reject) => { | ||
| const next = errors => { | ||
| // eslint-disable-next-line prefer-spread | ||
@@ -191,4 +168,4 @@ results.push.apply(results, errors); | ||
| } | ||
| objArrKeys.forEach(function (key) { | ||
| var arr = objArr[key]; | ||
| objArrKeys.forEach(key => { | ||
| const arr = objArr[key]; | ||
| if (firstFields.indexOf(key) !== -1) { | ||
@@ -201,5 +178,3 @@ asyncSerialArray(arr, func, next); | ||
| }); | ||
| pending.catch(function (e) { | ||
| return e; | ||
| }); | ||
| pending.catch(e => e); | ||
| return pending; | ||
@@ -211,4 +186,4 @@ } | ||
| function getValue(value, path) { | ||
| var v = value; | ||
| for (var i = 0; i < path.length; i++) { | ||
| let v = value; | ||
| for (let i = 0; i < path.length; i++) { | ||
| if (v == undefined) { | ||
@@ -222,4 +197,4 @@ return v; | ||
| export function complementError(rule, source) { | ||
| return function (oe) { | ||
| var fieldValue; | ||
| return oe => { | ||
| let fieldValue; | ||
| if (rule.fullFields) { | ||
@@ -237,3 +212,3 @@ fieldValue = getValue(source, rule.fullFields); | ||
| message: typeof oe === 'function' ? oe() : oe, | ||
| fieldValue: fieldValue, | ||
| fieldValue, | ||
| field: oe.field || rule.fullField | ||
@@ -245,7 +220,10 @@ }; | ||
| if (source) { | ||
| for (var s in source) { | ||
| for (const s in source) { | ||
| if (source.hasOwnProperty(s)) { | ||
| var value = source[s]; | ||
| if (_typeof(value) === 'object' && _typeof(target[s]) === 'object') { | ||
| target[s] = _objectSpread(_objectSpread({}, target[s]), value); | ||
| const value = source[s]; | ||
| if (typeof value === 'object' && typeof target[s] === 'object') { | ||
| target[s] = { | ||
| ...target[s], | ||
| ...value | ||
| }; | ||
| } else { | ||
@@ -252,0 +230,0 @@ target[s] = value; |
| import rules from "../rule"; | ||
| import { isEmptyValue } from "../util"; | ||
| var any = function any(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const any = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -7,0 +7,0 @@ if (isEmptyValue(value) && !rule.required) { |
| import rules from "../rule/index"; | ||
| var array = function array(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const array = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -6,0 +6,0 @@ if ((value === undefined || value === null) && !rule.required) { |
| import rules from "../rule"; | ||
| import { isEmptyValue } from "../util"; | ||
| var boolean = function boolean(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const boolean = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -7,0 +7,0 @@ if (isEmptyValue(value) && !rule.required) { |
| import rules from "../rule"; | ||
| import { isEmptyValue } from "../util"; | ||
| var date = function date(rule, value, callback, source, options) { | ||
| const date = (rule, value, callback, source, options) => { | ||
| // console.log('integer rule called %j', rule); | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| // console.log('validate on %s value', value); | ||
@@ -14,3 +14,3 @@ if (validate) { | ||
| if (!isEmptyValue(value, 'date')) { | ||
| var dateObject; | ||
| let dateObject; | ||
| if (value instanceof Date) { | ||
@@ -17,0 +17,0 @@ dateObject = value; |
| import rules from "../rule"; | ||
| import { isEmptyValue } from "../util"; | ||
| var ENUM = 'enum'; | ||
| var enumerable = function enumerable(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const ENUM = 'enum'; | ||
| const enumerable = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -8,0 +8,0 @@ if (isEmptyValue(value) && !rule.required) { |
| import rules from "../rule"; | ||
| import { isEmptyValue } from "../util"; | ||
| var floatFn = function floatFn(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const floatFn = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -7,0 +7,0 @@ if (isEmptyValue(value) && !rule.required) { |
+13
-13
@@ -17,14 +17,14 @@ import any from "./any"; | ||
| export default { | ||
| string: string, | ||
| method: method, | ||
| number: number, | ||
| boolean: boolean, | ||
| regexp: regexp, | ||
| integer: integer, | ||
| float: float, | ||
| array: array, | ||
| object: object, | ||
| string, | ||
| method, | ||
| number, | ||
| boolean, | ||
| regexp, | ||
| integer, | ||
| float, | ||
| array, | ||
| object, | ||
| enum: enumValidator, | ||
| pattern: pattern, | ||
| date: date, | ||
| pattern, | ||
| date, | ||
| url: type, | ||
@@ -34,4 +34,4 @@ hex: type, | ||
| tel: type, | ||
| required: required, | ||
| any: any | ||
| required, | ||
| any | ||
| }; |
| import rules from "../rule"; | ||
| import { isEmptyValue } from "../util"; | ||
| var integer = function integer(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const integer = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -7,0 +7,0 @@ if (isEmptyValue(value) && !rule.required) { |
| import rules from "../rule"; | ||
| import { isEmptyValue } from "../util"; | ||
| var method = function method(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const method = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -7,0 +7,0 @@ if (isEmptyValue(value) && !rule.required) { |
| import rules from "../rule"; | ||
| import { isEmptyValue } from "../util"; | ||
| var number = function number(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const number = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -7,0 +7,0 @@ if (value === '') { |
| import rules from "../rule"; | ||
| import { isEmptyValue } from "../util"; | ||
| var object = function object(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const object = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -7,0 +7,0 @@ if (isEmptyValue(value) && !rule.required) { |
| import rules from "../rule"; | ||
| import { isEmptyValue } from "../util"; | ||
| var pattern = function pattern(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const pattern = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -7,0 +7,0 @@ if (isEmptyValue(value, 'string') && !rule.required) { |
| import rules from "../rule"; | ||
| import { isEmptyValue } from "../util"; | ||
| var regexp = function regexp(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const regexp = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -7,0 +7,0 @@ if (isEmptyValue(value) && !rule.required) { |
@@ -1,6 +0,5 @@ | ||
| import _typeof from "@babel/runtime/helpers/esm/typeof"; | ||
| import rules from "../rule"; | ||
| var required = function required(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var type = Array.isArray(value) ? 'array' : _typeof(value); | ||
| const required = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const type = Array.isArray(value) ? 'array' : typeof value; | ||
| rules.required(rule, value, source, errors, options, type); | ||
@@ -7,0 +6,0 @@ callback(errors); |
| import rules from "../rule"; | ||
| import { isEmptyValue } from "../util"; | ||
| var string = function string(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const string = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -7,0 +7,0 @@ if (isEmptyValue(value, 'string') && !rule.required) { |
| import rules from "../rule"; | ||
| import { isEmptyValue } from "../util"; | ||
| var type = function type(rule, value, callback, source, options) { | ||
| var ruleType = rule.type; | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const type = (rule, value, callback, source, options) => { | ||
| const ruleType = rule.type; | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -8,0 +8,0 @@ if (isEmptyValue(value, ruleType) && !rule.required) { |
+234
-252
@@ -9,9 +9,3 @@ "use strict"; | ||
| exports.default = void 0; | ||
| var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); | ||
| var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); | ||
| var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); | ||
| var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); | ||
| var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); | ||
| var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
| var _messages2 = require("./messages"); | ||
| var _messages = require("./messages"); | ||
| var _util = require("./util"); | ||
@@ -26,3 +20,3 @@ var _index = _interopRequireDefault(require("./validator/index")); | ||
| enumerable: true, | ||
| get: function get() { | ||
| get: function () { | ||
| return _interface[key]; | ||
@@ -38,276 +32,264 @@ } | ||
| */ | ||
| var Schema = /*#__PURE__*/function () { | ||
| function Schema(descriptor) { | ||
| (0, _classCallCheck2.default)(this, Schema); | ||
| // ======================== Instance ======================== | ||
| (0, _defineProperty2.default)(this, "rules", null); | ||
| (0, _defineProperty2.default)(this, "_messages", _messages2.messages); | ||
| class Schema { | ||
| // ========================= Static ========================= | ||
| static register = function register(type, validator) { | ||
| if (typeof validator !== 'function') { | ||
| throw new Error('Cannot register a validator by type, validator is not a function'); | ||
| } | ||
| _index.default[type] = validator; | ||
| }; | ||
| static warning = _util.warning; | ||
| static messages = _messages.messages; | ||
| static validators = _index.default; | ||
| // ======================== Instance ======================== | ||
| rules = null; | ||
| _messages = _messages.messages; | ||
| constructor(descriptor) { | ||
| this.define(descriptor); | ||
| } | ||
| (0, _createClass2.default)(Schema, [{ | ||
| key: "define", | ||
| value: function define(rules) { | ||
| var _this = this; | ||
| if (!rules) { | ||
| throw new Error('Cannot configure a schema with no rules'); | ||
| } | ||
| if ((0, _typeof2.default)(rules) !== 'object' || Array.isArray(rules)) { | ||
| throw new Error('Rules must be an object'); | ||
| } | ||
| this.rules = {}; | ||
| Object.keys(rules).forEach(function (name) { | ||
| var item = rules[name]; | ||
| _this.rules[name] = Array.isArray(item) ? item : [item]; | ||
| }); | ||
| define(rules) { | ||
| if (!rules) { | ||
| throw new Error('Cannot configure a schema with no rules'); | ||
| } | ||
| }, { | ||
| key: "messages", | ||
| value: function messages(_messages) { | ||
| if (_messages) { | ||
| this._messages = (0, _util.deepMerge)((0, _messages2.newMessages)(), _messages); | ||
| if (typeof rules !== 'object' || Array.isArray(rules)) { | ||
| throw new Error('Rules must be an object'); | ||
| } | ||
| this.rules = {}; | ||
| Object.keys(rules).forEach(name => { | ||
| const item = rules[name]; | ||
| this.rules[name] = Array.isArray(item) ? item : [item]; | ||
| }); | ||
| } | ||
| messages(messages) { | ||
| if (messages) { | ||
| this._messages = (0, _util.deepMerge)((0, _messages.newMessages)(), messages); | ||
| } | ||
| return this._messages; | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/unified-signatures | ||
| validate(source_, o = {}, oc = () => {}) { | ||
| let source = source_; | ||
| let options = o; | ||
| let callback = oc; | ||
| if (typeof options === 'function') { | ||
| callback = options; | ||
| options = {}; | ||
| } | ||
| if (!this.rules || Object.keys(this.rules).length === 0) { | ||
| if (callback) { | ||
| callback(null, source); | ||
| } | ||
| return this._messages; | ||
| return Promise.resolve(source); | ||
| } | ||
| }, { | ||
| key: "validate", | ||
| value: function validate(source_) { | ||
| var _this2 = this; | ||
| var o = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
| var oc = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () {}; | ||
| var source = source_; | ||
| var options = o; | ||
| var callback = oc; | ||
| if (typeof options === 'function') { | ||
| callback = options; | ||
| options = {}; | ||
| } | ||
| if (!this.rules || Object.keys(this.rules).length === 0) { | ||
| if (callback) { | ||
| callback(null, source); | ||
| } | ||
| return Promise.resolve(source); | ||
| } | ||
| function complete(results) { | ||
| var errors = []; | ||
| var fields = {}; | ||
| function add(e) { | ||
| if (Array.isArray(e)) { | ||
| var _errors; | ||
| errors = (_errors = errors).concat.apply(_errors, (0, _toConsumableArray2.default)(e)); | ||
| } else { | ||
| errors.push(e); | ||
| } | ||
| } | ||
| for (var i = 0; i < results.length; i++) { | ||
| add(results[i]); | ||
| } | ||
| if (!errors.length) { | ||
| callback(null, source); | ||
| function complete(results) { | ||
| let errors = []; | ||
| let fields = {}; | ||
| function add(e) { | ||
| if (Array.isArray(e)) { | ||
| errors = errors.concat(...e); | ||
| } else { | ||
| fields = (0, _util.convertFieldsError)(errors); | ||
| callback(errors, fields); | ||
| errors.push(e); | ||
| } | ||
| } | ||
| if (options.messages) { | ||
| var messages = this.messages(); | ||
| if (messages === _messages2.messages) { | ||
| messages = (0, _messages2.newMessages)(); | ||
| } | ||
| (0, _util.deepMerge)(messages, options.messages); | ||
| options.messages = messages; | ||
| for (let i = 0; i < results.length; i++) { | ||
| add(results[i]); | ||
| } | ||
| if (!errors.length) { | ||
| callback(null, source); | ||
| } else { | ||
| options.messages = this.messages(); | ||
| fields = (0, _util.convertFieldsError)(errors); | ||
| callback(errors, fields); | ||
| } | ||
| var series = {}; | ||
| var keys = options.keys || Object.keys(this.rules); | ||
| keys.forEach(function (z) { | ||
| var arr = _this2.rules[z]; | ||
| var value = source[z]; | ||
| arr.forEach(function (r) { | ||
| var rule = r; | ||
| if (typeof rule.transform === 'function') { | ||
| if (source === source_) { | ||
| source = (0, _objectSpread2.default)({}, source); | ||
| } | ||
| value = source[z] = rule.transform(value); | ||
| if (value !== undefined && value !== null) { | ||
| rule.type = rule.type || (Array.isArray(value) ? 'array' : (0, _typeof2.default)(value)); | ||
| } | ||
| } | ||
| if (typeof rule === 'function') { | ||
| rule = { | ||
| validator: rule | ||
| } | ||
| if (options.messages) { | ||
| let messages = this.messages(); | ||
| if (messages === _messages.messages) { | ||
| messages = (0, _messages.newMessages)(); | ||
| } | ||
| (0, _util.deepMerge)(messages, options.messages); | ||
| options.messages = messages; | ||
| } else { | ||
| options.messages = this.messages(); | ||
| } | ||
| const series = {}; | ||
| const keys = options.keys || Object.keys(this.rules); | ||
| keys.forEach(z => { | ||
| const arr = this.rules[z]; | ||
| let value = source[z]; | ||
| arr.forEach(r => { | ||
| let rule = r; | ||
| if (typeof rule.transform === 'function') { | ||
| if (source === source_) { | ||
| source = { | ||
| ...source | ||
| }; | ||
| } else { | ||
| rule = (0, _objectSpread2.default)({}, rule); | ||
| } | ||
| value = source[z] = rule.transform(value); | ||
| if (value !== undefined && value !== null) { | ||
| rule.type = rule.type || (Array.isArray(value) ? 'array' : typeof value); | ||
| } | ||
| } | ||
| if (typeof rule === 'function') { | ||
| rule = { | ||
| validator: rule | ||
| }; | ||
| } else { | ||
| rule = { | ||
| ...rule | ||
| }; | ||
| } | ||
| // Fill validator. Skip if nothing need to validate | ||
| rule.validator = _this2.getValidationMethod(rule); | ||
| if (!rule.validator) { | ||
| return; | ||
| } | ||
| rule.field = z; | ||
| rule.fullField = rule.fullField || z; | ||
| rule.type = _this2.getType(rule); | ||
| series[z] = series[z] || []; | ||
| series[z].push({ | ||
| rule: rule, | ||
| value: value, | ||
| source: source, | ||
| field: z | ||
| }); | ||
| // Fill validator. Skip if nothing need to validate | ||
| rule.validator = this.getValidationMethod(rule); | ||
| if (!rule.validator) { | ||
| return; | ||
| } | ||
| rule.field = z; | ||
| rule.fullField = rule.fullField || z; | ||
| rule.type = this.getType(rule); | ||
| series[z] = series[z] || []; | ||
| series[z].push({ | ||
| rule, | ||
| value, | ||
| source, | ||
| field: z | ||
| }); | ||
| }); | ||
| var errorFields = {}; | ||
| return (0, _util.asyncMap)(series, options, function (data, doIt) { | ||
| var rule = data.rule; | ||
| var deep = (rule.type === 'object' || rule.type === 'array') && ((0, _typeof2.default)(rule.fields) === 'object' || (0, _typeof2.default)(rule.defaultField) === 'object'); | ||
| deep = deep && (rule.required || !rule.required && data.value); | ||
| rule.field = data.field; | ||
| function addFullField(key, schema) { | ||
| return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, schema), {}, { | ||
| fullField: "".concat(rule.fullField, ".").concat(key), | ||
| fullFields: rule.fullFields ? [].concat((0, _toConsumableArray2.default)(rule.fullFields), [key]) : [key] | ||
| }); | ||
| }); | ||
| const errorFields = {}; | ||
| return (0, _util.asyncMap)(series, options, (data, doIt) => { | ||
| const rule = data.rule; | ||
| let deep = (rule.type === 'object' || rule.type === 'array') && (typeof rule.fields === 'object' || typeof rule.defaultField === 'object'); | ||
| deep = deep && (rule.required || !rule.required && data.value); | ||
| rule.field = data.field; | ||
| function addFullField(key, schema) { | ||
| return { | ||
| ...schema, | ||
| fullField: `${rule.fullField}.${key}`, | ||
| fullFields: rule.fullFields ? [...rule.fullFields, key] : [key] | ||
| }; | ||
| } | ||
| function cb(e = []) { | ||
| let errorList = Array.isArray(e) ? e : [e]; | ||
| if (!options.suppressWarning && errorList.length) { | ||
| Schema.warning('async-validator:', errorList); | ||
| } | ||
| function cb() { | ||
| var e = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
| var errorList = Array.isArray(e) ? e : [e]; | ||
| if (!options.suppressWarning && errorList.length) { | ||
| Schema.warning('async-validator:', errorList); | ||
| } | ||
| if (errorList.length && rule.message !== undefined && rule.message !== null) { | ||
| errorList = [].concat(rule.message); | ||
| } | ||
| if (errorList.length && rule.message !== undefined && rule.message !== null) { | ||
| errorList = [].concat(rule.message); | ||
| } | ||
| // Fill error info | ||
| var filledErrors = errorList.map((0, _util.complementError)(rule, source)); | ||
| if (options.first && filledErrors.length) { | ||
| errorFields[rule.field] = 1; | ||
| // Fill error info | ||
| let filledErrors = errorList.map((0, _util.complementError)(rule, source)); | ||
| if (options.first && filledErrors.length) { | ||
| errorFields[rule.field] = 1; | ||
| return doIt(filledErrors); | ||
| } | ||
| if (!deep) { | ||
| doIt(filledErrors); | ||
| } 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 !== undefined) { | ||
| filledErrors = [].concat(rule.message).map((0, _util.complementError)(rule, source)); | ||
| } else if (options.error) { | ||
| filledErrors = [options.error(rule, (0, _util.format)(options.messages.required, rule.field))]; | ||
| } | ||
| return doIt(filledErrors); | ||
| } | ||
| if (!deep) { | ||
| doIt(filledErrors); | ||
| } 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 !== undefined) { | ||
| filledErrors = [].concat(rule.message).map((0, _util.complementError)(rule, source)); | ||
| } else if (options.error) { | ||
| filledErrors = [options.error(rule, (0, _util.format)(options.messages.required, rule.field))]; | ||
| } | ||
| return doIt(filledErrors); | ||
| let fieldsSchema = {}; | ||
| if (rule.defaultField) { | ||
| Object.keys(data.value).map(key => { | ||
| fieldsSchema[key] = rule.defaultField; | ||
| }); | ||
| } | ||
| fieldsSchema = { | ||
| ...fieldsSchema, | ||
| ...data.rule.fields | ||
| }; | ||
| const paredFieldsSchema = {}; | ||
| Object.keys(fieldsSchema).forEach(field => { | ||
| const fieldSchema = fieldsSchema[field]; | ||
| const fieldSchemaList = Array.isArray(fieldSchema) ? fieldSchema : [fieldSchema]; | ||
| paredFieldsSchema[field] = fieldSchemaList.map(addFullField.bind(null, field)); | ||
| }); | ||
| const schema = new Schema(paredFieldsSchema); | ||
| schema.messages(options.messages); | ||
| if (data.rule.options) { | ||
| data.rule.options.messages = options.messages; | ||
| data.rule.options.error = options.error; | ||
| } | ||
| schema.validate(data.value, data.rule.options || options, errs => { | ||
| const finalErrors = []; | ||
| if (filledErrors && filledErrors.length) { | ||
| finalErrors.push(...filledErrors); | ||
| } | ||
| var fieldsSchema = {}; | ||
| if (rule.defaultField) { | ||
| Object.keys(data.value).map(function (key) { | ||
| fieldsSchema[key] = rule.defaultField; | ||
| }); | ||
| if (errs && errs.length) { | ||
| finalErrors.push(...errs); | ||
| } | ||
| fieldsSchema = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, fieldsSchema), data.rule.fields); | ||
| var paredFieldsSchema = {}; | ||
| Object.keys(fieldsSchema).forEach(function (field) { | ||
| var fieldSchema = fieldsSchema[field]; | ||
| var fieldSchemaList = Array.isArray(fieldSchema) ? fieldSchema : [fieldSchema]; | ||
| paredFieldsSchema[field] = fieldSchemaList.map(addFullField.bind(null, field)); | ||
| }); | ||
| var schema = new Schema(paredFieldsSchema); | ||
| schema.messages(options.messages); | ||
| if (data.rule.options) { | ||
| data.rule.options.messages = options.messages; | ||
| data.rule.options.error = options.error; | ||
| } | ||
| schema.validate(data.value, data.rule.options || options, function (errs) { | ||
| var finalErrors = []; | ||
| if (filledErrors && filledErrors.length) { | ||
| finalErrors.push.apply(finalErrors, (0, _toConsumableArray2.default)(filledErrors)); | ||
| } | ||
| if (errs && errs.length) { | ||
| finalErrors.push.apply(finalErrors, (0, _toConsumableArray2.default)(errs)); | ||
| } | ||
| doIt(finalErrors.length ? finalErrors : null); | ||
| }); | ||
| } | ||
| doIt(finalErrors.length ? finalErrors : null); | ||
| }); | ||
| } | ||
| var res; | ||
| if (rule.asyncValidator) { | ||
| res = rule.asyncValidator(rule, data.value, cb, data.source, options); | ||
| } else if (rule.validator) { | ||
| try { | ||
| res = rule.validator(rule, data.value, cb, data.source, options); | ||
| } catch (error) { | ||
| var _console$error, _console; | ||
| (_console$error = (_console = console).error) === null || _console$error === void 0 || _console$error.call(_console, error); | ||
| // rethrow to report error | ||
| if (!options.suppressValidatorError) { | ||
| setTimeout(function () { | ||
| throw error; | ||
| }, 0); | ||
| } | ||
| cb(error.message); | ||
| } | ||
| let res; | ||
| if (rule.asyncValidator) { | ||
| res = rule.asyncValidator(rule, data.value, cb, data.source, options); | ||
| } else if (rule.validator) { | ||
| try { | ||
| res = rule.validator(rule, data.value, cb, data.source, options); | ||
| } catch (error) { | ||
| console.error?.(error); | ||
| // rethrow to report error | ||
| if (!options.suppressValidatorError) { | ||
| setTimeout(() => { | ||
| throw error; | ||
| }, 0); | ||
| } | ||
| if (res === true) { | ||
| cb(); | ||
| } else if (res === false) { | ||
| cb(typeof rule.message === 'function' ? rule.message(rule.fullField || rule.field) : rule.message || "".concat(rule.fullField || rule.field, " fails")); | ||
| } else if (res instanceof Array) { | ||
| cb(res); | ||
| } else if (res instanceof Error) { | ||
| cb(res.message); | ||
| } | ||
| cb(error.message); | ||
| } | ||
| if (res && res.then) { | ||
| res.then(function () { | ||
| return cb(); | ||
| }, function (e) { | ||
| return cb(e); | ||
| }); | ||
| if (res === true) { | ||
| cb(); | ||
| } else if (res === false) { | ||
| cb(typeof rule.message === 'function' ? rule.message(rule.fullField || rule.field) : rule.message || `${rule.fullField || rule.field} fails`); | ||
| } else if (res instanceof Array) { | ||
| cb(res); | ||
| } else if (res instanceof Error) { | ||
| cb(res.message); | ||
| } | ||
| }, function (results) { | ||
| complete(results); | ||
| }, source); | ||
| } | ||
| }, { | ||
| key: "getType", | ||
| value: function getType(rule) { | ||
| if (rule.type === undefined && rule.pattern instanceof RegExp) { | ||
| rule.type = 'pattern'; | ||
| } | ||
| if (typeof rule.validator !== 'function' && rule.type && !_index.default.hasOwnProperty(rule.type)) { | ||
| throw new Error((0, _util.format)('Unknown rule type %s', rule.type)); | ||
| if (res && res.then) { | ||
| res.then(() => cb(), e => cb(e)); | ||
| } | ||
| return rule.type || 'string'; | ||
| }, results => { | ||
| complete(results); | ||
| }, source); | ||
| } | ||
| getType(rule) { | ||
| if (rule.type === undefined && rule.pattern instanceof RegExp) { | ||
| rule.type = 'pattern'; | ||
| } | ||
| }, { | ||
| key: "getValidationMethod", | ||
| value: function getValidationMethod(rule) { | ||
| if (typeof rule.validator === 'function') { | ||
| return rule.validator; | ||
| } | ||
| var keys = Object.keys(rule); | ||
| var messageIndex = keys.indexOf('message'); | ||
| if (messageIndex !== -1) { | ||
| keys.splice(messageIndex, 1); | ||
| } | ||
| if (keys.length === 1 && keys[0] === 'required') { | ||
| return _index.default.required; | ||
| } | ||
| return _index.default[this.getType(rule)] || undefined; | ||
| if (typeof rule.validator !== 'function' && rule.type && !_index.default.hasOwnProperty(rule.type)) { | ||
| throw new Error((0, _util.format)('Unknown rule type %s', rule.type)); | ||
| } | ||
| }]); | ||
| return Schema; | ||
| }(); | ||
| // ========================= Static ========================= | ||
| (0, _defineProperty2.default)(Schema, "register", function register(type, validator) { | ||
| if (typeof validator !== 'function') { | ||
| throw new Error('Cannot register a validator by type, validator is not a function'); | ||
| return rule.type || 'string'; | ||
| } | ||
| _index.default[type] = validator; | ||
| }); | ||
| (0, _defineProperty2.default)(Schema, "warning", _util.warning); | ||
| (0, _defineProperty2.default)(Schema, "messages", _messages2.messages); | ||
| (0, _defineProperty2.default)(Schema, "validators", _index.default); | ||
| getValidationMethod(rule) { | ||
| if (typeof rule.validator === 'function') { | ||
| return rule.validator; | ||
| } | ||
| const keys = Object.keys(rule); | ||
| const messageIndex = keys.indexOf('message'); | ||
| if (messageIndex !== -1) { | ||
| keys.splice(messageIndex, 1); | ||
| } | ||
| if (keys.length === 1 && keys[0] === 'required') { | ||
| return _index.default.required; | ||
| } | ||
| return _index.default[this.getType(rule)] || undefined; | ||
| } | ||
| } | ||
| var _default = exports.default = Schema; |
+3
-3
@@ -56,4 +56,4 @@ "use strict"; | ||
| }, | ||
| clone: function clone() { | ||
| var cloned = JSON.parse(JSON.stringify(this)); | ||
| clone() { | ||
| const cloned = JSON.parse(JSON.stringify(this)); | ||
| cloned.clone = this.clone; | ||
@@ -64,2 +64,2 @@ return cloned; | ||
| } | ||
| var messages = exports.messages = newMessages(); | ||
| const messages = exports.messages = newMessages(); |
+2
-2
@@ -8,4 +8,4 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var ENUM = 'enum'; | ||
| var enumerable = function enumerable(rule, value, source, errors, options) { | ||
| const ENUM = 'enum'; | ||
| const enumerable = (rule, value, source, errors, options) => { | ||
| rule[ENUM] = Array.isArray(rule[ENUM]) ? rule[ENUM] : []; | ||
@@ -12,0 +12,0 @@ if (rule[ENUM].indexOf(value) === -1) { |
@@ -8,3 +8,3 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var pattern = function pattern(rule, value, source, errors, options) { | ||
| const pattern = (rule, value, source, errors, options) => { | ||
| if (rule.pattern) { | ||
@@ -20,3 +20,3 @@ if (rule.pattern instanceof RegExp) { | ||
| } else if (typeof rule.pattern === 'string') { | ||
| var _pattern = new RegExp(rule.pattern); | ||
| const _pattern = new RegExp(rule.pattern); | ||
| if (!_pattern.test(value)) { | ||
@@ -23,0 +23,0 @@ errors.push((0, _util.format)(options.messages.pattern.mismatch, rule.fullField, value, rule.pattern)); |
+10
-10
@@ -8,13 +8,13 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var range = function range(rule, value, source, errors, options) { | ||
| var len = typeof rule.len === 'number'; | ||
| var min = typeof rule.min === 'number'; | ||
| var max = typeof rule.max === 'number'; | ||
| const range = (rule, value, source, errors, options) => { | ||
| const len = typeof rule.len === 'number'; | ||
| const min = typeof rule.min === 'number'; | ||
| const max = typeof rule.max === 'number'; | ||
| // 正则匹配码点范围从U+010000一直到U+10FFFF的文字(补充平面Supplementary Plane) | ||
| var spRegexp = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g; | ||
| var val = value; | ||
| var key = null; | ||
| var num = typeof value === 'number'; | ||
| var str = typeof value === 'string'; | ||
| var arr = Array.isArray(value); | ||
| 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); | ||
| if (num) { | ||
@@ -21,0 +21,0 @@ key = 'number'; |
@@ -8,3 +8,3 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var required = function required(rule, value, source, errors, options, type) { | ||
| const required = (rule, value, source, errors, options, type) => { | ||
| if (rule.required && (!source.hasOwnProperty(rule.field) || (0, _util.isEmptyValue)(value, type || rule.type))) { | ||
@@ -11,0 +11,0 @@ errors.push((0, _util.format)(options.messages.required, rule.fullField)); |
+19
-20
@@ -8,3 +8,2 @@ "use strict"; | ||
| exports.default = void 0; | ||
| var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); | ||
| var _util = require("../util"); | ||
@@ -15,3 +14,3 @@ var _required = _interopRequireDefault(require("./required")); | ||
| var pattern = { | ||
| const pattern = { | ||
| // http://emailregex.com/ | ||
@@ -32,13 +31,13 @@ email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+\.)+[a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}))$/, | ||
| }; | ||
| var types = { | ||
| integer: function integer(value) { | ||
| const types = { | ||
| integer(value) { | ||
| return types.number(value) && parseInt(value, 10) === value; | ||
| }, | ||
| float: function float(value) { | ||
| float(value) { | ||
| return types.number(value) && !types.integer(value); | ||
| }, | ||
| array: function array(value) { | ||
| array(value) { | ||
| return Array.isArray(value); | ||
| }, | ||
| regexp: function regexp(value) { | ||
| regexp(value) { | ||
| if (value instanceof RegExp) { | ||
@@ -53,6 +52,6 @@ return true; | ||
| }, | ||
| date: function date(value) { | ||
| date(value) { | ||
| return typeof value.getTime === 'function' && typeof value.getMonth === 'function' && typeof value.getYear === 'function' && !isNaN(value.getTime()); | ||
| }, | ||
| number: function number(value) { | ||
| number(value) { | ||
| if (isNaN(value)) { | ||
@@ -63,22 +62,22 @@ return false; | ||
| }, | ||
| object: function object(value) { | ||
| return (0, _typeof2.default)(value) === 'object' && !types.array(value); | ||
| object(value) { | ||
| return typeof value === 'object' && !types.array(value); | ||
| }, | ||
| method: function method(value) { | ||
| method(value) { | ||
| return typeof value === 'function'; | ||
| }, | ||
| email: function email(value) { | ||
| email(value) { | ||
| return typeof value === 'string' && value.length <= 320 && !!value.match(pattern.email); | ||
| }, | ||
| tel: function tel(value) { | ||
| tel(value) { | ||
| return typeof value === 'string' && value.length <= 32 && !!value.match(pattern.tel); | ||
| }, | ||
| url: function url(value) { | ||
| url(value) { | ||
| return typeof value === 'string' && value.length <= 2048 && !!value.match((0, _url.default)()); | ||
| }, | ||
| hex: function hex(value) { | ||
| hex(value) { | ||
| return typeof value === 'string' && !!value.match(pattern.hex); | ||
| } | ||
| }; | ||
| var type = function type(rule, value, source, errors, options) { | ||
| const type = (rule, value, source, errors, options) => { | ||
| if (rule.required && value === undefined) { | ||
@@ -88,4 +87,4 @@ (0, _required.default)(rule, value, source, errors, options); | ||
| } | ||
| var custom = ['integer', 'float', 'array', 'regexp', 'object', 'method', 'email', 'tel', 'number', 'date', 'url', 'hex']; | ||
| var ruleType = rule.type; | ||
| const custom = ['integer', 'float', 'array', 'regexp', 'object', 'method', 'email', 'tel', 'number', 'date', 'url', 'hex']; | ||
| const ruleType = rule.type; | ||
| if (custom.indexOf(ruleType) > -1) { | ||
@@ -96,3 +95,3 @@ if (!types[ruleType](value)) { | ||
| // straight typeof check | ||
| } else if (ruleType && (0, _typeof2.default)(value) !== rule.type) { | ||
| } else if (ruleType && typeof value !== rule.type) { | ||
| errors.push((0, _util.format)(options.messages.types[ruleType], rule.fullField, rule.type)); | ||
@@ -99,0 +98,0 @@ } |
+42
-42
@@ -8,51 +8,51 @@ "use strict"; | ||
| // https://github.com/kevva/url-regex/blob/master/index.js | ||
| var urlReg; | ||
| var _default = exports.default = function _default() { | ||
| let urlReg; | ||
| var _default = () => { | ||
| if (urlReg) { | ||
| return urlReg; | ||
| } | ||
| var word = '[a-fA-F\\d:]'; | ||
| var b = function b(options) { | ||
| return options && options.includeBoundaries ? "(?:(?<=\\s|^)(?=".concat(word, ")|(?<=").concat(word, ")(?=\\s|$))") : ''; | ||
| }; | ||
| var v4 = '(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}'; | ||
| var v6seg = '[a-fA-F\\d]{1,4}'; | ||
| var v6List = ["(?:".concat(v6seg, ":){7}(?:").concat(v6seg, "|:)"), // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8 | ||
| "(?:".concat(v6seg, ":){6}(?:").concat(v4, "|:").concat(v6seg, "|:)"), // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6:: | ||
| "(?:".concat(v6seg, ":){5}(?::").concat(v4, "|(?::").concat(v6seg, "){1,2}|:)"), // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5:: | ||
| "(?:".concat(v6seg, ":){4}(?:(?::").concat(v6seg, "){0,1}:").concat(v4, "|(?::").concat(v6seg, "){1,3}|:)"), // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4:: | ||
| "(?:".concat(v6seg, ":){3}(?:(?::").concat(v6seg, "){0,2}:").concat(v4, "|(?::").concat(v6seg, "){1,4}|:)"), // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3:: | ||
| "(?:".concat(v6seg, ":){2}(?:(?::").concat(v6seg, "){0,3}:").concat(v4, "|(?::").concat(v6seg, "){1,5}|:)"), // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2:: | ||
| "(?:".concat(v6seg, ":){1}(?:(?::").concat(v6seg, "){0,4}:").concat(v4, "|(?::").concat(v6seg, "){1,6}|:)"), // 1:: 1::3:4:5:6:7:8 1::8 1:: | ||
| "(?::(?:(?::".concat(v6seg, "){0,5}:").concat(v4, "|(?::").concat(v6seg, "){1,7}|:))") // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 :: | ||
| const word = '[a-fA-F\\d:]'; | ||
| const b = options => options && options.includeBoundaries ? `(?:(?<=\\s|^)(?=${word})|(?<=${word})(?=\\s|$))` : ''; | ||
| const v4 = '(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}'; | ||
| const v6seg = '[a-fA-F\\d]{1,4}'; | ||
| const v6List = [`(?:${v6seg}:){7}(?:${v6seg}|:)`, | ||
| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8 | ||
| `(?:${v6seg}:){6}(?:${v4}|:${v6seg}|:)`, | ||
| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6:: | ||
| `(?:${v6seg}:){5}(?::${v4}|(?::${v6seg}){1,2}|:)`, | ||
| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5:: | ||
| `(?:${v6seg}:){4}(?:(?::${v6seg}){0,1}:${v4}|(?::${v6seg}){1,3}|:)`, | ||
| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4:: | ||
| `(?:${v6seg}:){3}(?:(?::${v6seg}){0,2}:${v4}|(?::${v6seg}){1,4}|:)`, | ||
| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3:: | ||
| `(?:${v6seg}:){2}(?:(?::${v6seg}){0,3}:${v4}|(?::${v6seg}){1,5}|:)`, | ||
| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2:: | ||
| `(?:${v6seg}:){1}(?:(?::${v6seg}){0,4}:${v4}|(?::${v6seg}){1,6}|:)`, | ||
| // 1:: 1::3:4:5:6:7:8 1::8 1:: | ||
| `(?::(?:(?::${v6seg}){0,5}:${v4}|(?::${v6seg}){1,7}|:))` // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 :: | ||
| ]; | ||
| var v6Eth0 = "(?:%[0-9a-zA-Z]{1,})?"; // %eth0 %1 | ||
| const v6Eth0 = `(?:%[0-9a-zA-Z]{1,})?`; // %eth0 %1 | ||
| var v6 = "(?:".concat(v6List.join('|'), ")").concat(v6Eth0); | ||
| const v6 = `(?:${v6List.join('|')})${v6Eth0}`; | ||
| // Pre-compile only the exact regexes because adding a global flag make regexes stateful | ||
| var v46Exact = new RegExp("(?:^".concat(v4, "$)|(?:^").concat(v6, "$)")); | ||
| var v4exact = new RegExp("^".concat(v4, "$")); | ||
| var v6exact = new RegExp("^".concat(v6, "$")); | ||
| var ip = function ip(options) { | ||
| return options && options.exact ? v46Exact : new RegExp("(?:".concat(b(options)).concat(v4).concat(b(options), ")|(?:").concat(b(options)).concat(v6).concat(b(options), ")"), 'g'); | ||
| }; | ||
| ip.v4 = function (options) { | ||
| return options && options.exact ? v4exact : new RegExp("".concat(b(options)).concat(v4).concat(b(options)), 'g'); | ||
| }; | ||
| ip.v6 = function (options) { | ||
| return options && options.exact ? v6exact : new RegExp("".concat(b(options)).concat(v6).concat(b(options)), 'g'); | ||
| }; | ||
| var protocol = "(?:(?:[a-z]+:)?//)"; | ||
| var auth = '(?:\\S+(?::\\S*)?@)?'; | ||
| var ipv4 = ip.v4().source; | ||
| var ipv6 = ip.v6().source; | ||
| var host = "(?:(?:[a-z\\u00a1-\\uffff0-9][-_]*)*[a-z\\u00a1-\\uffff0-9]+)"; | ||
| var domain = "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*"; | ||
| var tld = "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))"; | ||
| var port = '(?::\\d{2,5})?'; | ||
| var path = '(?:[/?#][^\\s"]*)?'; | ||
| var regex = "(?:".concat(protocol, "|www\\.)").concat(auth, "(?:localhost|").concat(ipv4, "|").concat(ipv6, "|").concat(host).concat(domain).concat(tld, ")").concat(port).concat(path); | ||
| urlReg = new RegExp("(?:^".concat(regex, "$)"), 'i'); | ||
| const v46Exact = new RegExp(`(?:^${v4}$)|(?:^${v6}$)`); | ||
| const v4exact = new RegExp(`^${v4}$`); | ||
| const v6exact = new RegExp(`^${v6}$`); | ||
| const ip = options => options && options.exact ? v46Exact : new RegExp(`(?:${b(options)}${v4}${b(options)})|(?:${b(options)}${v6}${b(options)})`, 'g'); | ||
| ip.v4 = options => options && options.exact ? v4exact : new RegExp(`${b(options)}${v4}${b(options)}`, 'g'); | ||
| ip.v6 = options => options && options.exact ? v6exact : new RegExp(`${b(options)}${v6}${b(options)}`, 'g'); | ||
| const protocol = `(?:(?:[a-z]+:)?//)`; | ||
| const auth = '(?:\\S+(?::\\S*)?@)?'; | ||
| const ipv4 = ip.v4().source; | ||
| const ipv6 = ip.v6().source; | ||
| const host = '(?:(?:[a-z\\u00a1-\\uffff0-9][-_]*)*[a-z\\u00a1-\\uffff0-9]+)'; | ||
| const domain = '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*'; | ||
| const tld = `(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))`; | ||
| const port = '(?::\\d{2,5})?'; | ||
| const path = '(?:[/?#][^\\s"]*)?'; | ||
| const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ipv4}|${ipv6}|${host}${domain}${tld})${port}${path}`; | ||
| urlReg = new RegExp(`(?:^${regex}$)`, 'i'); | ||
| return urlReg; | ||
| }; | ||
| }; | ||
| exports.default = _default; |
@@ -19,3 +19,3 @@ "use strict"; | ||
| */ | ||
| var whitespace = function whitespace(rule, value, source, errors, options) { | ||
| const whitespace = (rule, value, source, errors, options) => { | ||
| if (/^\s+$/.test(value) || value === '') { | ||
@@ -22,0 +22,0 @@ errors.push((0, _util.format)(options.messages.whitespace, rule.fullField)); |
+59
-80
| "use strict"; | ||
| var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; | ||
| Object.defineProperty(exports, "__esModule", { | ||
@@ -16,24 +15,13 @@ value: true | ||
| exports.warning = void 0; | ||
| var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); | ||
| var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); | ||
| var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); | ||
| var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); | ||
| var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); | ||
| var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); | ||
| var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper")); | ||
| var _wrapNativeSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/wrapNativeSuper")); | ||
| var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
| var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); | ||
| /* eslint no-console:0 */ | ||
| var formatRegExp = /%[sdj%]/g; | ||
| var warning = exports.warning = function warning() {}; | ||
| const formatRegExp = /%[sdj%]/g; | ||
| let warning = () => {}; | ||
| // don't print warning message when in production env or node runtime | ||
| exports.warning = warning; | ||
| if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV !== 'production' && typeof window !== 'undefined' && typeof document !== 'undefined') { | ||
| exports.warning = warning = function warning(type, errors) { | ||
| exports.warning = warning = (type, errors) => { | ||
| if (typeof console !== 'undefined' && console.warn && typeof ASYNC_VALIDATOR_NO_WARNING === 'undefined') { | ||
| if (errors.every(function (e) { | ||
| return typeof e === 'string'; | ||
| })) { | ||
| if (errors.every(e => typeof e === 'string')) { | ||
| console.warn(type, errors); | ||
@@ -46,5 +34,5 @@ } | ||
| if (!errors || !errors.length) return null; | ||
| var fields = {}; | ||
| errors.forEach(function (error) { | ||
| var field = error.field; | ||
| const fields = {}; | ||
| errors.forEach(error => { | ||
| const field = error.field; | ||
| fields[field] = fields[field] || []; | ||
@@ -55,8 +43,5 @@ fields[field].push(error); | ||
| } | ||
| function format(template) { | ||
| for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
| args[_key - 1] = arguments[_key]; | ||
| } | ||
| var i = 0; | ||
| var len = args.length; | ||
| function format(template, ...args) { | ||
| let i = 0; | ||
| const len = args.length; | ||
| if (typeof template === 'function') { | ||
@@ -67,3 +52,3 @@ // eslint-disable-next-line prefer-spread | ||
| if (typeof template === 'string') { | ||
| var str = template.replace(formatRegExp, function (x) { | ||
| const str = template.replace(formatRegExp, x => { | ||
| if (x === '%%') { | ||
@@ -114,7 +99,7 @@ return '%'; | ||
| function asyncParallelArray(arr, func, callback) { | ||
| var results = []; | ||
| var total = 0; | ||
| var arrLength = arr.length; | ||
| const results = []; | ||
| let total = 0; | ||
| const arrLength = arr.length; | ||
| function count(errors) { | ||
| results.push.apply(results, (0, _toConsumableArray2.default)(errors || [])); | ||
| results.push(...(errors || [])); | ||
| total++; | ||
@@ -125,3 +110,3 @@ if (total === arrLength) { | ||
| } | ||
| arr.forEach(function (a) { | ||
| arr.forEach(a => { | ||
| func(a, count); | ||
@@ -131,4 +116,4 @@ }); | ||
| function asyncSerialArray(arr, func, callback) { | ||
| var index = 0; | ||
| var arrLength = arr.length; | ||
| let index = 0; | ||
| const arrLength = arr.length; | ||
| function next(errors) { | ||
@@ -139,3 +124,3 @@ if (errors && errors.length) { | ||
| } | ||
| var original = index; | ||
| const original = index; | ||
| index = index + 1; | ||
@@ -151,45 +136,38 @@ if (original < arrLength) { | ||
| function flattenObjArr(objArr) { | ||
| var ret = []; | ||
| Object.keys(objArr).forEach(function (k) { | ||
| ret.push.apply(ret, (0, _toConsumableArray2.default)(objArr[k] || [])); | ||
| const ret = []; | ||
| Object.keys(objArr).forEach(k => { | ||
| ret.push(...(objArr[k] || [])); | ||
| }); | ||
| return ret; | ||
| } | ||
| var AsyncValidationError = exports.AsyncValidationError = /*#__PURE__*/function (_Error) { | ||
| (0, _inherits2.default)(AsyncValidationError, _Error); | ||
| var _super = (0, _createSuper2.default)(AsyncValidationError); | ||
| function AsyncValidationError(errors, fields) { | ||
| var _this; | ||
| (0, _classCallCheck2.default)(this, AsyncValidationError); | ||
| _this = _super.call(this, 'Async Validation Error'); | ||
| (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "errors", void 0); | ||
| (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "fields", void 0); | ||
| _this.errors = errors; | ||
| _this.fields = fields; | ||
| return _this; | ||
| class AsyncValidationError extends Error { | ||
| errors; | ||
| fields; | ||
| constructor(errors, fields) { | ||
| super('Async Validation Error'); | ||
| this.errors = errors; | ||
| this.fields = fields; | ||
| } | ||
| return (0, _createClass2.default)(AsyncValidationError); | ||
| }( /*#__PURE__*/(0, _wrapNativeSuper2.default)(Error)); | ||
| } | ||
| exports.AsyncValidationError = AsyncValidationError; | ||
| function asyncMap(objArr, option, func, callback, source) { | ||
| if (option.first) { | ||
| var _pending = new Promise(function (resolve, reject) { | ||
| var next = function next(errors) { | ||
| const pending = new Promise((resolve, reject) => { | ||
| const next = errors => { | ||
| callback(errors); | ||
| return errors.length ? reject(new AsyncValidationError(errors, convertFieldsError(errors))) : resolve(source); | ||
| }; | ||
| var flattenArr = flattenObjArr(objArr); | ||
| const flattenArr = flattenObjArr(objArr); | ||
| asyncSerialArray(flattenArr, func, next); | ||
| }); | ||
| _pending.catch(function (e) { | ||
| return e; | ||
| }); | ||
| return _pending; | ||
| pending.catch(e => e); | ||
| return pending; | ||
| } | ||
| var firstFields = option.firstFields === true ? Object.keys(objArr) : option.firstFields || []; | ||
| var objArrKeys = Object.keys(objArr); | ||
| var objArrLength = objArrKeys.length; | ||
| var total = 0; | ||
| var results = []; | ||
| var pending = new Promise(function (resolve, reject) { | ||
| var next = function next(errors) { | ||
| const firstFields = option.firstFields === true ? Object.keys(objArr) : option.firstFields || []; | ||
| const objArrKeys = Object.keys(objArr); | ||
| const objArrLength = objArrKeys.length; | ||
| let total = 0; | ||
| const results = []; | ||
| const pending = new Promise((resolve, reject) => { | ||
| const next = errors => { | ||
| // eslint-disable-next-line prefer-spread | ||
@@ -207,4 +185,4 @@ results.push.apply(results, errors); | ||
| } | ||
| objArrKeys.forEach(function (key) { | ||
| var arr = objArr[key]; | ||
| objArrKeys.forEach(key => { | ||
| const arr = objArr[key]; | ||
| if (firstFields.indexOf(key) !== -1) { | ||
@@ -217,5 +195,3 @@ asyncSerialArray(arr, func, next); | ||
| }); | ||
| pending.catch(function (e) { | ||
| return e; | ||
| }); | ||
| pending.catch(e => e); | ||
| return pending; | ||
@@ -227,4 +203,4 @@ } | ||
| function getValue(value, path) { | ||
| var v = value; | ||
| for (var i = 0; i < path.length; i++) { | ||
| let v = value; | ||
| for (let i = 0; i < path.length; i++) { | ||
| if (v == undefined) { | ||
@@ -238,4 +214,4 @@ return v; | ||
| function complementError(rule, source) { | ||
| return function (oe) { | ||
| var fieldValue; | ||
| return oe => { | ||
| let fieldValue; | ||
| if (rule.fullFields) { | ||
@@ -253,3 +229,3 @@ fieldValue = getValue(source, rule.fullFields); | ||
| message: typeof oe === 'function' ? oe() : oe, | ||
| fieldValue: fieldValue, | ||
| fieldValue, | ||
| field: oe.field || rule.fullField | ||
@@ -261,7 +237,10 @@ }; | ||
| if (source) { | ||
| for (var s in source) { | ||
| for (const s in source) { | ||
| if (source.hasOwnProperty(s)) { | ||
| var value = source[s]; | ||
| if ((0, _typeof2.default)(value) === 'object' && (0, _typeof2.default)(target[s]) === 'object') { | ||
| target[s] = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, target[s]), value); | ||
| const value = source[s]; | ||
| if (typeof value === 'object' && typeof target[s] === 'object') { | ||
| target[s] = { | ||
| ...target[s], | ||
| ...value | ||
| }; | ||
| } else { | ||
@@ -268,0 +247,0 @@ target[s] = value; |
@@ -10,5 +10,5 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var any = function any(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const any = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -15,0 +15,0 @@ if ((0, _util.isEmptyValue)(value) && !rule.required) { |
@@ -9,5 +9,5 @@ "use strict"; | ||
| var _index = _interopRequireDefault(require("../rule/index")); | ||
| var array = function array(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const array = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -14,0 +14,0 @@ if ((value === undefined || value === null) && !rule.required) { |
@@ -10,5 +10,5 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var boolean = function boolean(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const boolean = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -15,0 +15,0 @@ if ((0, _util.isEmptyValue)(value) && !rule.required) { |
@@ -10,6 +10,6 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var date = function date(rule, value, callback, source, options) { | ||
| const date = (rule, value, callback, source, options) => { | ||
| // console.log('integer rule called %j', rule); | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| // console.log('validate on %s value', value); | ||
@@ -22,3 +22,3 @@ if (validate) { | ||
| if (!(0, _util.isEmptyValue)(value, 'date')) { | ||
| var dateObject; | ||
| let dateObject; | ||
| if (value instanceof Date) { | ||
@@ -25,0 +25,0 @@ dateObject = value; |
@@ -10,6 +10,6 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var ENUM = 'enum'; | ||
| var enumerable = function enumerable(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const ENUM = 'enum'; | ||
| const enumerable = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -16,0 +16,0 @@ if ((0, _util.isEmptyValue)(value) && !rule.required) { |
@@ -10,5 +10,5 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var floatFn = function floatFn(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const floatFn = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -15,0 +15,0 @@ if ((0, _util.isEmptyValue)(value) && !rule.required) { |
@@ -10,5 +10,5 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var integer = function integer(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const integer = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -15,0 +15,0 @@ if ((0, _util.isEmptyValue)(value) && !rule.required) { |
@@ -10,5 +10,5 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var method = function method(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const method = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -15,0 +15,0 @@ if ((0, _util.isEmptyValue)(value) && !rule.required) { |
@@ -10,5 +10,5 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var number = function number(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const number = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -15,0 +15,0 @@ if (value === '') { |
@@ -10,5 +10,5 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var object = function object(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const object = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -15,0 +15,0 @@ if ((0, _util.isEmptyValue)(value) && !rule.required) { |
@@ -10,5 +10,5 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var pattern = function pattern(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const pattern = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -15,0 +15,0 @@ if ((0, _util.isEmptyValue)(value, 'string') && !rule.required) { |
@@ -10,5 +10,5 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var regexp = function regexp(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const regexp = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -15,0 +15,0 @@ if ((0, _util.isEmptyValue)(value) && !rule.required) { |
@@ -8,7 +8,6 @@ "use strict"; | ||
| exports.default = void 0; | ||
| var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); | ||
| var _rule = _interopRequireDefault(require("../rule")); | ||
| var required = function required(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var type = Array.isArray(value) ? 'array' : (0, _typeof2.default)(value); | ||
| const required = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const type = Array.isArray(value) ? 'array' : typeof value; | ||
| _rule.default.required(rule, value, source, errors, options, type); | ||
@@ -15,0 +14,0 @@ callback(errors); |
@@ -10,5 +10,5 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var string = function string(rule, value, callback, source, options) { | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const string = (rule, value, callback, source, options) => { | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -15,0 +15,0 @@ if ((0, _util.isEmptyValue)(value, 'string') && !rule.required) { |
@@ -10,6 +10,6 @@ "use strict"; | ||
| var _util = require("../util"); | ||
| var type = function type(rule, value, callback, source, options) { | ||
| var ruleType = rule.type; | ||
| var errors = []; | ||
| var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| const type = (rule, value, callback, source, options) => { | ||
| const ruleType = rule.type; | ||
| const errors = []; | ||
| const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field); | ||
| if (validate) { | ||
@@ -16,0 +16,0 @@ if ((0, _util.isEmptyValue)(value, ruleType) && !rule.required) { |
+4
-4
| { | ||
| "name": "@rc-component/async-validator", | ||
| "version": "5.1.0", | ||
| "version": "5.1.1", | ||
| "description": "Validate form asynchronous", | ||
@@ -32,3 +32,3 @@ "keywords": [ | ||
| "now-build": "npm run docs:build", | ||
| "prepublishOnly": "npm run compile && np --yolo --no-publish", | ||
| "prepublishOnly": "npm run compile && rc-np", | ||
| "prettier": "prettier . --write", | ||
@@ -53,3 +53,4 @@ "postpublish": "npm run gh-pages", | ||
| "devDependencies": { | ||
| "@rc-component/father-plugin": "^1.0.2", | ||
| "@rc-component/father-plugin": "^2.2.0", | ||
| "@rc-component/np": "^1.0.3", | ||
| "@types/jest": "^29.5.12", | ||
@@ -64,3 +65,2 @@ "@types/react": "18.x", | ||
| "lint-staged": "^15.2.2", | ||
| "np": "^10.0.3", | ||
| "prettier": "^3.2.5", | ||
@@ -67,0 +67,0 @@ "rc-test": "^7.0.15", |
125829
-7.61%2925
-2.99%