question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

yup's array validation is broken

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
janhesterscommented, Jan 3, 2020

@jquense I just found the bug and its on my end. I left out strict from the array. If you change the schema to:

const yupObject = yup.object().shape({
  message: yup.string().required(),
  numbers: yup
    .array()
    .of(yup.number().required())
    .strict()
    .required(),
  tokens: yup
    .array()
    .of(
      yup.object().shape({
        name: yup.string().strict().required(),
        id: yup.string().strict().required()
      })
    )
    .strict()
    .required()
});

It does the proper validation.

0reactions
janhesterscommented, Dec 16, 2019

This looks like a real bug, the issue is that the strings are still being cast from the boolean and number, even tho they are marked as strict

Thank you! Sounds right 😊

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found