2

我的问题是,如果其中一个字段存在,我如何使用 Yup 进行验证?

    const Schema = yup.object({
      firstname: yup.string().ensure().trim().required("There must be a name"),
      lastname: yup.string().ensure().trim().required("There must be a
      lastname")
       }
      );
4

1 回答 1

1

我认为这应该足以满足您的用例。如果需要,您始终可以通过链接它们来编写额外的测试!

const Schema = yup.object({
  firstname: yup.string().ensure().trim().required("There must be a name"),
  lastname: yup.string().ensure().trim().required("There must be a
  lastname")
})
.test('at-least-one-property', "you must provide at least one", value =>
  !!(value.firstname || value.lastname)
)
于 2019-02-12T12:26:34.240 回答