我有一个对象,其标志为 bool 值,另一个项目为对象数组。
我只想在标志为真时检查对象数组。
所以:
{
shouldCheck: false
}
这应该通过
{
shouldCheck: true
}
这应该会中断
{
shouldCheck: true,
rules: []
}
这应该会中断
{
shouldCheck: true,
rules: [1]
}
这应该会中断
{
shouldCheck: true,
rules: [{other: 'xx'}]
}
这应该会中断
{
shouldCheck: true,
rules: [right: 'one']
}
这应该通过
是的模式:
const delaySchema = yup.object().shape({
shouldCheck: yup.boolean(),
rules: yup.mixed()
.when(['shouldCheck'], {
is: (sck) => {
return sck;
},
then: yup.array().of(yup.object().shape({
right: yup.string().required(),
})),
otherwise: yup.mixed().nullable()
}),
});
现在这里的问题是它忽略了内部值并且不检查它们。
最佳答案
尝试在条件之前使用 yup.array()
const delaySchema = yup.object().shape({
shouldCheck: yup.boolean(),
rules: yup.array()
.when(['shouldCheck'], {
is: (sck) => {
return sck;
},
then: yup.array().of(yup.object().shape({
right: yup.string().required(),
})),
otherwise: yup.mixed().nullable()
}),
});
关于javascript - yup.js 在条件时检查数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53727239/