class-validator
class-validator copied to clipboard
fix: @IsEnum() together with IsNotEmpty() not working if input is empty ("")
Description
Version: 0.13.2
If is use @IsEnum() to validate a string enum, validation succeeds if i validate an empty input ""
Minimal code-snippet showcasing the problem
export enum ArticleStatus {
stop = 'stop',
start = 'start',
...
export class UpdateArticleDTO {
@IsNotEmpty()
@IsEnum(ArticleStatus)
status: ArticleStatus;
}
I try to validate { "status": "" }
Expected behavior
Validation should throw an error because of an empty input like for strings
{"isNotEmpty":"status should not be empty"}
Actual behavior
Validation passes without an error
Any news on this? This still doesn't work in 0.3.5
Any updates?
You need to npm install -S class-transformer
and then useGlobalPipes as below
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe()); // add this pipe
await app.listen(3008);
}
bootstrap();
I cannot reproduce. This is working as expected for me:
import { IsEnum, IsNotEmpty, validate } from 'class-validator';
export enum MyEnum {
stop = 'stop',
start = 'start',
}
class MyPayload {
@IsNotEmpty()
@IsEnum(MyEnum)
value?: string;
constructor(value?: string) {
this.value = value;
}
}
// Will pass as expected
validate(new MyPayload('stop')).then(console.log);
validate(new MyPayload('start')).then(console.log);
// Will fail as expected
validate(new MyPayload()).then(console.log);
validate(new MyPayload('')).then(console.log);
validate(new MyPayload('random value')).then(console.log);
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.