yup.array not working

yup.array not working

So you're having trouble with yup.array? Let's see what could be causing this issue and how to solve it.

What is yup.array?

yup is a JavaScript schema validator that allows you to validate values against a schema object. yup.array is a method that allows you to define a schema for an array of values.

Possible causes of the issue

There could be a few reasons why yup.array is not working:

  • The schema is not defined correctly
  • The values being validated are not in an array format
  • There is a syntax error in the code

Solution #1: Check the schema definition

The first thing you should check is if the schema is defined correctly. Make sure that the schema object is structured properly and that the array method is being used correctly.


const schema = yup.object().shape({
  names: yup.array().of(yup.string().required())
});

const data = {
  names: ['John', 'Doe']
};

schema.validate(data)
  .then(() => console.log('Valid'))
  .catch(err => console.log(err));

Solution #2: Check the data being validated

Another reason why yup.array might not be working is because the data being validated is not in an array format. Make sure that the data being validated is an array before calling the validate method.


const schema = yup.array().of(yup.string().required());

const data = ['John', 'Doe'];

schema.validate(data)
  .then(() => console.log('Valid'))
  .catch(err => console.log(err));

Solution #3: Check for syntax errors

If the above solutions don't work, the issue could be a syntax error in the code. Make sure that the code is structured properly and that there are no syntax errors.

Using these solutions should help solve the issue with yup.array not working. If you continue to have issues, double check your code and consider reaching out to the yup community for further assistance.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe