Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Keeping Your Data Sane with Bean Validation 2.0 (JavaZone 2017)

Keeping Your Data Sane with Bean Validation 2.0 (JavaZone 2017)

Find out what's new in Bean Validation 2.0 (JSR 380) in this talk given at JavaZone 2017. A video recording of the talk can be found at https://vimeo.com/233797511.

Gunnar Morling

September 13, 2017
Tweet

More Decks by Gunnar Morling

Other Decks in Programming

Transcript

  1. Gunnar Morling Open source software engineer at Red Hat Debezium

    Hibernate Spec Lead for Bean Validation 2.0 Other projects: ModiTect, MapStruct [email protected] @gunnarmorling http://in.relation.to/gunnar-morling/ 3
  2. What is Bean Validation? "Constraint once, validate everywhere" Constraints for

    JavaBeans Validation via API or automatically ​ JPA JSF, Spring MVC, GWT JAX-RS Extensible (custom constraints) BV 1.1: ​ method validation 4
  3. Bean Validation 2.0 - JSR 380 Benefit from new Java

    8 language features Support for API extensions 6
  4. Use Case: Different Passwords for Different Roles @Size.List({ @Size(min =

    8, group = Default.class), @Size(min = 12, group = Admin.class) }) private char[] password = ...; 7
  5. Use Case: Different Passwords for Different Roles @Size.List({ @Size(min =

    8, group = Default.class), @Size(min = 12, group = Admin.class) }) private char[] password = ...; @Size(min = 8, group = Default.class) @Size(min = 12, group = Admin.class) private char[] password = ...; 8
  6. Use Case: Ensure Non-Empty Collection Elements private List<String> names; private

    List<@NotEmpty String> names; @OnElements(constraint=@NotEmpty) private List<String> names; 12
  7. Use Case: Ensure Non-Empty Collection Elements private List<String> names; private

    List<@NotEmpty @Pattern(regexp="[a-zA-Z]*") String> names; @OnElements(constraint=@NotEmpty) private List<String> names; 13
  8. private List<@NotEmpty @Pattern(regexp="[a-zA-Z]*") String> names; Use Case: Ensure Non-Empty Collection

    Elements private List<String> names; @NotEmpty private List<@NotEmpty String> names; @OnElements(constraint=@NotEmpty) private List<String> names; 14
  9. @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) @Retention(RUNTIME) @Repeatable(List.class)

    @Documented @Constraint(validatedBy = { }) public @interface Size { ... } Type Annotations (JSR 308) New element type TYPE_USE 15
  10. Cascaded Validation @Valid private List<Address> addresses; private List<@Valid Address> addresses;

    private Map<@Valid Comment, Integer> scorePerComment; private Map<@Valid AddressType, List<@Valid Address>> addressesByType; 19
  11. Custom Containers Specific collection types (e.g. Google Guava) Other JVM

    languages (Ceylon, Scala etc.) Enabled via Extractor SPI private Table<Year, String, Integer> revenuePerYearAndCategory; 21
  12. private Table<Year, String, Integer> revenuePerYearAndCategory; private Table<Year, String, @Positive Integer>

    revenuePerYearAndCategory; Custom Containers Specific collection types (e.g. Google Guava) Other JVM languages (Ceylon, Scala etc.) Enabled via Extractor SPI 22
  13. Use Case: Delivery Date in the Future @Past/@Future supported for

    JSR 310 types: java.time.LocalDateTime, ZonedDateTime etc. @Future private LocalDate deliveryDate = LocalDate.of( 2017, Month.MAY, 12 ); 24
  14. Use Case: Delivery Date in the Future @Past/@Future supported for

    JSR 310 types: java.time.LocalDateTime, ZonedDateTime etc. @FutureOrPresent private LocalDate deliveryDate = LocalDate.of( 2017, Month.MAY, 12 ); @Future private LocalDate deliveryDate = LocalDate.of( 2017, Month.MAY, 12 ); 25
  15. Use Case: Testing ValidatorFactory vf = Validation.byDefaultProvider() .configure() .clockProvider( new

    FixedClockProvider( ZonedDateTime.of( 2016, 6, 15, 0, 0, 0, 0, ZoneId.of( "Europe/Paris" ) ) ) ) .buildValidatorFactory(); 26
  16. Other Java 8 Goodies Real parameter names in error messages

    ConstraintValidator without initialize() public class NotNullValidator implements ConstraintValidator<NotNull, Object> { public void initialize(NotNull constraintAnnotation) {} public boolean isValid(Object object, ConstraintValidatorContext ctx) { return object != null; } } public class NotNullValidator implements ConstraintValidator<NotNull, Object> { public boolean isValid(Object object, ConstraintValidatorContext ctx) { return object != null; } } 27
  17. Final Release Part of Java EE 8 Supported by Spring

    5 Contained in GlassFish 5; easy-to-use patch for WildFly Everything open source: spec, API, TCK and reference implementation 30
  18. Outlook Bean Validation 2.1 Constraint-API ConstraintMapping mapping = ...; mapping.type(

    Marathon.class ) .property( "numberOfHelpers", FIELD ) .constraint( new MinDef().value( 1 ) ) .property( "runners", METHOD ) .valid(); mapping.constraintDefinition( FileExists.class ) .validateType( File.class ) .with( f -> f.exists() ); Constraint-API: Lambda 31
  19. Resources Spec Reference implementation API, specifikation, TCK, website Everything is

    open source @gunnarmorling http://beanvalidation.org/2.0/spec/ github.com/hibernate/hibernate-validator/ github.com/beanvalidation/ 32
  20. 33