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

Keeping Your Data Sane with Bean Validation 2.0

Keeping Your Data Sane with Bean Validation 2.0

Rich UIs, schemaless datastores, microservices communicating with each other - the need for powerful and easy-to-use data validation services has never been bigger.

The Bean Validation standard is here to help, providing Java developers with a rich validation API based on annotations. Bean Validation 2.0 (JSR 380) takes validation to the next level by leveraging Java 8 features such as lambdas, additional annotation locations and repeatable annotations. This opens up exciting opportunities for Bean Validation - e.g. List<@Email String>. There will also be support for Optional, the java.time API and more.

Join us and see what's planned for Bean Validation 2.0 and what's already done. Like its predecessors, JSR 380 is developed fully in the open: come exchange, propose and participate!

Presentation given at RivieraDev 2017

Gunnar Morling

May 11, 2017
Tweet

More Decks by Gunnar Morling

Other Decks in Programming

Transcript

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

    Debezium 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 language

    features Support for API extensions 6
  4. Repeatable Annotations @Size.List({ @Size(min = 8, group = Default.class), @Size(min

    = 12, group = Admin.class) }) private char[] password = ...; 7
  5. Repeatable Annotations @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. Collection Validation private List<String> names; @Size(max=10) private List<@Size(max=50) String> names;

    @Size(max=10) private List<@Pattern(regexp="[a-zA-Z]*") String> names; @Size(max=10) private List<String> names; 12
  7. @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 13
  8. Cascaded Validation @Valid private List<Address> addresses; private List<@Valid Address> addresses;

    private Map<@Valid Comment, @Min(0) @Max(5) Integer> scorePerComment; 16
  9. 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; 18
  10. 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 19
  11. class TableValueExtractor implements ValueExtractor<Table<?, ?, @ExtractedValue ?>> { @Override public

    void extractValues(Table<?, ?, ?> originalValue, ValueExtractor.ValueReceiver receiver) { for ( Cell<?, ?, ?> cell : originalValue.cellSet() ) { receiver.keyedValue( "<table cell>", new CellKey( cell.getRowKey(), cell.getColumnKey() ), cell.getValue() ); } } } 20.2
  12. java.time API (JSR 310) @Past/@Future supported for java.time.LocalDateTime, ZonedDateTime etc.

    @Future private LocalDate deliveryDate = LocalDate.of( 2017, Month.MAY, 12 ); 21
  13. @Future private LocalDate deliveryDate = LocalDate.of( 2017, Month.MAY, 12 );

    java.time API (JSR 310) @Past/@Future supported for java.time.LocalDateTime, ZonedDateTime etc. @Future(orPresent=true) private LocalDate deliveryDate = LocalDate.of( 2017, Month.MAY, 12 ); 22
  14. java.time API (JSR 310) ValidatorFactory vf = Validation.byDefaultProvider() .configure() .clockProvider(

    new FixedClockProvider( ZonedDateTime.of( 2016, 6, 15, 0, 0, 0, 0, ZoneId.of( "Europe/Paris" ) ) ) ) .buildValidatorFactory(); 23
  15. Other Java 8 Goodies 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; } } 24
  16. Some More Improvements New constraints @NotEmpty, @NotBlank @Email @Positive, @Negative

    ValidatorFactory implements AutoCloseable try ( ValidatorFactory vf = Validation.buildDefaultValidatorFactory() ) { } 25
  17. JSF 2.3 and JAX-RS 2.1 JSF: Validation of class level

    constraints JAX-RS Consider Request Locale Option for disabling Bean Validation 26
  18. Status Spec: Public Review Reference implementation: Hibernate Validator 6.0 Beta1

    Your feedback is needed :-) Part of Java EE 8 (July 2017) 27
  19. Resources How to Contribute? API, specifikation, TCK, website Reference implementation

    Everything is open source (Apache License 2.0) beanvalidation.org/contribute/ github.com/beanvalidation/ github.com/hibernate/hibernate-validator/ 28
  20. 29