Skip to content

Optional fields with whitespace values still produce unclear validation errors #1348

@KavyashreeSN

Description

@KavyashreeSN

I noticed that there was a previously closed issue related to optional fields
and whitespace handling.

However, in current versions of express-validator, when optional() is used
together with validation chains (e.g. isInt()), fields that contain only
whitespace can still produce validation errors with messages that are not
clear for API developers.

This seems especially confusing when sanitizers like trim() are used before
or after optional().

Example:
body('age').optional().trim().isInt()

If the client sends:
age=" "

The resulting validation error does not clearly explain why the value was
considered invalid, even though the field is optional.

This is a common real-world API scenario. Improving error clarity or behavior
here would help developers avoid confusion.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions