react-redux - 当布尔值为假时如何验证数字数组?
问题描述
检查布尔值时,我需要验证一个数字数组,并且我在控制台中收到此错误:
Uncaught (in promise) TypeError: Cannot read property 'length' of undefined
at yupToFormErrors (formik.esm.js:701)
这是我的是的验证架构。
export default yup.object<myFormikForm.FormValues>({
allowOther: yup.boolean(),
arrayNumbers: yup
.array<number, *>(
yup
.number()
.nullable(true)
.transform((value, originalValue) =>
originalValue === '' ? null : value,
)
.min(
minSize,
oneLine`
Must be greater or equal than
0
`,
),
)
.when('allowOther', (allowOther: boolean, schema) =>
allowOther
? schema
: schema
.required('Required')
.test(
'hasAtLeastOneValue',
'Specify at least one value',
value =>
Array.isArray(value) ? value.some(Number.isFinite) : true,
),
),
});
顺便说一下表格工作正常,但我想摆脱这个错误,有什么想法吗?谢谢。
解决方案
你可以yup.ref()
在你的情况下使用。在此处了解更多信息。
allowOther: Yup.boolean(),
arrayNumbers: Yup.array().nullable().min(Yup.ref('allowOther'), 'Must be greater or equal than 0').required(Yup.ref('allowOther'), "arrayNumbers are required"),
推荐阅读
- typescript - 如何为 lodash/fp 管道传输 Promise.allSettled?(或任何功能,如管道库,例如:p-pipe)
- php - Xdebug 断点在 Sublime Text 3 上不起作用
- reactjs - Using React, how can sibling components best share a state? Should there even be a way to traverse DOM tree...?
- javascript - Material UI:如何检查哪个选项卡处于活动状态?
- vba - 连续的参考指示符用“-”分隔符组合成字符串,第一次和最后一次看到(VBA)
- ruby-on-rails - with_lock 会锁定块内的所有内容吗?
- javascript - 使用 puppeteer 单击元素列表
- laravel-livewire - 如何在多个输入 Laravel Livewire 中更新和显示正确的值
- typescript - 打字稿,将界面键转换为可能的字符串值的枚举
- rest - HERE.com 目的地天气 API 使用新创建的 API 密钥返回“禁止”