@betsys-nestjs/decorators
v2.0.0
Published
This library is a collection of various decorators using class-validator.
Downloads
3
Maintainers
Keywords
Readme
@betsys-nestjs/decorators
This is a simple library containing decorators helping you to validate types of properites
Dependencies
| Package | Version | | --------------- | ------- | | @nestjs/common | ^10.0.0 | | class-validator | ^0.13.2 |
Usage
The basic usage is similar to the class-validator. Use decorators in your DTOs or commands.
An example of usage - imagine a DTO as following:
export class SomeNiceDto {
booleanProperty!: boolean;
intProperty!: number;
};
Which you want to use in the following controller while it is automatically converted from the request:
import { Body, Controller, Post, Req } from '@nestjs/common';
import { Request } from 'express';
@Controller()
export class SomeController {
@Post('/some-url')
saveKittens(@Req() request: Request, @Body() body: SomeNiceDto) {
...
}
}
For the DTO you want to be sure, that booleanProperty
is of type boolan
as well as that intProperty
is really an integer.
You can easily enforce that by using the decorators from this lib:
import { IsFieldBoolean, IsFieldInteger, Language } from '@betsys-nestjs/decorators';
export class SomeNiceDto {
@IsFieldBoolean('booleanProperty', Language.PL)
booleanProperty!: boolean;
@IsFieldInteger('intProperty', Language.EN)
intProperty!: number;
};
The language settings concern the error message. If you do not provide any language, English is returned.