class-validator

ValidationPipe() does not work on override @Query in Nestjs/Crud

穿精又带淫゛_ 提交于 2021-02-16 15:06:52
问题 I'm trying to validate the parameters that come in the query of a get request, but for some reason, the validation pipe is unable to identify the elements of the query. import { Controller, Post, Query, Body, UseInterceptors, Param, Res, Logger, } from '@nestjs/common'; import { Crud, CrudController, Override } from '@nestjsx/crud'; import { OpenScheduleDto } from './open-schedule.dto'; @Crud(Schedule) export class ScheduleController implements CrudController<ScheduleService, Schedule> {

ValidationPipe() does not work on override @Query in Nestjs/Crud

戏子无情 提交于 2021-02-16 15:05:53
问题 I'm trying to validate the parameters that come in the query of a get request, but for some reason, the validation pipe is unable to identify the elements of the query. import { Controller, Post, Query, Body, UseInterceptors, Param, Res, Logger, } from '@nestjs/common'; import { Crud, CrudController, Override } from '@nestjsx/crud'; import { OpenScheduleDto } from './open-schedule.dto'; @Crud(Schedule) export class ScheduleController implements CrudController<ScheduleService, Schedule> {

Unable to inject Request into a class in nestjs

落花浮王杯 提交于 2020-06-28 05:36:58
问题 The class I want to inject the request into looks as follows: @ValidatorConstraint({ name: 'isUniqueEntryTitle', async: true }) @Injectable({ scope: Scope.REQUEST }) export class IsUniqueEntryTitle implements ValidatorConstraintInterface { constructor( private readonly userService: UserService, @Inject(REQUEST) private request: Request, ) {} public async validate(val: any, args: ValidationArguments): Promise<boolean> { console.log('Request', this.request); return true; } public defaultMessage

Class-validator - validate array of objects

非 Y 不嫁゛ 提交于 2020-05-25 17:22:18
问题 I am using class-validator package with NestJS and I am looking to validate an array of objects that need to have exactly 2 objects with the same layout: So far I have: import { IsString, IsNumber } from 'class-validator'; export class AuthParam { @IsNumber() id: number; @IsString() type: string; @IsString() value: string; } and import { IsArray, ValidateNested } from 'class-validator'; import { AuthParam } from './authParam.model'; export class SignIn { @IsArray() @ValidateNested({ each:

NestJS - Validating body conditionally, based on one property

匆匆过客 提交于 2020-01-13 04:43:49
问题 I'm trying to find a nice way to validate a body using DTO (using the brilliant class-validator and class-transformer libraries). It works really well, even for nested structures but in my case I'd like to have the body property based on some conditions. Example that will probably help to understand: Let's imagine my body should always have selectedCategory . Based on that field, the content could either be from category 1, which contains prop1 OR from category 2, which contains prop2 . I do

Is it possible to pass Typescript decorator object values in at runtime?

一世执手 提交于 2019-12-13 21:56:01
问题 I have a class that is decorated with a @MinDate constraint like this: export default class Order { purchaseDate: Date; @MinDate(this.purchaseDate) receiptDate: Date; } When attempting to validate an instance of Order that is valid the validation errors out. My question is is it even possible / valid to pass in this.purchaseDate as an argument to the @MinDate() decorator. In other words can typescript decorators receive runtime values from an object, or do these values have to be available at

validate nested objects using class-validator in nest.js controller

喜欢而已 提交于 2019-12-02 00:39:40
I want to validate body payload using class-validator in a nest.js controller. My currency.dto.ts file is like this: import { IsNotEmpty, IsString, ValidateNested, IsNumber, IsDefined, } from 'class-validator'; class Data { @IsNotEmpty() @IsString() type: string; @IsNotEmpty() @IsNumber() id: number; } export class CurrencyDTO { @ValidateNested({ each: true }) @IsDefined() data: Data[]; } and in my nest.js controller, I use it like this. @Post() @UseGuards(new AuthTokenGuard()) @UsePipes(new ValidationPipe()) addNewCurrency(@Req() req, @Body() data: CurrencyDTO) { console.log('data', data); }

validate nested objects using class-validator in nest.js controller

ぐ巨炮叔叔 提交于 2019-12-02 00:37:36
问题 I want to validate body payload using class-validator in a nest.js controller. My currency.dto.ts file is like this: import { IsNotEmpty, IsString, ValidateNested, IsNumber, IsDefined, } from 'class-validator'; class Data { @IsNotEmpty() @IsString() type: string; @IsNotEmpty() @IsNumber() id: number; } export class CurrencyDTO { @ValidateNested({ each: true }) @IsDefined() data: Data[]; } and in my nest.js controller, I use it like this. @Post() @UseGuards(new AuthTokenGuard()) @UsePipes(new