Allow for undefined properties under @ValidateNested decorator

35 views Asked by At

I'm using NesteJs, i want @ValidateNested to skip properties that are not defined in the class and not to throw an error:

property should not exists

This is my classes:

export default class InitialConfigClass extends SharedInitialConfigClass {
  @Transform((value) => plainToClass(SettingsClass, value))
  @IsNotEmpty()
  @ValidateNested()
  @Type(() => SettingsClass)
  settings!: SettingsClass;
}

export class SettingsClass {
  @IsNotEmpty()
  @ValidateNested()
  @Type(() => FormatSettingsClass)
  first_config!: FormatSettingsClass;

  @IsNotEmpty()
  @ValidateNested()
  @Type(() => FormatSettingsClass)
  second_config!: FormatSettingsClass;

  [key: string]: any;
}

Purpose:

  • I want to allow for sending other properties under settings object, without letting ValdiateNested throwing an error, and without defining them in SettingClass
1

There are 1 answers

0
Aleem On

You can apply turn on the skipMissingProperties option inside of the ValidateNested decorotator


export default class InitialConfigClass extends SharedInitialConfigClass {
  @Transform((value) => plainToClass(SettingsClass, value))
  @IsNotEmpty()
  @ValidateNested()
  @Type(() => SettingsClass)
  settings!: SettingsClass;
}

export class SettingsClass {
  @IsNotEmpty()
  @ValidateNested({ skipMissingProperties: true })
  @Type(() => FormatSettingsClass)
  first_config!: FormatSettingsClass;

  @IsNotEmpty()
  @ValidateNested()
  @Type(() => FormatSettingsClass)
  second_config!: FormatSettingsClass;

  [key: string]: any;
}