async-validator
async-validator copied to clipboard
validator 函数校验和其他基础校验一同出现时,会导致无法校验(Promise {<pending>})
import Validator from 'async-validator'
// 版本:"async-validator": "^3.5.2"
const desc= {
inputName: {
type: "url"
},
val: [
{
validator: (rule, value, callback) => {
if(value) {
if(value === '111') {
callback()
} else {
callback(new Error('值必须为: 111'))
}
}
}
}
]
}
// 场景1: console.log('ret', ret) => ret Promise {<pending>}
try {
let ret = new Validator(desc).validate({
// val: "0",
inputName: "1"
})
.then(res => {
debugger
console.log('res', res)
})
.catch( ({ errors, fields }) => {
debugger
console.log('fields', fields)
})
console.log('ret', ret)
} catch (error) {
console.log('error', error)
}
// 场景2: console.log('fields', fields) => fields {inputName: Array(1), val: Array(1)}
try {
let ret = new Validator(desc).validate({
// val: "0",
inputName: "1"
})
.then(res => {
console.log('res', res)
})
.catch( ({ errors, fields }) => {
console.log('fields', fields)
})
console.log('ret', ret)
} catch (error) {
console.log('error', error)
}
在 validator 函数校验没有问题时,我直接返回了true; 状态会变成Promise {
Still