diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.js index 6aae560e641de1fa7c8e5ffc9c0b81c2ac283655..55a05354656fdd65669219c7fe3ee7912731f1be 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/validator.js @@ -198,52 +198,6 @@ function getRuleCategory(rule) { } } -function isMatchUnionType(val, rule) { - if (!rule.children || rule.children.length === 0) { - return true - } - const children = rule.children - for (let i = 0; i < children.length; i++) { - const child = children[i] - const category = getRuleCategory(child) - let pass = false - switch (category) { - case 'base': - pass = isMatchBaseType(val, child) - break - case 'array': - pass = isMatchArrayType(val, child) - break - default: - break - } - if (pass) { - return true - } - } - return false -} - -function isMatchBaseType(val, rule) { - if (typeof baseValidator[rule.type] !== 'function') { - throw new Error(`invalid schema type: ${rule.type}`) - } - const validateRes = baseValidator[rule.type](val) - if (validateRes && validateRes.errCode) { - return false - } - return true -} - -function isMatchArrayType(arr, rule) { - if (getType(arr) !== 'array') { - return false - } - if (rule.children && rule.children.length && arr.some(item => !isMatchUnionType(item, rule))) { - return false - } - return true -} // 特殊符号 https://www.ibm.com/support/pages/password-strength-rules ~!@#$%^&*_-+=`|\(){}[]:;"'<>,.?/ // const specialChar = '~!@#$%^&*_-+=`|\(){}[]:;"\'<>,.?/' @@ -320,7 +274,57 @@ class Validator { return this.customValidator[type] || this.baseValidator[type] } + + _isMatchUnionType(val, rule) { + if (!rule.children || rule.children.length === 0) { + return true + } + const children = rule.children + for (let i = 0; i < children.length; i++) { + const child = children[i] + const category = getRuleCategory(child) + let pass = false + switch (category) { + case 'base': + pass = this._isMatchBaseType(val, child) + break + case 'array': + pass = this._isMatchArrayType(val, child) + break + default: + break + } + if (pass) { + return true + } + } + return false + } + + _isMatchBaseType(val, rule) { + const method = this.getRealBaseValidator(rule.type) + if (typeof method !== 'function') { + throw new Error(`invalid schema type: ${rule.type}`) + } + const validateRes = method(val) + if (validateRes && validateRes.errCode) { + return false + } + return true + } + + _isMatchArrayType(arr, rule) { + if (getType(arr) !== 'array') { + return false + } + if (rule.children && rule.children.length && arr.some(item => !this._isMatchUnionType(item, rule))) { + return false + } + return true + } + get validator() { + const _this = this return new Proxy({}, { get: (_, prop) => { if (typeof prop !== 'string') { @@ -332,7 +336,7 @@ class Validator { } const rule = parseValidatorName(prop) return function (val) { - if (!isMatchUnionType(val, rule)) { + if (!_this._isMatchUnionType(val, rule)) { return { errCode: ERROR.INVALID_PARAM } diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js index 60cae0759bb8213da79d4db25b6baed1c65e4ae2..7f6c25825193fb6ca4f45646ba604fe092cd7940 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js @@ -180,21 +180,21 @@ module.exports = { // } // }) // // 新增规则同样可以在数组验证规则中使用 - // this.validator.valdate({ + // this.validator.validate({ // timestamp: 123456789 // }, { // timestamp: 'timestamp' // }) - // this.validator.valdate({ + // this.validator.validate({ // timestampList: [123456789, 123123123123] // }, { // timestampList: 'array' // }) // // 甚至更复杂的写法 - // this.validator.valdate({ - // timestamp: [123456789, 123123123123] + // this.validator.validate({ + // timestamp: [123456789123123123, 123123123123] // }, { - // timestamp: 'timestamp|array' + // timestamp: 'timestamp|array' // }) // 挂载uni-captcha到this上,方便后续调用