typestack/routing-controllers

fix: @ValidateNested not working

AdmanDev opened this issue · 4 comments

Description

Hello, I have a problem with @ValidateNested decorator. It is not working for me; I have tried with a single object and an array, but neither is working.

Minimal code-snippet showcasing the problem

export class CreateEventRequest {

	@ValidateNested({ each: true })
	public polls: CreatePollRequest[] = []

	@ValidateNested()
	public singlePoll: CreatePollRequest = new CreatePollRequest()
}

export class CreatePollRequest {
       ....
	@IsString({ message: "La question du sondage est requise !" })
	@IsNotEmpty({ message: "La question du sondage est requise !" })
	public question = ""

	@IsArray({ message: "Les options du sondage sont requises !" })
	@IsString({
		each: true,
		message: "Les options du sondage sont requises !"
	})
	@IsNotEmpty({
		each: true,
		message: "Une option est vide !"
	})
	@ArrayMinSize(2, { message: "Le sondage doit avoir au moins 2 options !" })
	public options: string[] = []
}

Expected behavior

I sent this body param with a bad value for "question" field

{
...
    "polls": [
        {
            "question": 1 // should be a string,
            "options": ["Le légendaire", "La tête dans les nuage"]
        }
    ],
    "singlePoll": {
            "question": 1 // should be a string,
            "options": ["Le légendaire", "La tête dans les nuage"]
    }
}

Actual behavior

The "polls" and "singlePoll" are not validated and the request will pass.
But an error should be thrown.

My package.json

{
   ...,
   "reflect-metadata": "^0.1.13",
    "routing-controllers": "^0.10.2",
    "typedi": "^0.10.0"
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
}

The issue #483 is similar to my problem but there is no solution

Have you any solution ?

(Sorry for my english, I am french ^^)

@AdmanDev this seems like an issue in class-validator rather than routing-controllers. Could you post this issue in that repo?