Skip to content

question: array of custom objects validation not working #389

@Jrmyy

Description

@Jrmyy

Hi,

Validation for array of custom objects does not seem to work.

Versions:

class-transformer0.2.3
class-validator: 0.9.1

My objects:

questions.ts

export class QuestionsDto {
  @IsNotEmpty()
  @IsString()
  applicationId: string;

  @IsArray()
  @ValidateNested({each: true})
  @Type(() => QuestionDto)
  @ArrayMinSize(1)
  questions: QuestionDto[];
}

question.ts

export class QuestionDto {

  private static ALLOWED_QUESTION_TYPES = ['mcq', 'text', 'date'];

  @IsNotEmpty()
  @IsString()
  @IsIn(QuestionDto.ALLOWED_QUESTION_TYPES)
  type: string;

  @IsNotEmpty()
  @IsString()
  designation: string;

  @IsNotEmpty()
  @IsString()
  reason: string;

  @IsOptional()
  @IsArray()
  extraContent: string[];
}

Use case:

const q = {
  applicationId: 'aaa',
  questions: ['aaa']
}

const errors = validate(QuestionsDto, q)
console.log(errors.length)  // 0

Thanks by advance :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questionQuestions about the usage of the library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions