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.

Need Help on validating array of objects

See original GitHub issue

Sample code snippet

var data = {
      users: [
        {
          name: "Sample",
          bio: {
            age: 28,
            education: {
              primary: 'Elementary School',
              secondary: 'Secondary School'
            }
          }
        },
        {
          name: undefined,
          bio: {
            age: 28,
            education: {
              primary: 'Elementary School',
              secondary: 'Secondary School'
            }
          }
        }
      ]
    }

Rules

    var rules = {
      'users.*.name': 'required',
      'users.*.bio.age': 'min:18',
      'users.*.bio.education.primary': 'string',
      'users.*.bio.education.secondary': 'string'
    }

Validate code

   var validation = new Validator(data, rules);
    validation.fails();

output

{"errors":{"users.1.name":["The users.1.name field is required."]}}

the above output is correct because i wrote a generic rule to see in each “user” object check “name” is present/not

but i want to check any one of the ‘user’ object 'name is present/not in that array. Not Sure how to accomplish this…

Please advice on the same … Thanks in advance

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mikeericksoncommented, Oct 19, 2020

@iedmrc my apologies for the confusion. I have edited the initial comment accordingly

0reactions
iedmrccommented, Oct 19, 2020

Thanks @mikeerickson for the answer!

I think you should update the above comment because it’s written We currently have native support for what you are looking for ....

Read more comments on GitHub >

github_iconTop Results From Across the Web

Class-validator - validate array of objects - Stack Overflow
Hi, any idea on how to achieve this for an array of numbers in a query param? imagine im trying to validate [www.url.com/path?ids=1,2,3]...
Read more >
Help validating array of objects · Issue #176 - GitHub
I have a array of objects that looks like this [{ name: 'some name' catId: 2, }, { name: 'another name' catId: 3,...
Read more >
Validating an array of objects - Laracasts
Validating an array of objects. Hello,. I'm making a REST endpoint which sends email, and for each recipient there needs to be both...
Read more >
array — Understanding JSON Schema 2020-12 documentation
List validation is useful for arrays of arbitrary length where each item matches the same schema. For this kind of array, set the...
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 >

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