允许覆盖/设置约束验证以用于 Hibernate 验证的子对象。

huangapple go评论95阅读模式
英文:

Allow to override/set constraints validation for a child object for Hibernate validation

问题

我有以下的类:

  1. class ContactInformation {
  2. String phone;
  3. String email;
  4. }

它被以下的类使用:

  1. class Person {
  2. @Valid
  3. ContactInformation contactInformation;
  4. }
  1. class Station {
  2. @Valid
  3. ContactInformation contactInformation;
  4. }

问题是,任何 Person 类的实例必须有一个电子邮件地址,但对于 Station 类来说,它是可选信息。我是否有办法在拥有者级别上定义这一点,以避免重复定义 ContactInformation 类?

英文:

I have the following class :

  1. class ContactInformation {
  2. String phone;
  3. String email;
  4. }

which is used in the following classes :

  1. class Person {
  2. @Valid
  3. ContactInformation contactInformation;
  4. }
  1. class Station {
  2. @Valid
  3. ContactInformation contactInformation;
  4. }

The thing is that any instance of Person must have an email, but it is an optional information for Station. Do I have a way to define this at owner level to avoid duplicate the class ContactInformation ?

答案1

得分: 1

代替field级别的验证器,您可以添加Type级别的验证器。
步骤:

  • 定义Type级别的注解
  • 编写新注解的验证器
  • 使用新注解引入您的类型

定义:

  1. @Constraint(validatedBy = {PersonClassOptionalEmailValidator.class})
  2. @Target({ElementType.TYPE})
  3. @Retention(RetentionPolicy.RUNTIME)
  4. public @interface PersonalEmailValid {
  5. String message() default "Invalid Email address";
  6. Class<?>[] groups() default {};
  7. Class<? extends Payload>[] payload() default {};
  8. }

编写自定义验证器:

  1. public static class PersonClassOptionalEmailValidator implements ConstraintValidator<PersonalEmailValid, Person> {
  2. // Test Email validator, you should check proper regex for production
  3. public static final String EMAIL_REGEX = "^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$";
  4. private final Pattern pattern;
  5. public PersonClassOptionalEmailValidator() {
  6. pattern = Pattern.compile(EMAIL_REGEX);
  7. }
  8. @Override
  9. public boolean isValid(Person person, ConstraintValidatorContext constraintValidatorContext) {
  10. if (person.contactInformation != null) {
  11. return pattern.matcher(person.contactInformation.email).matches();
  12. }
  13. return false;
  14. }
  15. }

引入新的注解到类中:

  1. @Getter
  2. @Setter
  3. @NoArgsConstructor
  4. @PersonalEmailValid
  5. static class Person {
  6. @Valid
  7. ContactInformation contactInformation;
  8. }

参考链接

Gist链接

英文:

Instead of the field level validator you can add the Type level validator.
Steps:

  • Define Type level annotation
  • Write Validator for new annotation
  • Introduce your type with a new annotation

Define:

  1. @Constraint(validatedBy = {PersonClassOptionalEmailValidator.class})
  2. @Target({ElementType.TYPE})
  3. @Retention(RetentionPolicy.RUNTIME)
  4. public @interface PersonalEmailValid {
  5. String message() default &quot;Invalid Email address&quot;;
  6. Class&lt;?&gt;[] groups() default {};
  7. Class&lt;? extends Payload&gt;[] payload() default {};
  8. }

Writing Custom Validator:

  1. public static class PersonClassOptionalEmailValidator implements ConstraintValidator&lt;PersonalEmailValid, Person&gt; {
  2. // Test Email validator, you should check prope regex for production
  3. public static final String EMAIL_REGEX = &quot;^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$&quot;;
  4. private final Pattern pattern;
  5. public PersonClassOptionalEmailValidator() {
  6. pattern = Pattern.compile(EMAIL_REGEX);
  7. }
  8. @Override
  9. public boolean isValid(Person person, ConstraintValidatorContext constraintValidatorContext) {
  10. if (person.contactInformation != null) {
  11. return pattern.matcher(person.contactInformation.email).matches();
  12. }
  13. return false;
  14. }
  15. }

Introduce new annotation to class

  1. @Getter
  2. @Setter
  3. @NoArgsConstructor
  4. @PersonalEmailValid
  5. static class Person {
  6. @Valid
  7. ContactInformation contactInformation;
  8. }

Reference

Gist

huangapple
  • 本文由 发表于 2020年9月4日 21:43:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63742393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定