TheSprtCZ
TheSprtCZ

Reputation: 3

Hibernate Validator validate set

I have custom validation annotation called @Role and I have custom validator which validates User and it works. Problem is when I want to use this validator on Set<User> to validate every element on this set like this

public class Project {
  // Validates
  @Role
  private User creator;

  // Throws error
  @Role
  private Set<User> users;
}

This sadly throws this error:

 javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'cz.studenthub.validators.annotations.Role' validating type 'java.util.Set<cz.studenthub.core.User>'

Is there any way to validate collections without having to write new validator for Set<User>?

Note: I don't want to use @Valid for User because that instance doesn't need to be completely valid, it just needs to meet that one condition validated by my RoleValidator.

Upvotes: 0

Views: 382

Answers (1)

Guillaume Smet
Guillaume Smet

Reputation: 10529

I would suggest you to use Hibernate Validator 6.0.0.Beta2 if you can.

We now have support for container element constraints so you can simply do:

public class Project {
    // Validates
    @Role
    private User creator;

    private Set<@Role User> users;
}

We are planning a Candidate Release 1 in a week so it's already pretty stable.

Upvotes: 1

Related Questions