Validate array of objects?
See original GitHub issueWhen 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:
- Created 10 years ago
- Reactions:3
- Comments:17 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 -
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 likereq.checkBody().isArray()
.I used
Wildcards
in this situation and worked well.array_of_objects
:Validate array items:
check('array_of_objects[*].*').trim().isLength({ min: 1, max: 10 }) etc.