gpt4 book ai didi

javascript - yup.js 在条件时检查数组

转载 作者:行者123 更新时间:2023-11-28 21:01:54 25 4
gpt4 key购买 nike

我有一个对象,其标志为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com