是的,充满了田野之一

时间:2019-02-12 12:08:58

标签: reactjs yup

我的问题是,如果其中一个字段存在,我如何用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")
       }
      );

1 个答案:

答案 0 :(得分:0)

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

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