Annotation Type ScriptAssert
@Documented
@Constraint(validatedBy={})
@Target(TYPE)
@Retention(RUNTIME)
@Repeatable(List.class)
public @interface ScriptAssert
A class-level constraint, that evaluates a script expression against the annotated element. This constraint can be used to implement validation routines, that depend on multiple attributes of the annotated element.
Script expressions can be written in any scripting or expression language, for which a JSR 223 ("Scripting for the JavaTM Platform") compatible engine can be found on the classpath. The following listing shows an example using the JavaScript engine, which comes with the JDK:
@ScriptAssert(lang = "javascript", script = "_this.startDate.before(_this.endDate)")
public class CalendarEvent {
private Date startDate;
private Date endDate;
//...
}
Using a real expression language in conjunction with a shorter object alias allows for very compact expressions:
@ScriptAssert(lang = "jexl", script = "_.startDate > _.endDate", alias = "_")
public class CalendarEvent {
private Date startDate;
private Date endDate;
//...
}
Accepts any type.
- Author:
- Gunnar Morling
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic @interface
Defines several@ScriptAssert
annotations on the same element. -
Required Element Summary
-
Optional Element Summary
-
Element Details
-
lang
String lang- Returns:
- The name of the script language used by this constraint as
expected by the JSR 223
ScriptEngineManager
. AConstraintDeclarationException
will be thrown upon script evaluation, if no engine for the given language could be found.
-
script
String script- Returns:
- The script to be executed. The script must return
Boolean.TRUE
, if the annotated element could successfully be validated, otherwiseBoolean.FALSE
. Returning null or any type other than Boolean will cause aConstraintDeclarationException
upon validation. Any exception occurring during script evaluation will be wrapped into a ConstraintDeclarationException, too. Within the script, the validated object can be accessed from thescript context
using the name specified in thealias
attribute.
-
-
-
message
String message- Default:
- "{org.hibernate.validator.constraints.ScriptAssert.message}"
-
groups
Class<?>[] groups- Default:
- {}
-
payload
- Default:
- {}
-
alias
String alias- Returns:
- The name, under which the annotated element shall be registered within the script context. Defaults to "_this".
- Default:
- "_this"
-
reportOn
String reportOn- Returns:
- The name of the property for which you would like to report a validation error. If given, the resulting constraint violation will be reported on the specified property. If not given, the constraint violation will be reported on the annotated bean.
- Since:
- 5.4
- Default:
- ""
-