Skip to content

Conversation

@KavyashreeSN
Copy link

Fixes #1348

This PR improves validation behavior for optional fields when values contain
only whitespace and are trimmed to an empty string.

When optional() is used, whitespace-only values are now skipped cleanly
instead of producing confusing validation errors. The change is minimal,
backward-compatible, and covered by targeted tests.

All existing tests pass and new tests cover the reported edge cases.

@gustavohenke gustavohenke self-requested a review January 29, 2026 14:02
@coveralls
Copy link

Coverage Status

coverage: 100.0%. remained the same
when pulling 5a5ff5a on KavyashreeSN:fix-optional-whitespace
into 24da8b7 on express-validator:master.

@KavyashreeSN
Copy link
Author

Thanks for the review request!
I’m fixing the lint issue now — will update the PR shortly.

src/context.ts Outdated
(value: any) => (optional === 'null' ? value != null : true),
(value: any) => (optional === 'falsy' ? value : true),

(value: any) => !(typeof value === 'string' && value === ''),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change. Would like to avoid it. What do you think of adding a new type of optional value, called empty?

It's very similar to falsy, except it's for strings only.

Comment on lines -93 to -96
// #331 - When multiple locations are involved, all of them must pass the validation.
// If none of the locations contain the field, we at least include one for error reporting.
// #458, #531 - Wildcards are an exception though: they may yield 0..* instances with different
// paths, so we may want to skip this filtering.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is important context to retain

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this suite that's heavily mirroring your examples, you should add a test for the new option to the following suite instead:

describe('#getData()', () => {

@KavyashreeSN
Copy link
Author

Thanks for the feedback — that makes sense.

I’ll avoid changing existing optional behavior and instead add a new optional('empty') type for string-only empty values.

I’ll also restore the removed context comments and move the tests into context.spec.ts as suggested. Updating the PR now.

@KavyashreeSN
Copy link
Author

I’ve updated the implementation to avoid the breaking change and introduced a new optional('empty') mode that skips validation only for empty strings.

I restored the explanatory comments you pointed out and moved the tests into context.spec.ts under #getData(). The separate spec file has been removed.

All tests and lint checks are passing locally. Please let me know if any further adjustments are needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optional fields with whitespace values still produce unclear validation errors

4 participants