【问题标题】:yup conditional validation of array是的,数组的条件验证
【发布时间】:2021-08-24 14:40:35
【问题描述】:

如果贷款寄存器为真,我想在贷款寄存器的基础上验证数组,否则数组应该验证。

yup.object().shape({
                
                loan_register: yup.boolean(),
                loans: yup.array()
                    .of(
                        yup.object().shape({
                            bank_name: yup.string().required(),
                            bank_reg_no: yup.string().required(),
                            loan_amount: yup.string().required(),

                        })
                    )

            })

【问题讨论】:

    标签: javascript reactjs validation yup


    【解决方案1】:

    of 之所以不是 yup 的导出成员,是因为 yup 需要先知道数据的类型。只有知道类型后才能使用of

    例如:array().of()string().oneOf()、等

    因此,在您的情况下,您需要提供数据类型,它将解决您的问题。

    const validationSchema = yup.object({     
                 loan_register: yup.boolean(),
                 loans: yup.array().when('loan_register', {
                              is: true,
                              then: yup.array().of(
                                      yup.object({
                                          bank_name: yup.string().required(),
                                          bank_reg_no:yup.string().required(),
                                          loan_amount:yup.string().required(),
                                   }))
                })
            })
    

    【讨论】:

      【解决方案2】:

      编辑:'当 loan_register === true 时,bank_name、bank_reg_no 和 loan_amount 必须是字符串和必填字段。'

      您可以将该要求转换为如下代码(包括 是的条件验证,使用 .when()):

          const validationSchema = yup.object().shape({        
               loan_register: yup.boolean(),
               loans: yup.array()
              .when('loan_register', {
                  is: true,
                  then: yup.of(
                      yup.object().shape({
                          bank_name: yup.string().required(),
                          bank_reg_no: yup.string().required(),
                          loan_amount: yup.string().required(),
                      })
                   )
              })
          })
      

      【讨论】:

      • 如果你能解释一下你对代码做了什么以及为什么这样做会更有帮助。
      • 代码自行解释。当("loan_register") 为真时,bank_name、bank_reg_no 和 loan_amount 必须作为字符串进行验证并且必须是必需的。与原始问题相比,我只添加了一个条件。你不必仅仅因为这个而否决我的回答。我正在努力提供帮助!
      • "虽然此代码 sn-p 可能是解决方案,但包含解释确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。” ——我也不打算投反对票,那是我的错误。如果您编辑答案以包含简短说明,我认为这将清除反对票(或允许我清除它)。
      • @Ani Attempted import error: 'of' is not exported from 'yup' (imported as 'yup'). 您提供的方法出现此错误我的导入语句是`import * as yup from 'yup'`
      • @FaizanAhmed,你添加了这些导入吗?从“@hookform/resolvers/yup”导入 { yupResolver };从“yup”导入*作为yup;另外,能否告诉我 react-hook-form 和 @hookform/resolvers 的版本?
      【解决方案3】:

      我想你会想要使用.when()。通过提供基于其他属性值的条件验证检查,这完全可以满足您的需求。

      它有一个更明确的方法,你可以添加

      .when('loan_register', {is: true, then: /* yup schema */ })
      

      我相信变化会是这样的

      yup.object().shape({
          loan_register: yup.boolean(),
          loans: yup.array()
              .when('loan_register', {
                  is: true,
                  then: yup.of(
                      yup.object().shape({
                          bank_name: yup.string().required(),
                          bank_reg_no: yup.string().required(),
                          loan_amount: yup.string().required(),
                      })
                  )
              })
      })
      

      【讨论】:

      • Attempted import error: 'of' is not exported from 'yup' (imported as 'yup'). 您提供的方法出现此错误我的导入语句是`import * as yup from 'yup'`
      猜你喜欢
      • 2018-12-31
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 2018-03-20
      • 2021-06-24
      相关资源
      最近更新 更多