for me, I would able to validate nested object with 'class-transformer'

import { Type } from 'class-transformer';

full example:

import {
  MinLength,
  MaxLength,
  IsNotEmpty,
  ValidateNested,
  IsDefined,
  IsNotEmptyObject,
  IsObject,
  IsString,
} from 'class-validator';
import { Type } from 'class-transformer';

class MultiLanguageDTO {
  @IsString()
  @IsNotEmpty()
  @MinLength(4)
  @MaxLength(40)
  en: string;

  @IsString()
  @IsNotEmpty()
  @MinLength(4)
  @MaxLength(40)
  ar: string;
}

export class VideoDTO {
  @IsDefined()
  @IsNotEmptyObject()
  @IsObject()
  @ValidateNested()
  @Type(() => MultiLanguageDTO)
  name!: MultiLanguageDTO;
}
Answer from tarek noaman on Stack Overflow
๐ŸŒ
Medium
medium.com โ€บ @ahureinebenezer โ€บ mastering-data-validation-in-nestjs-a-complete-guide-with-class-validator-and-class-transformer-02a029db6ecf
Mastering Data Validation in NestJS: A Complete Guide with Class-Validator and Class-Transformer | by Ahurein Ebenezer | Medium
March 29, 2025 - If youโ€™re diving deep into NodeJs, you might find my article on Mastering AsyncLocalStorage in Node.js: Effortless Context and State Management with Express and NestJS useful. Itโ€™s a must-read for anyone looking to manage request context and state seamlessly across async operations. Check it out! https://github.com/typestack/class-validator?tab=readme-ov-file#class-validator
๐ŸŒ
NestJS
docs.nestjs.com โ€บ techniques โ€บ validation
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
๐ŸŒ
OneUptime
oneuptime.com โ€บ home โ€บ blog โ€บ how to add validation with class-validator in nestjs
How to Add Validation with class-validator in NestJS
February 2, 2026 - The pipe uses class-transformer to convert plain JSON objects into class instances, then class-validator checks those instances against the decorators you have defined. Invalid data triggers an automatic BadRequestException with detailed error ...
Top answer
1 of 5
106

for me, I would able to validate nested object with 'class-transformer'

import { Type } from 'class-transformer';

full example:

import {
  MinLength,
  MaxLength,
  IsNotEmpty,
  ValidateNested,
  IsDefined,
  IsNotEmptyObject,
  IsObject,
  IsString,
} from 'class-validator';
import { Type } from 'class-transformer';

class MultiLanguageDTO {
  @IsString()
  @IsNotEmpty()
  @MinLength(4)
  @MaxLength(40)
  en: string;

  @IsString()
  @IsNotEmpty()
  @MinLength(4)
  @MaxLength(40)
  ar: string;
}

export class VideoDTO {
  @IsDefined()
  @IsNotEmptyObject()
  @IsObject()
  @ValidateNested()
  @Type(() => MultiLanguageDTO)
  name!: MultiLanguageDTO;
}
2 of 5
45

You are expecting positions: [1] to throw a 400 but instead it is accepted.

According to this Github issue, this seems to be a bug in class-validator. If you pass in a primitive type (boolean, string, number,...) or an array instead of an object, it will accept the input as valid although it shouldn't.


I don't see any standard workaround besides creating a custom validation decorator:

import { registerDecorator, ValidationOptions, ValidationArguments } from 'class-validator';

export function IsNonPrimitiveArray(validationOptions?: ValidationOptions) {
  return (object: any, propertyName: string) => {
    registerDecorator({
      name: 'IsNonPrimitiveArray',
      target: object.constructor,
      propertyName,
      constraints: [],
      options: validationOptions,
      validator: {
        validate(value: any, args: ValidationArguments) {
          return Array.isArray(value) && value.reduce((a, b) => a && typeof b === 'object' && !Array.isArray(b), true);
        },
      },
    });
  };
}

and then use it in your dto class:

