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.

Can't validate array length

See original GitHub issue

Hi, kinda new to Yup and I can’t figure out how to to validate that an array is not empty. I’m using react + formik + yup + material-ui

here is an example I’ve created: https://codesandbox.io/s/new-fire-29onf?file=/src/App.js

I tried in the validationSchema to just use the required method:

validationSchema={Yup.object({ permissions : Yup.array().required('permission cant be empty') })}

i tried to add my functionally using the test method like this:

validationSchema={Yup.object({ permission: Yup.array().test ('notEmptyArr', 'array is empty', (value) =>{ console.log(value); return value.length > 0; }) })} i also tried to add method to the array like this:

Yup.addMethod(Yup.array, "notEmpty", function(message) { return this.test("notEmpty", message, function(arr) { return Boolean( arr.length > 0 ); }); });

But none of that worked for me : (

if I remove the validation I see the value.permission is indeed an array, with values (if selected)

what am I doing wrong?

thanks

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

8reactions
rodrigograca31commented, Nov 12, 2021

UPDATE / WARNING:

Previously only array().required() was required to make sure there was at leas 1 item in the array. For example [] would not pass the test.

⚠️ As of version 0.31.0 (2020-11-23) the behavior of array().required() changed! Now to make sure you have at least 1 item in the array you need to sure: array().required().min(1)

1reaction
SumiSastricommented, Feb 20, 2021

I am also facinga similar challenge - same set up Formik /React/ Yup- checked solutions on the documentation and stack overflow - this is what is recommended but it does not seem to work.

const musicGenre=[]
musicGenre: Yup.array().of(Yup.string().min(1, 'At least one box must be ticked')).required('Required')

Validation is occurring even with a null submission – log musicGenre[0]

Read more comments on GitHub >

github_iconTop Results From Across the Web

Yup (with formik and react) - Can't validate array length
Previously only array().required() was required to make sure there was at leas 1 item in the array. For example [] would not pass...
Read more >
Add array size check to validation - Laracasts
Add array size check to validation. hi, I have a voting system where each user has to vote vote for a number of...
Read more >
RangeError: invalid array length - JavaScript - MDN Web Docs
The JavaScript exception "Invalid array length" occurs when specifying an array length that is either negative, a floating number or exceeds ...
Read more >
array — Understanding JSON Schema 2020-12 documentation
There are two ways in which arrays are generally used in JSON: List validation: a sequence of arbitrary length where each item matches...
Read more >
17.7.0 API Reference - joi.dev
If the schema is a joi type, the schema.validate(value) can be called ... length(item) - reutrn the length of an array or string,...
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