class-validator: ValidateNested not working

Hello,

i have something like this:

class Day
{
    @IsMilitaryTime()
    from: string;

    @IsMilitaryTime()
    to: string;
}

export class DailyCalendar
{
    @IsDefined() // do i need it?
    @ValidateNested()
    monday: Day;
    ...
}

When calling the route with this body: { "monday": { "from": "foo" } } or { "monday": { } } it doesn’t throw an error. Also when not using @IsDefined() i can call the route without monday property and it doesn’t throw an error.

The class will be created, but not validated.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 21 (9 by maintainers)

Most upvoted comments

Well, I’ve just solved my problem, I share the solution:

DummyDto (main class)

import { Type } from 'class-transformer';
import { Contains, ValidateNested } from 'class-validator';

import { SubDto } from './sub.dto';

export class DummyDto {
    @Contains("hello")
    public stringAttr: string;
    @ValidateNested()
    @Type(() => SubDto)
    public subAttr: SubDto;
}

SubDto file:

import { Contains } from 'class-validator';

export class SubDto {
    @Contains("eric")
    public sub1: string;
    public sub2: string;
}

This dto works:

{
	"stringAttr": "hello",
	"subAttr": {
		"sub1": "eric",
		"sub2": "subhello2"
	}
}

This dto fails:

{
	"stringAttr": "hello",
	"subAttr": {
		"sub1": "aaa",
		"sub2": "subhello2"
	}
}

Note for NestJs users:

The ValidatorPipe is very similar to the existing on docs.

I hope this helps somebody.

I have the same problem. This work for me:

export class ICreateOrUpdateActivity implements IActivity {
  @IsString() title: string
  @ValidateNested() @Type(() => ICreateOrUpdateActivityPrize) prizes: ICreateOrUpdateActivityPrize[]
  @ValidateNested() @Type(() => ICreateOrUpdateActivityReply) replies: ICreateOrUpdateActivityReply[]
}

@Type is from module class-transformer.

I just ran into this unexpected bug. It works fine if I have an actual instance of the type, but non-instances are ignored. E.g., in one of my unit tests, I’m casting a string to any, when it should be a class instance, and assigning the any to an array property on the parent. The any value passes as valid. I would expect this to throw an error because a string is not an instance of my expected type.

Example:

export class ChildPropertyType {
  @IsDefined()
  @IsNotEmpty()
  @IsString()
  public prop: string;
}

export class Parent {
  @IsOptional()
  @IsArray()
  @ValidateNested()
  @Type(type => ChildPropertyType)
  public children: ChildPropertyType[];
}

// in the unit test
const p = new Parent();
p.children = [<any>'string'];
// call validate... no errors thrown.

The reason I even tried this is because we want to have more control over error responses, so we turned off validation in routing-controllers and call the validation method manually. This allows for clients to pass in any info they want and it by-passes the validation routine.

Hi @Masterrg I’m also using Nestjs, could you share a simple example of how you do nested validation? I try with plainToClass on ValidatorPipe but only validates parent class.

I have another issue (using TypeScript 2.5.3, NodeJS 6.10.3 and class-validator 0.7.3):

class Nested {
  foo: string;
}

class Klass {
  @ValidateNested() 
  @IsDefined()
  nested: Nested;
}

const obj = new Klass();
obj.nested = null as any; // I need `as any` because I'm using strictNullChecks
const res = validateSync(obj);

Last line throws an error Only objects and arrays are supported to nested validation (https://github.com/pleerock/class-validator/blob/master/src/validation/ValidationExecutor.ts#L208).

If I remove the line obj.nested = null as any, then obj.nested is undefined and everything works as expected - in this case I receive a validation error that obj.nested must be defined.

If I remove IsDefined then the error is still thrown.

Hey @pleerock

any updates on this issue? We are using NestJS and are implementing a validation pipe (combination of class-transformer and class-validator). Unfortunately @ ValidateNested() still doesn’t work as it doesn’t return any errors in obviously wrong objects.

Best Regards