javascript - yup.js 条件时检查数组
问题描述
我有一个对象,其标志为布尔值,另一个项目为对象数组。
我只想在标志为真时检查对象数组。
所以:
{
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()
}),
});
现在这里的问题是它忽略了内部值并且不检查它们。
解决方案
尝试使用是的。条件前的数组()
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()
}),
});
推荐阅读
- rdf - 解析 Wikidata n-triples 数据
- webpack - Webpack less-loader javascriptEnabled 错误
- python - 如何在 Django 中更新模板变量
- javascript - 按钮有 1.04% 的机会引导到页面 A 和 98.96% 的机会引导到页面 B?-JavaScript
- json - Swagger 2.0 时出错:(Patchable)responce.description 是强制性的 - 在 Postman 中导入 Swagger 时
- cordova - 带有 Cordova iOS cordova-plugin-wkwebview-engine 插件的白色空白屏幕
- powershell - Powershell 电子邮件正文格式未按预期显示
- python - Python - 累积总和,直到总和匹配一个确切的数字
- c - 试图在该类型的数组中添加某种类型的变量
- python - Python 代码在函数和类之外工作,但不在内部