Validating Array objects for required fields, .exists() - doesn't seem to work
See original GitHub issueI am able to validate requests with latest version 5, works great, but facing one issue with arrays. Is there a way to force that a field must be present in array object? the exists() doesn’t seem to work for arrays.
...
return [
check('items').exists(), //WORKS GREAT
check('items.*._id').exists().isAlphanumeric(), //EXISTS() IS IGNORED BUT WILL VALIDATE IF FIELD IS PRESENT :((
check('items.*.item_type').exists().isIn(Item.ITEM_TYPES), //SAME HERE, EXISTS() IS IGNORED BUT WILL VALIDATE IF FIELD IS PRESENT :((
check('items.*.update_status').exists().isInt().isIn(Constants.UPDATE_STATUS_VALUES).withMessage("must be one of:"+JSON.stringify(Constants.UPDATE_STATUS)),
sanitize('items.*.long_desc').toString(),
];
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:14 (3 by maintainers)
Top Results From Across the Web
Class-validator - validate array of objects - Stack Overflow
Add @Type(() => AuthParam) to your array and it should be working. Type decorator is required for nested objects(arrays). Your code becomes
Read more >Validation - Laravel - The PHP Framework For Web Artisans
This object exposes only , except , and all methods to retrieve a subset of the validated data or the entire array of...
Read more >How to validate array fields? - Laracasts
Not sure how to get this to work though, I've tried: Copy Code roles' => 'array|each|exists:roles,id'. But that doesn't work.
Read more >Working with Angular 4 Forms: Nesting and Input Validation
In this article, you will learn how you can work with forms and perform form validation with ease in your Angular application. In...
Read more >Why does Mongoose remove the key when specified in the ...
Here's what happens: When I specify the schema for an array of objects (may be doing this wrong), that particular key and all...
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
@davida5 see https://github.com/ctavan/express-validator#check-api
Simply change
check()
tobody()
. Example:And it’ll check each item. For example a request with 2 invalid item and 1 valid item results in 2 errors:
Hey @davida5, the right name is
body
. See docs for more reference.Also, while skimming through the issue, the feeling I have is that
*
is only working for the first results it encounters. Is it correct?If so we’ll need some serious bug fixing 🙂