toonvanstrijp / nestjs-i18n

The i18n module for nestjs.
https://nestjs-i18n.com
Other
656 stars 113 forks source link

fix: prevent nested validations from throwing #674

Closed velvet-lynx closed 2 weeks ago

velvet-lynx commented 2 weeks ago

Description

class PostDto {
  @IsString({
    message: i18nValidationMessage<I18nTranslations>('validation.nested'),
  })
  title: string;

  @IsString({
    message: i18nValidationMessage<I18nTranslations>('validation.nested'),
  })
  body: string;
}

export class PostsDto {
  @ValidateNested({ each: true })
  @Type(() => PostDto)
  posts: PostDto[];
}

When using the decorator @ValidateNested({each: true]) from class-validator like in the above example, the I18nService.validate(...) method will throw the following error TypeError: Cannot convert undefined or null to object.

This is because in the absence of validation errors in the parent, the constraints property of the ValidationError will be undefined. Thus, the call to Object.keys(error.constraints) in the formatI18nErrors util function will fail. Since Object.keys only works with objects.

Linked Issues

Fixes #633

Additional context

coveralls commented 2 weeks ago

Coverage Status

coverage: 90.837% (-0.07%) from 90.909% when pulling 7ad950d9974d81046f66984ed03cc9ccdf2d007e on velvet-lynx:fix/validate-nested-each into c7946131c9ea84468f676f7b6c5239387e406d35 on toonvanstrijp:main.