1

我尝试使用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")
      })
    })
  })
});
4

1 回答 1

1

以下将起作用:

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")
                })
            })
        })
    });
于 2021-06-12T17:00:05.273 回答