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

Custom Validator called multiple time #2171

Open
Dsantosjaime opened this issue Jul 21, 2023 · 0 comments
Open

Custom Validator called multiple time #2171

Dsantosjaime opened this issue Jul 21, 2023 · 0 comments
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@Dsantosjaime
Copy link

Dsantosjaime commented Jul 21, 2023

Description

My Custom Validator:

@Injectable()
@ValidatorConstraint({ name: 'isValidSiteIds', async: true })
export class IsValidSiteIdsValidator implements ValidatorConstraintInterface {
    constructor(private readonly SiteService: SiteService) { }

    async validate(siteIds: string[], validationArguments: ValidationArguments) {
        const groupId = validationArguments.object['groupId'];
        log(siteIds, groupId, validationArguments);

        const sitesInGroup = await this.SiteService.findAll({ groupId, id: { in: siteIds } });
        log(sitesInGroup.length === siteIds.length);
        return sitesInGroup.length === siteIds.length;
    }
}

registerDecorator({
    name: 'isValidSiteIds',
    target: Object,
    propertyName: 'siteIds',
    options: { message: 'Un ou plusieurs sites spécifiés ne correspondent pas au groupe.' },
    validator: IsValidSiteIdsValidator,
});

My DTO:

export class ClientDto {
    @ApiProperty()
    @IsEnum(ClientRole)
    role: ClientRole;

    @ApiProperty({ type: [String] })
    @IsArray()
    @IsString({ each: true })
    @ValidateIf(o => ... )
    @ArrayNotEmpty({ message: ... })
    @Validate(IsValidSiteIdsValidator)
    siteIds: string[]

    @ApiProperty()
    @IsString()
    groupId: string
}

export class CreateClientDto extends CreateUserDto {
    @ApiProperty()
    @Type(() => ClientDto)
    @ValidateNested()
    @IsNotEmpty()
    client: ClientDto;
}

Expected behavior

I want my validator to check my field "siteIds".

Actual behavior

My validator is called two times.
I logged "ValidationArguments" and the first time it gave me :

{
  targetName: 'ClientDto',
  property: 'siteIds',
  object: ClientDto {
    role: 'MANAGER',
    siteIds: [ '64ba804f35dc69d2d58f6585' ],
    groupId: '64b6a9910c437e88fb1dfa77'
  },
  value: [ '64ba804f35dc69d2d58f6585' ],
  constraints: undefined
}

and the second time:

{
  targetName: 'CreateClientDto',
  property: 'siteIds',
  object: CreateClientDto {
    email: 'generic@email.com',
    firstname: 'generic',
    lastname: 'generic',
    phone: '+33000000000',
    client: ClientDto {
      role: 'MANAGER',
      siteIds: [Array],
      groupId: '64b6a9910c437e88fb1dfa77'
    }
  },
  value: undefined,
  constraints: undefined
}
@Dsantosjaime Dsantosjaime added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Jul 21, 2023
@Dsantosjaime Dsantosjaime changed the title fix: <your-title-goes-here> Custom Validator called multiple time Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

No branches or pull requests

1 participant