Slide 1

Slide 1 text

Keeping Your Data Sane with Bean Validation 2.0 Gunnar Morling, Red Hat 1

Slide 2

Slide 2 text

Agenda What is Bean Validation? What's new in Bean Validation 2.0? Questions, feedback 2

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Demo 5

Slide 6

Slide 6 text

Bean Validation 2.0 - JSR 380 Benefit from new language features Support for API extensions 6

Slide 7

Slide 7 text

Repeatable Annotations @Size.List({ @Size(min = 8, group = Default.class), @Size(min = 12, group = Admin.class) }) private char[] password = ...; 7

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Collection Validation private List names; 9

Slide 10

Slide 10 text

Collection Validation private List names; @Size(max=10) private List names; 10

Slide 11

Slide 11 text

Collection Validation private List names; @Size(max=10) private List<@Pattern(regexp="[a-zA-Z]*") String> names; @Size(max=10) private List names; 11

Slide 12

Slide 12 text

Collection Validation private List 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 names; 12

Slide 13

Slide 13 text

@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

Slide 14

Slide 14 text

Cascaded Validation @Valid private List
addresses; 14

Slide 15

Slide 15 text

Cascaded Validation @Valid private List
addresses; private List<@Valid Address> addresses; 15

Slide 16

Slide 16 text

Cascaded Validation @Valid private List
addresses; private List<@Valid Address> addresses; private Map<@Valid Comment, @Min(0) @Max(5) Integer> scorePerComment; 16

Slide 17

Slide 17 text

java.util.Optional Neuer ElementType TYPE Also for StringProperty et al. (JavaFX) Optional<@Email String> getEmail() { ... }; 17

Slide 18

Slide 18 text

Custom Containers Specific collection types (e.g. Google Guava) Other JVM languages (Ceylon, Scala etc.) Enabled via Extractor SPI private Table revenuePerYearAndCategory; 18

Slide 19

Slide 19 text

private Table revenuePerYearAndCategory; private Table revenuePerYearAndCategory; Custom Containers Specific collection types (e.g. Google Guava) Other JVM languages (Ceylon, Scala etc.) Enabled via Extractor SPI 19

Slide 20

Slide 20 text

Demo 20.1

Slide 21

Slide 21 text

class TableValueExtractor implements ValueExtractor> { @Override public void extractValues(Table originalValue, ValueExtractor.ValueReceiver receiver) { for ( Cell cell : originalValue.cellSet() ) { receiver.keyedValue( "", new CellKey( cell.getRowKey(), cell.getColumnKey() ), cell.getValue() ); } } } 20.2

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

@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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Other Java 8 Goodies ConstraintValidator without initialize() public class NotNullValidator implements ConstraintValidator { public void initialize(NotNull constraintAnnotation) { } public boolean isValid(Object object, ConstraintValidatorContext ctx) { return object != null; } } public class NotNullValidator implements ConstraintValidator { public boolean isValid(Object object, ConstraintValidatorContext ctx) { return object != null; } } 24

Slide 26

Slide 26 text

Some More Improvements New constraints @NotEmpty, @NotBlank @Email @Positive, @Negative ValidatorFactory implements AutoCloseable try ( ValidatorFactory vf = Validation.buildDefaultValidatorFactory() ) { } 25

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Status Spec: Public Review Reference implementation: Hibernate Validator 6.0 Beta1 Your feedback is needed :-) Part of Java EE 8 (July 2017) 27

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

29