Validate an array prop if other prop inside the same object is true
See original GitHub issueDescribe the bug
I’m trying to validate a few props inside an array object only if the enabled
prop of that same object is true.
To Reproduce
My request object looks like this.
{
"propA": "",
"propB": "",
"items": [
{
"enabled": true,
"name": "",
}
]
}
I want to validate if the name prop of the array object is notEmpty only of the enabled property is set to true
.
Environment:
Express-validator version: 6.12.1 Express version: 4.17.1
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Validate an array property only if another ... - Stack Overflow
I want to be able to set the name and icon as required only if the enabled property is set to true ....
Read more >How to validate React props using PropTypes - LogRocket Blog
Learn how to validate props with React PropTypes, React's internal mechanism for adding type checking to component props.
Read more >3 Ways to Check If an Object Has a Property in JavaScript
The first way is to invoke object. hasOwnProperty(propName) . The method returns true if the propName exists inside object , and false ...
Read more >Objects - The Modern JavaScript Tutorial
Objects are associative arrays with several special features. They store properties (key-value pairs), where: ... To access a property, we can use ...
Read more >React.js TypeScript Conditional Props - YouTube
Conditional props are props that depend on other props values/types. In ... and value prop only available when data is an array of...
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
I don’t think it’s possible then but for v7 we planned to improve support for
oneOf
with wildcards. To use the same function that express-validator is using but without directly using express-validator you can import methods from ‘validator’ https://github.com/validatorjs/validator.js which is already packed up with express-validator as a dip.Thank you for your responses.
I tried this, but I couldn’t make this work. My main issue is to be able to test properties of the array’s object based on another prop value. In this example you sent, it just uses properties of the main object.