最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to make the annotation @NotNull works before custom validator? - Stack Overflow

programmeradmin5浏览0评论

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
  • 2 Who says that your constraint validator works before @NotNull? Is that because your validator receives a null 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 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. – Rob Spoor Commented Feb 5 at 10:09
  • @RobSpoor , you are absolutely right! Thanks! – Eugene_Z Commented Feb 5 at 10:43
  • You're welcome. – Rob Spoor Commented Feb 5 at 13:48
Add a comment  | 

1 Answer 1

Reset to default 0

As 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.

发布评论

评论列表(0)

  1. 暂无评论