yup's array validation is broken
See original GitHub issueDescribe the bug When using yup with an array and pass it a shape, there are no errors returned, even if the object is invalid.
To Reproduce
const invalidObj = {
message: 'hello',
numbers: ['hello'],
tokens: [{ name: 1, id: true }]
};
const yupObject = yup.object().shape({
message: yup.string().required(),
numbers: yup
.array()
.of(yup.number().required())
.required(),
tokens: yup
.array()
.of(
yup.object().shape({
name: yup.string().strict().required(),
id: yup.string().strict().required()
})
)
.required()
});
const res = await yupObject
.validate(invalidObj, { abortEarly: false })
.catch(({ errors }) => errors);
console.log('res', res);
Logs out:
res [
'numbers[0] must be a `number` type, but the final value was: `NaN` (cast from the value `"hello"`).'
]
Even though it should catch that name
and id
aren’t the correct type.
Expected behavior Yup should see the invalid types and log out errors that name and id must be strings.
Platform (please complete the following information):
- Browser: chrome
- Version: 0.27
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Formik FieldArray Yup error validation not working
Hello I am trying to achieve error validation with Yup and Formik and I cannot figure out how to get it working with...
Read more >yup array required not working - Code Examples & Solutions ...
Empty arrays are considered truthy that's why we cannot use array().required() const validationSchema = Yup.object().shape({ stringArray: Yup.array().min(1, ...
Read more >Using yup and typescript for typesafe select validation
Typescript automatically infers the string type for our array, which is actually correct because our variable might be const but adding new values...
Read more >Yup | Best of JS
Yup is a schema builder for runtime value parsing and validation. ... ValidationError(errors: string | Array<string>, value: any, path: string).
Read more >FieldArray
<FieldArray is a specialized <Field> that helps with list manipulations. ... const schema = yup.object({. friends: yup .array() .of(friend).
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jquense I just found the bug and its on my end. I left out
strict
from the array. If you change the schema to:It does the proper validation.
Thank you! Sounds right 😊