Skip to content

class-validator resolver not working properly when using Expose annotations with groups  #445

@ramirezsandin

Description

@ramirezsandin

Describe the bug
When a class is annotated with @Expose({groups: "group-name"}) the validation doesn't work.
With the plain @Expose() annotation there's no problem.

To Reproduce
Create a simple class to validate:

class TestDto {
  @Expose( {groups: ["find", "create", "update"]} )
  @Type(() => Number)
  @IsDefined({
    message: `All fields must be defined.`,
    groups: ["publish"],
  })
  @IsNumber({}, { message: `Must be a number`, always: true })
  @Min(0, { message: `Cannot be lower than 0`, always: true })
  @Max(255, { message: `Cannot be greater than 255`, always: true })
  random: number;
}

Then just create a simple resolver:

const saveResolver = classValidatorResolver(TestDto , {
  groups: ["update"],
  skipMissingProperties: true,
});

All the validations are ignored.

My assumption is that the resolver, transforms the value into an instance of the TestDto class without any particular option to expose the given annotated properties. So the resulting instance doesn't have the property. So as skipMissingProperties: true is set, the validations won't run on it, except for the @IsDefined annotation if it was annotated with the same group as the resolver: @IsDefined({groups: ["update"]}).

Expected behavior
The validation should be run on the object, and reject it if it doesn't meet the criteria.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions