Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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);
}
```