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.

Validate array of objects?

See original GitHub issue

When I submit a single object as my payload I can validate keys very easily with:

req.checkBody('year', 'invalid year').len(4,4).isNumeric();

But I would like to submit an array of objects and loop through them with something like:

_.each(self.req.body, function(element, index, list) {
    req.checkBody([index].year, 'invalid year').len(4,4).isNumeric();
});

I’ve also tried using req.assert with element.year and even element.year.toString() with no luck.

What am I doing wrong? Or is this type of functionality even possible?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:3
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

16reactions
jyotmancommented, Feb 17, 2017

@rustybailey @gustavohenke @IOAyman I don’t think this is same issue as #125.

I think the OP was asking what if the req.body is an array of objects itself.

Example JSON -

[
    {"name": "Abc"},
    {"name": "Def"}
]

How do I begin to validate this. What argument do I provide to req.checkbody() because I want to validate the body itself. I want to do something like req.checkBody().isArray().

2reactions
gofurnazarovcommented, Jan 6, 2019

I used Wildcards in this situation and worked well.

array_of_objects:

[
  { name: 'something', value: 'anything'},
  { name: 'something', value: 'anything'},
  { name: 'something', value: 'anything'}
]

Validate array items: check('array_of_objects[*].*').trim().isLength({ min: 1, max: 10 }) etc.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to validate objects in Array - javascript - Stack Overflow
You can use Array.prototype.every for this The every() method tests whether all elements in the array pass ...
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 >
Validating an object or array of similarly-shaped objects with ...
In this brief article you will learn how to implement such validation logic with Yup.
Read more >
array - AdonisJS
You can validate the array length by using the minLength and the maxLength rules. In the following example, we accept a minimum of...
Read more >
How to validate an array of objects? · Issue #999 · jquense/yup
I have a JSON object which contains an array of JSON objects. My need is out of 2 array elements (vehicles), I just...
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