question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValidateNested not working

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:21 (9 by maintainers)

github_iconTop GitHub Comments

8reactions
ericzoncommented, Nov 10, 2017

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.

8reactions
ruimingcommented, Jun 28, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validate nested objects using class validator and nestjs
I've just put your code in an empty sample project and it seems to work for me. What specific value is "not working"?...
Read more >
Validating nested objects with class-validator in NestJS
import { ValidateNested } from 'class-validator'; export class Post ... i could solve this problem with the help of custom recursive ...
Read more >
typestack/class-validator - Gitter
Hello - I'm using class-validator within NestJS and liking it! - but I'm struggling with the @IsMobilePhone() decorator - it isn't respecting the...
Read more >
How to use the class-validator.IsIn function in class-validator
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
Read more >
class-validator - npm
Start using class-validator in your project by running `npm i ... otherwise @ValidateNested won't know what class is target of validation.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found