My SpringBoot application receives the DTO:
@GroupSequence(value = {ValidationGroup.LightErrors.class, ValidationGroup.HeavyError.class, MyDto.class})
@MyDtoConstraint(groups = ValidationGroup.HeavyError.class)
public class MyDto {
@Valid
private MyCommissionDto commission;
}
Annotation MyDtoConstraint looks like this:
@Documented
@Constraint(validatedBy = MyDtoConstraintValidator.class) <-- custom validator!
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyDtoConstraint{
String message() default "My data is incorrect.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
MyCommissionDto looks like :
@GroupSequence(value = {ValidationGroup.LightErrors.class, ValidationGroup.HeavyError.class, MyCommissionDto.class})
public class MyCommissionDto {
@NotNull(message = ErrorMessage.NOT_NULL, groups = ValidationGroup.LightErrors.class)
CommissionType type;
}
Why MyDtoConstraintValidator on MyDto works before NotNull constraint on field MyCommissionDto.type?
I've read the documentation .1/spec/#constraintdeclarationvalidationprocess-groupsequence but it's not helped me.
My SpringBoot application receives the DTO:
@GroupSequence(value = {ValidationGroup.LightErrors.class, ValidationGroup.HeavyError.class, MyDto.class})
@MyDtoConstraint(groups = ValidationGroup.HeavyError.class)
public class MyDto {
@Valid
private MyCommissionDto commission;
}
Annotation MyDtoConstraint looks like this:
@Documented
@Constraint(validatedBy = MyDtoConstraintValidator.class) <-- custom validator!
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyDtoConstraint{
String message() default "My data is incorrect.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
MyCommissionDto looks like :
@GroupSequence(value = {ValidationGroup.LightErrors.class, ValidationGroup.HeavyError.class, MyCommissionDto.class})
public class MyCommissionDto {
@NotNull(message = ErrorMessage.NOT_NULL, groups = ValidationGroup.LightErrors.class)
CommissionType type;
}
Why MyDtoConstraintValidator on MyDto works before NotNull constraint on field MyCommissionDto.type?
I've read the documentation https://beanvalidation.org/1.1/spec/#constraintdeclarationvalidationprocess-groupsequence but it's not helped me.
Share Improve this question asked Feb 5 at 9:58 Eugene_ZEugene_Z 2733 silver badges16 bronze badges 3 |1 Answer
Reset to default 0As Rob Spoor answered in comments, everything is correct, NotNull works before validator.
first each validation takes place without throwing any errors, then all validation errors are collected and wrapped in a single ConstraintViolationException. That's why it has a set of violations. That's why when implementing a constraint validator, the first step is to return true if the thing to validate is null. If it shouldn't be null then let @NotNull handle that.
@NotNull
? Is that because your validator receives anull
value? That's per design, and will happen even if@NotNull
is processed first. The reason is simple: first each validation takes place without throwing any errors, then all validation errors are collected and wrapped in a singleConstraintViolationException
. That's why it has a set of violations. That's why when implementing a constraint validator, the first step is to returntrue
if the thing to validate isnull
. If it shouldn't benull
then let@NotNull
handle that. – Rob Spoor Commented Feb 5 at 10:09