【发布时间】:2020-02-29 21:12:29
【问题描述】:
我正在使用 array().of 方法创建 Yup 验证模式,我需要根据来自外部模式对象的值在该数组中设置一些验证规则。我怎样才能获得对该值的引用?
const termsSchema = Yup.object().shape({
termType: Yup.string().when('condition', {
is: true,
then: Yup.string()
.required('Type is required'),
}),
});
const schema = Yup.object().shape({
condition: Yup.boolean()
.required('Error'),
terms: Yup.array().of(termsSchema),
});
【问题讨论】:
-
我很确定这是不可能的,因为 according to the documentation、
.when()允许您“根据同级或同级子字段调整架构。” -
is: (value) => value == true.你可以为is使用回调,它可以访问value,你可以随意使用它。
标签: javascript formik yup