Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent nested validations from throwing #674

Merged
merged 1 commit into from
Nov 7, 2024

Conversation

velvet-lynx
Copy link
Contributor

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

@rubiin rubiin merged commit ac6e4f9 into toonvanstrijp:main Nov 7, 2024
2 checks passed
@coveralls
Copy link

Coverage Status

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError at formatI18nErrors when validating nested array of objects
3 participants