我想使用我的架构在其他字段的基础上验证一个字段。就像,如果 carType 是“SUV”,则最大“noOfPassengers”应该是 6,否则是 4。但是,它没有验证这种情况,我既无法调试它,也无法在控制台中收到任何错误。
const validationSchema = yup.object().shape({
location: yup
.string()
.required('location is required'),
destination: yup
.string()
.required('Destination is required'),
carType: yup
.string(),
noOfPassengers: yup
.string()
.when('carType', {
is: value => value && value === "SUV",
then: yup
.string()
.max(6, 'Max 6 passengers are required'),
otherwise: yup
.string()
.max(4, 'Max 4 passengers are required'),
}),
});
Run Code Online (Sandbox Code Playgroud)