nest-validation-pipe
v1.0.2
Published
inherits ValidationPipe of @nestjs/common, but provides override options decorator
Downloads
5
Readme
Nest Validation Pipe
Inherits ValidationPipe
of @nestjs/common
, but provides override options decorator.
Related:
- https://github.com/nestjs/nest/issues/2390
- https://github.com/nestjs/nest/issues/7779
Installation
npm install nest-validation-pipe
Usage
import { ValidationPipe } from 'nest-validation-pipe';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
}),
);
}
import { ValidationOptions } from 'nest-validation-pipe';
@ValidationOptions({
// Override global validation options
whitelist: false,
})
export class SomeDTO {
}
Implementation Principle
https://github.com/nestjs/nest/blob/master/packages/common/pipes/validation.pipe.ts
import { ValidatorOptions, validate } from 'class-validator';
import { Injectable, SetMetadata, ValidationPipe as NestValidationPipe } from '@nestjs/common';
@Injectable()
export class ValidationPipe extends NestValidationPipe {
protected validate(object: object, validatorOptions?: ValidatorOptions) {
const overrideOptions = Reflect.getMetadata(VALIDATION_OPTIONS, object.constructor);
return validate(object, { ...validatorOptions, ...overrideOptions });
}
}
export const VALIDATION_OPTIONS = Symbol('VALIDATION_OPTIONS');
export function ValidationOptions(options: { whitelist?: boolean }) {
return SetMetadata(VALIDATION_OPTIONS, options);
}