I have the following object:
{
array: [1]
}
And the following code:
myArray: Yup.array().of(
Yup.object().shape({
name: Yup.string().max(255).required().label('Name')
})
)
Now I check the name is required, I need to check if myArray
has length === 1
to return an error.
You could use mixed.test(options: object)
if you would just like to test 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,
})
Demo:
And array.min(limit: number | Ref, message?: string | function)
if you want to test 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')
Demo:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With