提交 6362c8b1 编写于 作者: 雪洛's avatar 雪洛

fix: uni-id-co validator mixin error

上级 62754ecd
...@@ -198,52 +198,6 @@ function getRuleCategory(rule) { ...@@ -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 ~!@#$%^&*_-+=`|\(){}[]:;"'<>,.?/ // 特殊符号 https://www.ibm.com/support/pages/password-strength-rules ~!@#$%^&*_-+=`|\(){}[]:;"'<>,.?/
// const specialChar = '~!@#$%^&*_-+=`|\(){}[]:;"\'<>,.?/' // const specialChar = '~!@#$%^&*_-+=`|\(){}[]:;"\'<>,.?/'
...@@ -320,7 +274,57 @@ class Validator { ...@@ -320,7 +274,57 @@ class Validator {
return this.customValidator[type] || this.baseValidator[type] 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() { get validator() {
const _this = this
return new Proxy({}, { return new Proxy({}, {
get: (_, prop) => { get: (_, prop) => {
if (typeof prop !== 'string') { if (typeof prop !== 'string') {
...@@ -332,7 +336,7 @@ class Validator { ...@@ -332,7 +336,7 @@ class Validator {
} }
const rule = parseValidatorName(prop) const rule = parseValidatorName(prop)
return function (val) { return function (val) {
if (!isMatchUnionType(val, rule)) { if (!_this._isMatchUnionType(val, rule)) {
return { return {
errCode: ERROR.INVALID_PARAM errCode: ERROR.INVALID_PARAM
} }
......
...@@ -180,21 +180,21 @@ module.exports = { ...@@ -180,21 +180,21 @@ module.exports = {
// } // }
// }) // })
// // 新增规则同样可以在数组验证规则中使用 // // 新增规则同样可以在数组验证规则中使用
// this.validator.valdate({ // this.validator.validate({
// timestamp: 123456789 // timestamp: 123456789
// }, { // }, {
// timestamp: 'timestamp' // timestamp: 'timestamp'
// }) // })
// this.validator.valdate({ // this.validator.validate({
// timestampList: [123456789, 123123123123] // timestampList: [123456789, 123123123123]
// }, { // }, {
// timestampList: 'array<timestamp>' // timestampList: 'array<timestamp>'
// }) // })
// // 甚至更复杂的写法 // // 甚至更复杂的写法
// this.validator.valdate({ // this.validator.validate({
// timestamp: [123456789, 123123123123] // timestamp: [123456789123123123, 123123123123]
// }, { // }, {
// timestamp: 'timestamp|array<timestamp>' // timestamp: 'timestamp|array<timestamp|number>'
// }) // })
// 挂载uni-captcha到this上,方便后续调用 // 挂载uni-captcha到this上,方便后续调用
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册