@ValidateNested({ each: true })
@IsNonPrimitiveArray()
@Type(() => PositionDto)
positions: PositionDto[];
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ @nestjs โ€บ class-validator โ€บ v โ€บ 0.13.1
nestjs/class-validator
October 29, 2021 - Fork of the class-validator package. Decorator-based property validation for classes.. Latest version: 0.13.4, last published: 4 years ago. Start using @nestjs/class-validator in your project by running `npm i @nestjs/class-validator`. There are 23 other projects in the npm registry using @nestjs/class-validator.
      ยป npm install @nestjs/class-validator
    
Published ย  Feb 14, 2022
Version ย  0.13.1
๐ŸŒ
DEV Community
dev.to โ€บ nithinkjoy โ€บ how-to-use-class-validator-and-return-custom-error-object-in-nestjs-562h
How to use class-validator and generate custom error object in nest.js - DEV Community
June 6, 2023 - In order to use class-validator in nest.js we need to install class-validator and class-transformer. npm i --save class-validator class-transformer ยท To configure, in main.ts file we need to invoke ValidationPipe() constructor function imported form @nestjs/common and pass it as a argument ...
๐ŸŒ
DEV Community
dev.to โ€บ kasir-barati โ€บ nestjs-class-validator-j2
NestJS + class-validator - DEV Community
August 10, 2024 - I created a decorator for the class and called it OneOf. Here we needed to have ValidateIf in order to tell class-validator that it needs to validate a field when that field is present.
๐ŸŒ
DEV Community
dev.to โ€บ avantar โ€บ validating-nested-objects-with-class-validator-in-nestjs-1gn8
Validating nested objects with class-validator in NestJS - DEV Community
September 5, 2021 - import { ValidateNested } from 'class-validator'; export class Post { @ValidateNested() user: User; } But for some reason It doesn't work in NestJS! Here is an easy solution. Install class-transformer package, if you haven't done it yet. Then import ...
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ next.js โ€บ how-to-validate-nested-objects-with-class-validator-in-nest-js
How to Validate Nested Objects with Class-Validator in Nest.js? - GeeksforGeeks
October 16, 2024 - Nested Objects: When DTOs contain ... own validation rules. class-validator: A library that provides a set of decorators and utility functions to validate objects against specified rules....
๐ŸŒ
Medium
medium.com โ€บ deno-the-complete-reference โ€บ 3-commonly-used-input-validation-techniques-in-nestjs-apps-node-js-a94a7f42dc36
3 Commonly used input validation techniques in NestJS apps (Node.js) | Tech Tonic
May 20, 2024 - Class-Validator uses decorators to add validation metadata to the NestJS classes. We can define validation rules for properties, methods, and even entire classes using a set of built-in validation decorators.
๐ŸŒ
GitHub
github.com โ€บ kurollo โ€บ nestjs-class-validator
GitHub - kurollo/nestjs-class-validator: Decorator-based property validation for netsJS multi-languages. ยท GitHub
Allows use of decorator and non-decorator based validation. Internally uses validator.js to perform validation with multi-languages messages supported. Nestjs-Class-validator works on both browser and node.js platforms.
Author ย  kurollo
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ class-validator
class-validator - npm
February 26, 2026 - You can also supply a validation constraint name - this name will be used as "error type" in ValidationError. If you will not supply a constraint name - it will be auto-generated. Our class must implement ValidatorConstraintInterface interface and its validate method, which defines validation logic.
      ยป npm install class-validator
    
Published ย  Feb 26, 2026
Version ย  0.15.1
๐ŸŒ
Reddit
reddit.com โ€บ r/node โ€บ nestjs + class-validator: validating decimal values
r/node on Reddit: Nestjs + Class-validator: Validating decimal values
March 8, 2024 -

I just ran into a pretty simple issue, this definition normally should have worked perfectly and pass the validation as price field in JSON is already sent in decimal format as you can figure below, but class-validator keeps raising such error even though I meet the criterias.Whats the best practices to handling this kind of validations on Nestjs, wondering how everyone else would handle that, thanks

