Hibernate Validator 6.0.0.Alpha2 改进对 Bean Validation 2.0 的支持

简介:

Hibernate Validator 6.0.0.Alpha2 发布了。Hibernate Validator 6 将成为 Bean Validation 2.0 的参考实现。此 Alpha2 版本与 Bean Validation 规范的 2.0.0.Alpha2 版本协调。

请注意,Hibernate Validator 6 需要 JDK 8 或更高版本。

自 Alpha1 以来的新功能

改进对容器元素验证的支持

Container element validation support is the big new feature of Bean Validation 2.0 and it got some more love during this cycle.

编程 API 和 XML 支持

To obtain the equivalent of:

public class FishTank {
      
          private Optional<@Valid FishTankModel> model;
      
          private Map<@Size(min = 3, max = 10) String, @Min(1) Integer> fishCountByType;
      }

it is now possible to declare container element type constraints and cascaded validation either via an XML mapping configuration (which is part of the Bean Validation 2.0 specification):

[...]
           class="com.acme.FishTank" ignore-annotations="false">
               name="model">
                  
                       />
                  
              
               name="fishCountByType">
                   typeArgumentIndex="0">
                       annotation="javax.validation.constraints.Size">
                           name="min">
                              3
                          
                           name="max">
                              10
                          
                      
                  
                   typeArgumentIndex="1">
                       annotation="javax.validation.constraints.Min">
                           name="value">
                              1
                          
                      
                  
              
          
      [...]

or via the (implementation specific) programmatic API:

ConstraintMapping newMapping = config.createConstraintMapping();
      newMapping
          .type( FishTank.class )
              .property( "model", FIELD )
                  .containerElementType()
                      .valid()
              .property( "fishCountByType", FIELD )
                  .containerElementType( 0 )
                      .constraint( new SizeDef().min( 3 ).max( 10 ) )
                  .containerElementType( 1 )
                      .constraint( new MinDef().value( 1 ) );

其他改进

We also made a couple of more minor improvements:

the TYPE_ARGUMENT node type has been renamed to CONTAINER_ELEMENT (type argument don&rsquo;t make sense for annotated array elements)

we allow per context custom value extractors

we allow value extractors to be contributed using the service loader mechanism

we allow to specify value extractors using XML configuration

we explore the parent classes and interfaces to search the ValueExtractor definition

支持添加到 Bean Validation 2.0 的新约束

In the Alpha2 release of Bean Validation 2.0, we introduced the following new constraints:

@NotBlank: check that a char sequence is not blank (i.e. not null, and length of trimmed char sequence > 0)

@NotEmpty: check that an element (char sequence, collection, array) is not null and not empty

@Email: check that a char sequence is a valid email

@Positive: check that a number is positive

@Negative: check that a number is negative

We added support for these new constraints to Hibernate Validator.

性能改进

We started to run a few benchmarks on Hibernate Validator 6 and we fixed a few performance regressions.

6.0.0.Alpha2 is now significantly faster than 5.4, mostly thanks to the new ValueExtractor design.

获取 6.0.0.Alpha2

To get the release with Maven, Gradle etc. use the GAV coordinates org.hibernate.validator:{hibernate-validator|hibernate-validator-cdi|hibernate-validator-annotation-processor}:6.0.0.Alpha2. Note that the group id has changed from org.hibernate (Hibernate Validator 5 and earlier) to org.hibernate.validator (from Hibernate Validator 6 onwards).

Alternatively, a distribution bundle containing all the bits is provided on SourceForge (TAR.GZ, ZIP).

反馈, issues, 建议?

To get in touch, use the usual channels:

hibernate-validator tag on Stackoverflow (usage questions)

User forum (usage questions, general feedback)

Issue tracker (bug reports, feature requests)

Mailing list (development-related discussions)

Bean Validation development mailing list (discussions about the Bean Validation specification)

下一步是什么?

Bean Validation 2.0 和 Hibernate Validator 6 仍在积极开发中,会定期发布新的 alpha 版本。

本文来自开源中国社区 [http://www.oschina.net]

目录
相关文章
|
7月前
|
Java 数据库连接 API
【hibernate validator】(二)声明和验证Bean约束(下)
【hibernate validator】(二)声明和验证Bean约束(下)
|
7月前
|
Java 数据库连接 容器
【hibernate validator】(二)声明和验证Bean约束(上)
【hibernate validator】(二)声明和验证Bean约束
|
11月前
|
前端开发 Java 数据库连接
Hibernate Validator -- 伟大的校验器
validator Engine 是支持Javax.validator 的接口的实现, 并且可以通过一些简单的标注的形式(annotation形式)实现一个校验的形式, 它其实也是一个约定大于执行的过程
93 0
|
前端开发 Java 数据库连接
源码解析最流行的Validator框架——Hibernate Validator
源码解析最流行的Validator框架——Hibernate Validator
562 0
源码解析最流行的Validator框架——Hibernate Validator
|
Java 数据库连接
[原创]自定义Hibernate Validator校验注解
[原创]自定义Hibernate Validator校验注解
[原创]自定义Hibernate Validator校验注解
|
Java 数据库连接
SpringBoot 2.0参数校验Hibernate Validator
SpringBoot 2.0参数校验Hibernate Validator
SpringBoot 2.0参数校验Hibernate Validator
|
Java 数据库连接 API
springboot使用hibernate validator校验
在做web相关的应用时,经常需要提供接口与用户交互(获取数据、上传数据等),由于这个过程需要用户进行相关的操作,为了避免出现一些错误的数据等,一般需要对数据进行校验,随着接口的增多,校验逻辑的冗余度也越来越大,虽然可以通过抽象出校验的方法来处理,但还是需要每次手动调用校验逻辑,相对来说还是不方便。
3581 0
|
Java 数据库连接
自定义Hibernate Validator校验注解
在《SpringBoot 2.0参数校验Hibernate Validator》基础上开发 定义注解 package com.futao.springmvcdemo.
2176 0
|
Java 数据库连接 程序员
SpringBoot 2.0参数校验Hibernate Validator
Spring Boot (v2.0.5.RELEASE)Hibernate Validator springboot起步依赖自动添加了对hibernate validator的依赖 hibernate validator依赖 或者也可以自己手动添加依赖 org.
4270 0