Search code examples
resteasybean-validationhibernate-validator

Validate object to RESTeasy method using specific validation group


I am attempting to validate the input to a REST method using one or more specific validation groups, but so far I have been unable to figure out how to properly do it. I am using RESTeasy 3.X with Hibernate Validator 5.X.

I know that prior to the Bean Validation 1.1 spec, a RESTeasy method could be annotated with something like:

 @ValidateRequest(groups=MyGroup.class)

However, this functionality no longer exists in Hibernate Validator 5.X.

Suppose I have an entity like:

 @Entity
 @Table(name = "example_table")
 @XmlRootElement
 public class ExampleEntity implements Mappable, Serializable
 {
     // ...

     @Column(name = "title")
     @NotEmpty(groups = Default.class, ExampleEntityGroup.class)
     @Size(max = 255)
     private String title;

     @Column(name = "description")
     @NotEmpty(groups = Default.class)
     @Size(max = 255)
     private String description;

     // ...
 }

Now I'd like to define a REST PUT method to update this entity, and I'd like the "description" field to allow an empty value. For this to happen I'd like to validate the entity using the ExampleEntityGroup group that I've defined (whereas the Default group would be used to validate POST requests that create new objects).

Right now my update method interface looks something like:

 @Path("{id}")
 @PUT
 @Consumes(...)
 @Produces(...)
 ExampleEntity update(@PathParam("id"), ExampleEntity exampleEntity);

However, with this as written, it will always validate exampleEntity using the Default validation group. What would I do to force the update() method to use the other validation group?

The only documentation I could find that seems close to this is an example from the Bean Validation spec that uses an interceptor. Is there a better way?


Solution

  • RestEasy 3 with Bean Validation 1.1 will automatically validate constrained resource methods, i.e. it suffices to place a constraint annotation to a resource method parameter or return value and it will be validated. There is no way to validate a specific group, it will always be the default group. You could try and open a feature request for the RestEasy project for this.