2

I am using class-validator in a NestJS project and facing an issue regarding validation. I want to allow properties to accept values like null, '' (empty string), or undefined without validation. But if there's any other value, it should go through the validation decorators.

Here's an example of the properties:

@ApiProperty({ required: false })
@IsNumber()
@IsOptional()
sample_result?: number;

@ApiProperty({ required: false })
@IsString()
@IsOptional()
sample_comment?: string;

Even with @IsOptional() and @ValidateIf() decorators, I'm not achieving the desired behavior. If I send a null value for sample_result, it still throws an error like:

"isNumber": "sample_result must be a number conforming to the specified constraints"

Has anyone faced a similar issue or knows how to bypass validation for null, '', and undefined values while enforcing it for other values?

I tried using the @IsOptional() decorator from class-validator in my NestJS application to allow properties to accept values like null, '' (empty string), or undefined without any validation. My expectation was that if I provided any of these values, the validation would bypass without any errors.

However, if there was any other value provided (like a string for a number field), I expected it to undergo the validation and throw an error if it didn't match the validation rules.

Despite using the @IsOptional() decorator, whenever I send a null value for a property that is decorated with @IsNumber(), I am receiving an error:

"isNumber": "sample_result must be a number conforming to the specified constraints"

This is not what I expected, as I assumed @IsOptional() would allow null values without any issues.

5
  • do you have this line implemented app.useGlobalPipes(new ValidationPipe()); Commented Sep 19, 2023 at 4:23
  • btw, you should use IsEmpty because you want to allow empty string Commented Sep 19, 2023 at 4:27
  • app.useGlobalPipes( new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true, transformOptions: { enableImplicitConversion: true }, exceptionFactory: (errors) => { Logger.log(Received data: ${JSON.stringify(errors)}); return new BadRequestException(errors); }, }), ); Commented Sep 19, 2023 at 4:27
  • i can't see problem here, how did you get the result? swagger UI or curl? Commented Sep 19, 2023 at 4:34
  • I do it on the swagger and on the front-end client that I configured Commented Sep 19, 2023 at 4:36

1 Answer 1

2

if you want to allow properties to accept values like null, '' (empty string), or undefined without validation. The @IsOptional() decorator will allow null values but doesn't consider empty strings ('') or undefined values.

you can use a custom validation decorator @ValidateIf() decorator like this.

 @ValidateIf((o, value) => value !== null && value !== undefined && value !== '')
Sign up to request clarification or add additional context in comments.

1 Comment

@ValidateIf( (o, value) => value !== null && value !== undefined && value !== '', ) @IsDate() @ApiProperty({ required: false }) sample_receive_dt?: Date | null; Thank you. Should I write it like this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.