smolarek999
smolarek999

Reputation: 519

Validation is skipped when constraints are only in parent class

I am using MOXy as JAXB provider along with Bean Validation.

I'm trying to validate a class where only parent fields are annotated with constraints.

I can see that MOXy is skipping validation for this class, probably it is not looking into the parent class.

Can you advise on how to overcome this?

Upvotes: 0

Views: 400

Answers (2)

smolarek999
smolarek999

Reputation: 519

I found a quick workaround. It is about creating dummy constraints which will always pass. Then you can put it on any class. and MOXy will turn on validation.

Dummy constraints:

@Target( {ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = PutItWhenYouDoNotHaveConstraintsButParentHas.Validator.class)
@Documented
public @interface  PutItWhenYouDoNotHaveConstraintsButParentHas {

    String message() default "{com.mycompany.constraints.checkcase}";
    Class<? extends Payload>[] payload() default {};
    Class<?>[] groups() default {};
    public static class Validator implements ConstraintValidator<PutItWhenYouDoNotHaveConstraintsButParentHas, Object>{

        @Override
        public void initialize(PutItWhenYouDoNotHaveConstraintsButParentHas constraintAnnotation) {

        }

        @Override
        public boolean isValid(Object value, ConstraintValidatorContext context) {
            return true;
        }
    }
}

Example usage:

public class IdoNotHaveConstraints extends ButMyParentHas{

    @PutItWhenYouDoNotHaveConstraintsButParentHas
    public IdoNotHaveConstraints (){

    }
}

Upvotes: 1

Dmitry
Dmitry

Reputation: 417

It's a known issue in 2.6.0. It is fixed in master (2.7.x) and will be backported to 2.6.2, backporting to 2.6.1 is not planned. Unfortunately these builds are not available for public yet. You should wait for 2-3 months.

As a workaround I can suggest moving BV annotations from superclass to all children. And yes, I agree that it's ugly. :(

Upvotes: 0

Related Questions