async-validator icon indicating copy to clipboard operation
async-validator copied to clipboard

validator 函数校验和其他基础校验一同出现时,会导致无法校验(Promise {<pending>})

Open NoeyNoi opened this issue 4 years ago • 2 comments

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)
  }

NoeyNoi avatar Aug 06 '21 03:08 NoeyNoi

在 validator 函数校验没有问题时,我直接返回了true; 状态会变成Promise {: undefined}

zhanghaoranas avatar Apr 28 '22 08:04 zhanghaoranas

Still

connect-kai avatar Mar 05 '23 07:03 connect-kai