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