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

Unexpected change in behaviour v6/v7 for .notEmpty() #1243

Open
brianweet opened this issue Sep 12, 2023 · 1 comment · May be fixed by #1280
Open

Unexpected change in behaviour v6/v7 for .notEmpty() #1243

brianweet opened this issue Sep 12, 2023 · 1 comment · May be fixed by #1280
Labels

Comments

@brianweet
Copy link

brianweet commented Sep 12, 2023

Describe the bug

We're using .notEmpty() to validate an incoming request body. After upgrading major version from 6 to 7, our unit tests started to fail. Turns out v7 .notEmpty() does not return a validation error for an empty array.

I do not see this in the breaking changes, unless it's part of some of the other points described there

edit: after looking through the docs, it seems .notEmpty() should have been used for strings. However I think the changed behaviour is still unexpected?

To Reproduce

Here you can see the different results:

  1. Validation error for empty array on version 6.14.0: https://runkit.com/brianweet/express-validator-issue-template/6.14.0
  2. No validation error on version 7.0.1: https://runkit.com/brianweet/express-validator-issue-template/7.0.1

Expected behavior

v6 and v7 to have similar behaviour

Current behavior

v7 does not return validation error for empty array

Environment:

  • Express-validator version: 7.0.1
  • Express version:
  • Node.js version:
@brianweet brianweet changed the title Different behaviour v6/v7 using .notEmpty() to check empty array Unexpected change in behaviour v6/v7 for .notEmpty() Sep 12, 2023
@gustavohenke
Copy link
Member

gustavohenke commented Sep 16, 2023

I'm guessing this is an unexpected regression from fixing #791.

Basically, the new behaviour for standard validators is: if you have an array, then every item in the array should comply with the validator.
In the case of there being no items, then it means essentially every item is complying in not being empty...

I think that changing isEmpty() to check the array's length might make sense.

In the mean time, you can fix this by instead using the isArray({ min: 1 }) validator.

@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
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants