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

Empty array causing false positive #1230

Open
RenaudAubert opened this issue Jun 30, 2023 · 3 comments · May be fixed by #1280
Open

Empty array causing false positive #1230

RenaudAubert opened this issue Jun 30, 2023 · 3 comments · May be fixed by #1280

Comments

@RenaudAubert
Copy link

RenaudAubert commented Jun 30, 2023

Describe the bug

Hi!

When validations fields I noticed that array had a different behavior than other values.

If an empty array is given no validation is run and the field is considered valid even if we're not expecting an array value.
I think this line in standard-validation.ts producing this behavior.

To Reproduce

Here's a RunKit reproducing the error.

Expected behavior

In the RunKit I'm expecting only count1 and count6 to be valid because the doc states if value is an array then each element of an array is validated.

It seems a bit weird that all types fails except arrays which means most validators needs an extra not().isArray() to prevent unexpected values in following middlewares.

Current behavior

In the RunKit , count1, count5 and count6 are valid.

Environment:

  • Express-validator version: 7.0.1
  • Express version: 4.18.2
  • Node.js version: 18.16.1

PS:
I tagged this as a bug but maybe it should be a feature request?
An option or a config to add that enable or disable the use of array values?

@gustavohenke
Copy link
Member

Understandable. The behaviour today is like Array#some()/Array#every(), in that they respectively return false/true for empty arrays, no matter what the predicate is.

An option or a config to add that enable or disable the use of array values?

This would be the most pragmatic approach. Changing the behaviour at all without such will be a breaking change.

I don't know how that'd look, but I'd be open for some brainstorming (before anyone jumps into coding).

@RenaudAubert
Copy link
Author

Hi @gustavohenke !

I'm willing to help but I don't have any experience in open-source so I'm not sure how that brainstorming would go? 😄
I might have an idea or 2 I'll try to test them when I have the time to make sure they work.

@gustavohenke
Copy link
Member

Oh, feel free to post some of the API ideas and maybe how you think they should work, and we can go from there.

Anything that's not user facing can be changed freely!

@fedeci fedeci linked a pull request Feb 16, 2024 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants