From 6362c8b1e53ff933f50826c6e147b9b2a52816c6 Mon Sep 17 00:00:00 2001 From: wangyaqi Date: Mon, 19 Jun 2023 16:37:19 +0800 Subject: [PATCH] fix: uni-id-co validator mixin error --- .../uni-id-co/common/validator.js | 98 ++++++++++--------- .../cloudfunctions/uni-id-co/index.obj.js | 10 +- 2 files changed, 56 insertions(+), 52 deletions(-) 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 6aae560..55a0535 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 60cae07..7f6c258 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上,方便后续调用 -- GitLab