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.

Validation for array returns error for only 1st item and ignores other errors

See original GitHub issue

Hi, I am using schema-validation with express-validator 4.3.0, using a jsonSchema.json file. Within the schema, there is an array of objects. express-validator returns an error if only the 1st object value is incorrect. If other objects i.e 2nd onwards (index : 1 ) has incorrect values, it does not return errors.

Using code as :

  req.checkBody(jsonSchema);
  return req.asyncValidationErrors();

eg: jsonSchema

 {
"property1":  {
    "optional": true
  },
  "propertytest.*": {
    "isURL": true
  }
}

Data:

{
"property1":  {
  "test":"testvalue"
},
"propertytest": [
   "http://sldjfsdjf.sldfjsdf.com/sdlfjskd",
    "test"
]
}

I have looked at the code and it seems this is because in express_validator.js: following code picks up only the first value from selectFields() as selectFields(req, context)[0]

return function(req, res, next) {
    const contexts = [];
    function runContexts() {
      contexts.filter(context => !context.promise).forEach(context => {
        const field = selectFields(req, context)[0];

Can you please let me know if there is a workaround for this and an example of it.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
gustavohenkecommented, Jan 23, 2018

Potential fix with a7156181a5262342b7abdd1ba7cb80da85680bc3. Will let you guys know when it’s published.

0reactions
lock[bot]commented, May 31, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Class-validator - validate array of objects - Stack Overflow
Hi, I tried something similar and is working as expected. But I noticed that when the array contains an empty array (as opposed...
Read more >
Validation - Laravel - The PHP Framework For Web Artisans
Validating Nested Array Input; Error Message Indexes & Positions ... Laravel provides several different approaches to validate your application's incoming ...
Read more >
CWE-129: Improper Validation of Array Index (4.9) - MITRE
The product uses untrusted input when calculating or using an array index, but the product does not validate or incorrectly validates the index...
Read more >
Handling operation errors - Apollo GraphQL Docs
If a syntax error or validation error occurs, your server doesn't execute the operation at all because it's invalid. If resolver errors occur,...
Read more >
The 10 Most Common JavaScript Issues Developers Face
That's why JavaScript issues, and finding the mistakes that cause them, ... in other words, objects will be removed from memory if and...
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