我有以下对象:
{
array: [1]
}
以及以下代码:
myArray: Yup.array().of(
Yup.object().shape({
name: Yup.string().max(255).required().label('Name')
})
)
现在我检查名称是必需的,我需要检查是否
myArray
有 length === 1
返回错误。
最佳答案
您可以使用 mixed.test(options: object)
如果您只想测试 length === 1
:
myArray: array()
.of(
object().shape({
name: string()
.max(255)
.required()
.label("Name")
})
)
.test({
message: 'The error message if length === 1',
test: arr => arr.length !== 1,
})
演示:
和
array.min(limit: number | Ref, message?: string | function)
如果你想测试length === 0 | 1
:myArray: Yup.array()
.of(
Yup.object().shape({
name:Yup.string()
.max(255)
.required()
.label('Name')
})
)
.min(2, 'The error message if length === 0 | 1')
演示:
关于javascript - 使用 Yup 检查数组长度的验证 --> 如果长度 === 1 则出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62148702/