内置的约束条件
Hibernate Validator包含了一些基本的使用比较广的约束,下面是一些Hibernate Validator给出的最常用的约束.另外Hibernate Validator还提供了一些有有的custom约束.
一.Bean Validation constraints
下面是一些常的constraints,它们的作用,支持的数据类型.它们全部都是属性级别的注释,如果你使用Hibernate 对象-关系映射,当你为你的model创建ddl时,有一些约束是要被考虑在内的.
注意:Hibernate Validator的有些约束是能支持比Bean Validation specification要求的数据类型的更多的类型.e.g. @Max can be applied to Strings
Annotation | Supported data types | 作用 | Hibernate metadata impact |
@AssertFalse | Boolean , boolean | Checks that the annotated elementis false . | 没有 |
@AssertTrue | Boolean , boolean | Checks that the annotated elementis true . | 没有 |
@DecimalMax | BigDecimal , BigInteger , String ,byte , short , int ,long and therespective wrappersof the primitivetypes. Additionallysupported byHV: any sub-typeof Number and CharSequence . | 被标注的值必须 不大于约束中指定的最大值. 这个约束的参数是一个通过 BigDecimal 定义的最大值的字符串表示. | 没有 |
@DecimalMin | BigDecimal , BigInteger , String ,byte , short , int ,long and therespective wrappersof the primitivetypes. Additionallysupported byHV: any sub-typeof Number andCharSequence . | 被标注的值必须 不小于约束中指定的最小值. 这个约束的参数是一个通过 BigDecimal 定义的最小值的字符串表示. | 没有 |
@Digits(integer=, fraction= | BigDecimal , BigInteger , String ,byte , short , int ,long and therespective wrappersof the primitivetypes. Additionallysupported byHV: any sub-typeof Number andCharSequence . | Checks whether the annoted value is anumber having up tointeger digits andfraction fractionaldigits | 对应的数据库表 字段会被设置精度(precision)和准度(scale). |
@Future | java.util.Date , java.util.Calendar ;Additionallysupported by HV,if the Joda Time[http://joda-time.sourceforge.net/] date/time API ison the class path:any implementationsof ReadablePartialand ReadableInstant . | 检查给定的日期是否 比现在晚. | 没有 |
@Max | BigDecimal , BigInteger , byte ,short , int , longand the respectivewrappers of theprimitive types.Additionallysupported by HV:any sub-type ofCharSequence (thenumeric valuerepresented by thechar sequence isevaluated), anysub-type of Number . | 检查该值是否大于或 等于约束条件中规定的最大值 | 会给对应的数据库表 字段添加一个check的约束条件. |
@Min | BigDecimal , BigInteger , byte ,short , int , longand the respectivewrappers of theprimitive types.Additionallysupported by HV:any sub-type ofCharSequence (thenumeric valuerepresented by thechar sequence isevaluated), anysub-type of Number . | 检查该值是否大于或 等于约束条件中规定的最小值. | 的最小值. 会给对应的数据库表字段添加一个check的约束条件. |
@NotNull | Any type | Checks that the annotated value isnot null. | 对应的表字段不允许 为null. |
@Null | Any type | Checks that the annotated value is null. | 没有 |
@Past | java.util.Date , java.util.Calendar ;Additionallysupported by HV,if the Joda Time[http://joda-time.sourceforge.net/] date/time API ison the class path:any implementationsof ReadablePartial and ReadableInstant . | 检查标注对象中的值 表示的日期比当前早. | 没有 |
@Pattern(regex=, flag= | String . Additionally supported by HV:any sub-type ofCharSequence | 检查该字符串是否能 够在 match 指定的情况下被 regex 定义的正则表达式匹配. | 没有 |
@Size(min=, max=) | String , Collection , Map and arrays .Additionallysupported by HV:any sub-type ofCharSequence . | Checks if the annotated element'ssize is betweenmin and max(inclusive). | 对应的数据库表字段 的长度会被设置成约束中定义的最大值. |
@Valid | Any non-primitive type | 递归的对关联对象进 行校验, 如果关联对象是个集合或者数组,那么对其中的元素进行递归校验,如果是一个map,则对其中的值部分进行校验 | 没有 |
注意: 这些约束都能支持message,groups,payload的参数 |
二,Additional constraints
除了Bean Validation API Hibernate Validator中的约束外,还有一些有用的定制的约束.其中@ScriptAssert是一个类级别的注释
Annotation | Supported data types | 作用 | Hibernate metadata impact |
@CreditCardNumber | CharSequence | Checks that the annotated charactersequence passesthe Luhn checksumtest. Note, this validation aimsto check foruser mistakes,not credit cardvalidity! Seealso Anatomyof Credit CardNumbers [http://www.merriampark.com/anatomycc.htm]. | 没有 |
CharSequence | Checks whether the specified charactersequence is a validemail address. Theoptional parametersregexp and flagsallow to specifyan additionalregular expression(including regularexpression flags)which the emailmust match. | 没有 | |
@Length(min=, max=) | CharSequence | Validates that the annotatedcharacter sequenceis between min andmax included. | 对应的数据库表字段 的长度会被设置成约束中定义的最大值 |
@ModCheck(modType=, multiplier=,startIndex=,endIndex=,checkDigitPosition=,ignoreNonDigitCharacters=) | CharSequence | Checks that the digits withinthe annotatedcharacter sequencepass the mod 10or mod 11 checksumalgorithm. modTypeis used to selectthe modulo typeand the multiplierdetermines thealgorithm specificmultiplier (see also Luhnalgorithm [http://en.wikipedia.org/wiki/Luhn_algorithm]).startIndex andendIndex allowto only run themodulo algorithmon the specifiedsub-string.checkDigitPositionallows to usean arbitrarydigit withinthe charactersequence to be thecheck digit. Ifnot specified itis assumed thatthe check digitis part of thespecified range.Last but not least,ignoreNonDigitCharactersallows toignore non digitcharacters. | 没有 |
@NotBlank | CharSequence | Checks that the annotated charactersequence is notnull and thetrimmed length isgreater than 0.The difference to@NotEmpty is thatthis constraintcan only be appliedon strings andthat trailingwhitespaces are ignored. | 没有 |
@NotEmpty | CharSequence , Collection , Map andarrays | Checks whether the annotated elementis not null norempty | 没有 |
@Range(min=, max=) | BigDecimal , BigInteger ,CharSequence , byte ,short , int , longand the respectivewrappers of theprimitive types | Checks whether the annotatedvalue lies between(inclusive) thespecified minimumand maximum. | 没有 |
@SafeHtml(whitelistType=, additionalTags=) | CharSequence | Checks whether the annotatedvalue containspotentiallymalicious fragmentssuch as <script/> . In order to usethis constraint,the jsoup [http://jsoup.org/] librarymust be part of theclass path. Withthe whitelistTypeattributepredefinedwhitelist typescan be chosen. Youcan also specifyadditional htmltags for thewhitelist withthe additionalTagsattribute. | 没有 |
@ScriptAssert(lang=, script=, alias=) | Any type | Checks whether the given script cansuccessfully beevaluated againstthe annotatedelement. In order to use thisconstraint, animplementationof the JavaScripting API asdefined by JSR 223("Scripting for theJava TM Platform")must part of theclass path. Theexpressions tobe evaluated canbe written inany scriptingor expressionlanguage, forwhich a JSR 223compatible enginecan be found in theclass path. | 没有 |
@URL(protocol=, host=, port=,regexp=, flags=) | CharSequence | Checks if the annotated charactersequence is a validURL according toRFC2396. If anyof the optionalparameters protocol ,host or port arespecified, thecorresponding URLfragments mustmatch the specifiedvalues. Theoptional parametersregexp and flagsallow to specifyan additionalregular expression(including regularexpression flags)which the URL must match. | 没有 |
三, Country specific constraints(略)