33 lines
1008 B
TypeScript
33 lines
1008 B
TypeScript
import { PipeTransform, Injectable, ArgumentMetadata, BadRequestException } from '@nestjs/common';
|
|
import { validate } from 'class-validator';
|
|
import { plainToClass } from 'class-transformer';
|
|
|
|
@Injectable()
|
|
export class ValidationPipe implements PipeTransform<any> {
|
|
async transform(value: any, { metatype }: ArgumentMetadata) {
|
|
if (!metatype || !this.toValidate(metatype)) {
|
|
return value;
|
|
}
|
|
|
|
const object = plainToClass(metatype, value);
|
|
const errors = await validate(object);
|
|
|
|
if (errors.length > 0) {
|
|
const errorMessages = errors.map(error => ({
|
|
field: error.property,
|
|
errors: Object.values(error.constraints || {}),
|
|
}));
|
|
throw new BadRequestException({
|
|
message: 'Validation failed',
|
|
errors: errorMessages,
|
|
});
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
private toValidate(metatype: Function): boolean {
|
|
const types: Function[] = [String, Boolean, Number, Array, Object];
|
|
return !types.includes(metatype);
|
|
}
|
|
} |