create-product.dto.ts

 @IsDecimal(
    { force_decimal: true, decimal_digits: '2' },
    { message: 'Price must be a valid number !' },
  )
  @IsNotEmpty({ message: 'Price is required' })
  @Min(0, { message: 'Price must be greater than or equal to 0' })
  price: number;

request sent

{
 // other fields...

    "price" : 1574.23,

}

response

{
    "message": [
        "Price must be a valid number !"
    ],
    "error": "Bad Request",
    "statusCode": 400
}

tried to switch to @IsNumberString type as well but got no luck

๐ŸŒ
DEV Community
dev.to โ€บ sarathsantoshdamaraju โ€บ nestjs-and-class-validator-cheat-sheet-13ao
NestJS and 'class validator' cheat sheet
June 13, 2022 - // example.dto.ts import { IsNumber, IsNotEmpty, MinLength, MaxLength, ValidatorConstraint, ValidatorConstraintInterface } from 'class-validator' @ValidatorConstraint({ name: 'isEvenNumber' }) export class IsEvenNumber implements ValidatorConstraintInterface { validate(numbers: number): boolean { if (numbers { return numbers.every(number => number % 2 === 0) } return false } } const validLanguages = ["en", "es", "fr"] const validGeneres = ["sci-fi", "thriller", "animation", "horror", "vintage"] // ๐Ÿ’ก Types here export class Movie { @IsString() @IsNotEmpty() @ISRequired() name: string @IsNotE
๐ŸŒ
Medium
medium.com โ€บ @jradzik4 โ€บ data-validation-in-nestjs-with-class-validator-and-class-transformer-ec9e5057e54f
Data Validation in NestJS with class-validator and class-transformer | by Jakub Radzik | Medium
February 5, 2026 - Before jumping into code, letโ€™s quickly clarify what these libraries do: class-validator: A library that allows you to use decorators to define validation rules on your TypeScript classes.
๐ŸŒ
Medium
shivamethical.medium.com โ€บ nest-js-class-validators-complex-examples-to-make-life-easier-in-project-32ee460c328e
Nest JS class-validators complex examples to make life easier in project | by Shivam Gupta | Medium
June 5, 2023 - // example.dto.tsty import { ArrayNotEmpty, IsArray, IsIn } from 'class-validator' const weekdays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']; export class SpecificString { @IsArray() @ArrayNotEmpty() @IsIn(weekdays) day: string[] }
๐ŸŒ
Medium
medium.com โ€บ @gabrielaugusto753 โ€บ validating-query-parameters-in-a-nestjs-project-with-class-validator-f5e8d51e1a21
Validating Query Parameters in a NestJS Project with class-validator | by Gabriel Augusto Gomes da Silva | Medium
July 24, 2024 - However, this approach can make the code messy and goes against the intended use of each HTTP method. Itโ€™s confusing (and even amusing) to see a route named get-something being a POST method. In NestJS projects, we typically use two libraries for validating data received from requests: class-validator and class-transformer.
๐ŸŒ
Rodion-blog
rodion-blog.tech โ€บ main โ€บ backend โ€บ common validation examples in nestjs
Common validation examples in NestJS - Rodion Abdurakhimov
September 30, 2023 - Validation logic in NestJS applications usually stored in files called DTOs (Data Transfer Object). class-validator package provides a lot of decorators that we can use to validate our DTOs. For simplicity, we will create simple APIs in app.controller.ts.
๐ŸŒ
This Dot Labs
thisdot.co โ€บ blog โ€บ combining-validators-and-transformers-in-nestjs
Combining Validators and Transformers in NestJS - This Dot Labs
February 13, 2023 - First, let's install the dependencies needed for using data transformation and validation in NestJS: ... As their names would suggest, the class-validator package brings support for validating data, while the class-transformer package brings ...