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

Keeping Your Data Sane with Bean Validation 2.0 (jdk.io)

Keeping Your Data Sane with Bean Validation 2.0 (jdk.io)

Introduction to the new features added in Bean Validation 2.0. Slides from a talk at the jdk.io conference in Copenhagen.

Gunnar Morling

June 19, 2017
Tweet

More Decks by Gunnar Morling

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  8. Use Case: Ensure Non-Empty
    Collection Elements
    private List names;
    9

    View full-size slide

  9. Use Case: Ensure Non-Empty
    Collection Elements
    private List names;
    @NotEmpty
    private List names;
    10

    View full-size slide

  10. Use Case: Ensure Non-Empty
    Collection Elements
    private List names;
    @OnElements(constraint=@NotEmpty)
    private List names;
    11

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  14. @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

    View full-size slide

  15. Cascaded Validation
    @Valid
    private List addresses;
    16

    View full-size slide

  16. Cascaded Validation
    @Valid
    private List addresses;
    private List<@Valid Address> addresses;
    17

    View full-size slide

  17. Cascaded Validation
    @Valid
    private List addresses;
    private List<@Valid Address> addresses;
    private Map<@Valid Comment, Integer> scorePerComment;
    18

    View full-size slide

  18. Cascaded Validation
    @Valid
    private List addresses;
    private List<@Valid Address> addresses;
    private Map<@Valid Comment, Integer> scorePerComment;
    private Map<@Valid AddressType,
    List<@Valid Address>> addressesByType;
    19

    View full-size slide

  19. Further Supported Containers
    Optional, OptionalInt, OptionalLong, OptionalDouble
    JavaFX's property types
    Optional<@Email String> getEmail() { ... };
    20

    View full-size slide

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

    View full-size slide

  21. 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
    22

    View full-size slide

  22. 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()
    );
    }
    }
    }
    24

    View full-size slide

  23. 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
    );
    25

    View full-size slide

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

    View full-size slide

  25. 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();
    27

    View full-size slide

  26. Other Java 8 Goodies
    Real parameter names in error messages
    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;
    }
    }
    28

    View full-size slide

  27. New Constraints
    @NotEmpty, @NotBlank
    @Email
    @Positive, @PositiveOrZero , @Negative, @NegativeOrZero
    29

    View full-size slide

  28. Status
    Passed Public Review
    Ballot last week
    30

    View full-size slide

  29. Next Steps
    Proposed Final Draft this week
    Reference implementation: Hibernate Validator 6.0 Beta2
    Your feedback is needed :-)
    Part of Java EE 8 (July 2017)
    31

    View full-size slide

  30. Outlook Bean Validation 2.1
    Constraint-API
    mapping.constraintDefinition( FileExists.class )
    .validateType( File.class )
    .with( f -> f.exists() );
    Constraint-API: Lambda
    ConstraintMapping mapping = ...;
    mapping.type( Marathon.class )
    .property( "numberOfHelpers", FIELD )
    .constraint( new MinDef().value( 1 ) )
    .property( "runners", METHOD )
    .valid();
    32

    View full-size slide

  31. Resources
    All changes
    Reference implementation
    API, specifikation, TCK, website
    Everything is open source
    @gunnarmorling
    http://beanvalidation.org/latest-draft/diff-to-1.1/
    github.com/hibernate/hibernate-validator/
    github.com/beanvalidation/
    33

    View full-size slide