Skip to content
\n

If an empty string is specified for email, both @IsNotEmpty and @IsEmail validation will fail.
\nTwo messages are generated.

\n\n

I want to skip IsEmail validation in these cases, since it is sufficient to just tell the user that it is empty.

\n

I know that this can be achieved by creating a Custom validation decorator that performs the same checks as IsEmail only if it is non-empty.

\n

If there is another, simpler solution, I would like to know.

\n

Thanks.

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

@onozaty use the stopAtFirstError option when calling validate(). this stops validation on each property after the first constraint fails, so if @IsNotEmpty already caught the empty string, @IsEmail won't run:

\n
const errors = await validate(dto, { stopAtFirstError: true });
\n

in NestJS, pass it to ValidationPipe:

\n
app.useGlobalPipes(new ValidationPipe({ stopAtFirstError: true }));
\n

this is a built-in option in class-validator v0.14+. make sure your decorators are ordered the way you want (first decorator = first check).

\n

ref: class-validator validate options

","upvoteCount":2,"url":"https://github.com/typestack/class-validator/discussions/2419#discussioncomment-15764057"}}}
Discussion options

You must be logged in to vote

@onozaty use the stopAtFirstError option when calling validate(). this stops validation on each property after the first constraint fails, so if @IsNotEmpty already caught the empty string, @IsEmail won't run:

const errors = await validate(dto, { stopAtFirstError: true });

in NestJS, pass it to ValidationPipe:

app.useGlobalPipes(new ValidationPipe({ stopAtFirstError: true }));

this is a built-in option in class-validator v0.14+. make sure your decorators are ordered the way you want (first decorator = first check).

ref: class-validator validate options

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@onozaty
Comment options

Answer selected by onozaty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants