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.

I want to create array of fields but to validate array elements customly

See original GitHub issue

Hi guys,

I have a problem validating array of fields on my own, what I mean is that I have an array of fields where I don’t want to validate specific fields on index


const schema = Yup.object().shape({
  inviteUsers: Yup.array().of(Yup.object().shape({
    firstName: Yup.string().required(),
    lastName: Yup.string().required(),
    email: Yup.string().email().required(),
    userType: Yup.string().required(),
  })),
});

so, for example, I don’t what to validate the last element of the array of inviteUsers, how is it possible? I checked custom addMethod function but I didn’t find any viable solution

thank you in advance

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
knicolacommented, Nov 2, 2020

you could also use .compact() if you can identify the last entry based on a fixed set of criteria. fyi, .validate() will exclude that entry from the returned value, but the original input will remain intact.

example:

const yup = require('yup')

const input = ['one', 'two', 'three']

const schema = yup
    .array()
    .compact((value) => value === 'three')
    .of(yup.string().matches(/one|two/i))

schema
    .validate(input)
    .then(console.log)
    .catch(console.error)
0reactions
beqramocommented, Nov 3, 2020

@knicola I think I can use the last one, thanks for your time

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validating Arrays And passing field values between each other
I am attempting to validate an array from my form inputs. I will need to pass one of my fields' value to an...
Read more >
Validating arrays and nested values in Laravel - LogRocket Blog
In our form, we have fields for the item name, the SKU, and the price. We need to validate these items when a...
Read more >
How To: Validate an array of form fields with Laravel – Eric L ...
Going off the top of my head I'd think you would want to check the Input for “items” then loop Input::get('items') in your...
Read more >
How to validate array fields? - Laracasts
So I have two fields that looks like this. ... Why is it so hard with array form element ? ... Custom validation...
Read more >
5. Working with Arrays and Loops - JavaScript Cookbook [Book]
Not all array elements have to be defined when created. For instance, if you create an array literal, you can use commas to...
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