Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: oneOf returns all the errors of a group #956

Closed
kouraf opened this issue Dec 7, 2020 · 5 comments
Closed

Bug: oneOf returns all the errors of a group #956

kouraf opened this issue Dec 7, 2020 · 5 comments
Labels
Milestone

Comments

@kouraf
Copy link

kouraf commented Dec 7, 2020

Describe the bug

when using oneOf with groups of validation it returns all the errors of a group

To Reproduce

  1. use oneOf
oneOf([ 
 [
  body('name').equals('foo'),
  body('age').exists().isInt()
 ],
 [
  body('name').equals('bar'),
  body('work').exists().isString()
 ]
])
  1. send a body with one wrong filed
{
"name":"foo",
"age":"f"
}

Expected behavior

{
      "msg": "Invalid value(s)",
      "param": "_error",
      "nestedErrors": [
        {
          "value": "f",
          "msg": "Invalid value",
          "param": "age",
          "location": "body"
        }
     ]
}

Current behavior

{
     "msg": "Invalid value(s)",
     "param": "_error",
     "nestedErrors": [
       {
         "value": "f",
         "msg": "Invalid value",
         "param": "age",
         "location": "body"
       },
       {
         "value": "foo",
         "msg": "Invalid value",
         "param": "name",
         "location": "body"
       },
       {
         "msg": "Invalid value",
         "param": "work",
         "location": "body"
       },
       {
         "msg": "Invalid value",
         "param": "work",
         "location": "body"
       }
     ]
   }

Express-validator version:

  • Version: 6.6.0
@kouraf kouraf added the i: bug label Dec 7, 2020
@fedeci
Copy link
Member

fedeci commented Dec 12, 2020

Hi, I don't think this should be treated as a bug, but instead as an improvement of nestedErrors.
In particular oneOf allows to apply to validators the boolean operator 'or' and, in case of failure, to return all the errors that have been generated.
In your case you only have two properties per "branch", so you can may not want errors of the others, but if you have 5 or 6 it could be useful. What undoubtedly can be done is a further grouping of errors or a specification of the branch from which they were generated.

@gustavohenke do you have any suggestions on how to improve this?

@gustavohenke
Copy link
Member

Looks like I should have seen this a long time ago 🙃

What undoubtedly can be done is a further grouping of errors

Can you clarify, please?

or a specification of the branch from which they were generated.

This is a simple change, SGTM.

@gustavohenke
Copy link
Member

Other than these: adding an option to oneOf to either see all errors (current v6 behaviour) or just those of the least red group could SGTM if people find it useful.

@fedeci
Copy link
Member

fedeci commented Apr 17, 2021

What undoubtedly can be done is a further grouping of errors

Can you clarify, please?

I meant that we may group the errors per branch instead of showing all of them together.

@gustavohenke
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants