Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hungtcs/nest-validation-pipe
Inherits ValidationPipe of @nestjs/common, but provides override options decorator.
https://github.com/hungtcs/nest-validation-pipe
nestjs validation validation-pipe
Last synced: 27 days ago
JSON representation
Inherits ValidationPipe of @nestjs/common, but provides override options decorator.
- Host: GitHub
- URL: https://github.com/hungtcs/nest-validation-pipe
- Owner: hungtcs
- Created: 2022-06-29T10:43:46.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-07-26T10:39:36.000Z (over 2 years ago)
- Last Synced: 2024-09-28T13:42:51.097Z (about 1 month ago)
- Topics: nestjs, validation, validation-pipe
- Language: TypeScript
- Homepage:
- Size: 20.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
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
```shell
npm install nest-validation-pipe
```## Usage
```typescript
import { ValidationPipe } from 'nest-validation-pipe';async function bootstrap() {
const app = await NestFactory.create(AppModule);app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
}),
);
}
``````typescript
import { ValidationOptions } from 'nest-validation-pipe';@ValidationOptions({
// Override global validation options
whitelist: false,
})
export class SomeDTO {}
```## Implementation Principle
```typescript
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);
}
```