nestjs-validate-headers
v0.1.5
Published
A decorator meant to replace NestJS's @Headers()
Downloads
1,564
Readme
@ValidateHeaders()
NestJS's Headers decorator skips validation. This package exports a custom ValidateHeaders decorator which behaves the same as NestJS's Headers but also performs validation.
Installation
$ npm install nestjs-validate-headers`Setup
To enable validation in this decorator, you must add a global pipe to your NestJS app with validateCustomDecorators set to true. Typically this is inside your main.ts's bootstrap function. Add this to enable validation globally.
// main.ts -> bootstrap
app.useGlobalPipes(new ValidationPipe({ validateCustomDecorators: true }));Alternatively, you can enable validation per-use by passing a validation pipe to the decorator wherever you're using it. Again, be sure to enable validateCustomDecorators.
// controller.ts
@ValidateHeaders(new ValidationPipe({ validateCustomDecorators: true }))Example
main.ts
import { ValidationPipe } from '@nestjs/common';
async function bootstrap() {
// ...
app.useGlobalPipes(new ValidationPipe({ validateCustomDecorators: true })); // <- Add this if you want to enable global validation
// ...
}
bootstrap();app.controller.ts
import { Controller, Get, ValidationPipe } from '@nestjs/common';
import { ValidateHeaders } from 'nestjs-validate-headers';
import { AppDto } from './app.dto';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('/')
getHello(
@ValidateHeaders() headers: AppDto,
// @ValidateHeaders(new ValidationPipe({ validateCustomDecorators: true })) headers: AppDto, // <- Use this if you have not added global validation pipes.
) {
console.log({ headers });
// ...
}
}app.dto.ts
import { IsNumberString, IsEmail } from 'class-validator';
export class AppDto {
@IsNumberString()
'custom-header-1': `${number}`;
@IsEmail()
email: string;
}