Yup条件验证-何时

时间:2019-04-20 17:38:50

标签: reactjs formik yup

我尝试使用when方法基于父值有条件地验证嵌套对象,但是我没有设法使其起作用。

const firstStep = Yup.object().shape({
  accountHolder: Yup.object().shape({
    accountType: Yup.number()
      .typeError("Required")
      .required("Required"),
    mailingAddress: Yup.object().shape({
      address1: Yup.string().when("accountType", {
        is: 2,
        then: Yup.string()
          .min(5, "Min 5 char")
          .required("Required")
      })
    })
  })
});

1 个答案:

答案 0 :(得分:0)

以下将起作用:

const firstStep = Yup.object().shape({
        accountHolder: Yup.object().shape({
            accountType: Yup.number()
                .typeError("Required")
                .required("Required"),
            mailingAddress: Yup.object().when("accountType", {
                is: 2,
                then: Yup.object().shape({
                    address1: Yup.string()
                        .min(5, "Min 5 char")
                        .required("Required")
                })
            })
        })
    });