Olá, estava acompanhando a aula mas não aconteceu o "Bad Request" no Postman não sei o que fiz de errado segue o código abaixo:
Main:
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { useContainer } from 'class-validator';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe({
transform: true
}));
useContainer(app.select(AppModule), { fallbackOnErrors: true});
await app.listen(3000);
}
bootstrap()
is-user-already-taken.validator.ts:
@Injectable()
@ValidatorConstraint()
export class IsUserNameAlreadyTakenContraint implements ValidatorConstraintInterface{
constructor(private userService: UserService){}
validate(userName: string, validationArguments?: ValidationArguments,): boolean | Promise<boolean> {
return !!!this.userService.searchForUserName(userName);
}
}
export function IsUserNameAlreadyTaken(validationOptions?: ValidationOptions){
return function (object: Object, propertyName: string){
registerDecorator({
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
constraints: [],
validator: IsUserNameAlreadyTakenContraint,
})
}
}
user.Module.ts:
@Module({
controllers:[UserController],
providers:[UserService, IsUserNameAlreadyTakenContraint]
})
export class UserModule{}