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 an array prop if other prop inside the same object is true

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fedecicommented, Dec 28, 2021

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.

0reactions
thiagocolebruscocommented, Dec 28, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

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