Questions tagged [class-validator]

Open-source TypeScript library for validation using TypeScript decorators.

class-validator

class-validator is an open-source TypeScript library for validation using TypeScript decorators.

Allows use of decorator and non-decorator based validation. Internally uses validator.js to perform validation. Class-validator works on both browser and node.js platforms.

It integrates well with its counterpart library .

384 questions
68
votes
6 answers

Class-validator - validate array of objects

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 { …
bensiu
  • 24,660
  • 56
  • 77
  • 117
63
votes
3 answers

Validate nested objects using class validator and nestjs

I'm trying to validate nested objects using class-validator and NestJS. I've already tried following this thread by using the @Type decorator from class-transform and didn't have any luck. This what I have: DTO: class PositionDto { @IsNumber() …
36
votes
8 answers

Password confirmation in TypeScript with `class-validator`

Today, I am trying to figure out how to validate a Sign Up form in the backend side (NestJS) of the app. I am just wondering if exists a way to validate password and passwordConfirm matching, using class-validator package to build up a custom…
Piero Macaluso
  • 1,166
  • 1
  • 10
  • 24
35
votes
5 answers

Validation on optional Parameter using class-validator in nestjs?

I want to apply validation on request payload like, there is field name with string type. But name is not compulsory field but if it exist it must execute @IsNotEmpty() I tried something like this @IsNotEmpty() name?: string // it not considering ?…
Revansiddh
  • 2,932
  • 3
  • 19
  • 33
30
votes
2 answers

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

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 { …
Usama Tahir
  • 1,707
  • 3
  • 15
  • 30
28
votes
9 answers

Boolean parameter in request body is always true in NestJS api

Consider this endpoint in my API: @Post('/convert') @UseInterceptors(FileInterceptor('image')) convert( @UploadedFile() image: any, @Body( new ValidationPipe({ validationError: { target: false, }, …
joe_inz
  • 998
  • 2
  • 13
  • 30
24
votes
4 answers

How to allow null, but forbid undefined?

e.g. for database rows, we may need nullable properties that must not be undefined: class DbRow { @IsNumber() id!: number; @IsNumber() numNullable!: number | null; } So numNullable can be a number or null - but it must never be…
TmTron
  • 17,012
  • 10
  • 94
  • 142
24
votes
5 answers

How do I prevent unwanted object properties from the client in nestjs while updating an existing row in mongodb

Creating a new user will ignore non-specified objects from create-user.dto.ts However when I update the user it will add unwanted fields like this: // update-user.dto.ts import { IsEmail } from 'class-validator'; import { Address } from…
Tea Zulo
  • 65
  • 1
  • 6
  • 14
23
votes
3 answers

How to inject service to validator constraint interface in nestjs using class-validator?

I'm trying to inject my users service into my validator constraint interface but it doesn't seem to work: import { ValidatorConstraintInterface, ValidatorConstraint, ValidationArguments, registerDecorator, ValidationOptions } from…
Dinaiscoding
  • 992
  • 1
  • 7
  • 16
22
votes
6 answers

NestJS: How to transform an array in a @Query object

I'm new to NestJS and I am trying to fill a filter DTO from query Parameters. Here is what I have: Query: localhost:3000/api/checklists?stations=114630,114666,114667,114668 Controller @Get() public async getChecklists(@Query(ValidationPipe) filter:…
yuva.le
  • 289
  • 1
  • 3
  • 9
18
votes
3 answers

Forbid specific enum value for DTO in Nestjs

My "AppState" enum has following possible enum values: export enum AppState { SUCCESS, ERROR, RUNNING } I have a UpdateAppStateDTO with an appState which should accept every enum value except RUNNING. export class UpdateAppStateDTO { …
user11668595
17
votes
4 answers

Throw same error format as `class-validator` in NestJS

Let's have this controller in NestJS project: @Post('resetpassword') @HttpCode(200) async requestPasswordReset( @Body() body: RequestPasswordResetDTO, ): Promise { try { return await…
Baterka
  • 3,075
  • 5
  • 31
  • 60
17
votes
2 answers

class-validator validate union type

I have a mongoose discriminator schema, which mean the data will be different according to one of the attributes. class Feature { name: string option: ColorFeature|SizeFeature } class ColorFeature { kind: 'color' color: string } class…
kingwei
  • 643
  • 6
  • 14
16
votes
5 answers

Validating empty space in class validator

I want to validate Address field, it may contains numbers or strings, but it should not accept continioues empty spaces @IsAlphaNUmereic() Address: string; i want that, Address can be numeric or alphabetic... but it should not accepts continues…
GaneSH
  • 543
  • 2
  • 5
  • 16
15
votes
4 answers

Transform class to class/object (Entity to DTO) in TypeScript and NestS

How to transform database entity User: class User { public firstName: string; public lastName: string; public phone?: string; public email: string; public status: EUserState; public tokens: Token[]; public password: string; } into…
Baterka
  • 3,075
  • 5
  • 31
  • 60
1
2 3
25 26