org.hibernate.validator.constraints
Annotation Type ScriptAssert


@Target(value=TYPE)
@Retention(value=RUNTIME)
@Constraint(validatedBy=ScriptAssertValidator.class)
@Documented
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.

For evaluation of expressions the Java Scripting API as defined by JSR 223 ("Scripting for the JavaTM Platform") is used. Therefore an implementation of that API must part of the class path. This is automatically the case when running on Java 6. For older Java versions, the JSR 223 RI can be added manually to the class path.

The expressions to be evaluated can be written in any scripting or expression language, for which a JSR 223 compatible engine can be found in the class path. The following listing shows an example using the JavaScript engine, which comes with Java 6:

 @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

Required Element Summary
 String lang
           
 String script
           
 
Optional Element Summary
 String alias
           
 Class<?>[] groups
           
 String message
           
 Class<? extends Payload>[] payload
           
 

Element Detail

lang

public abstract String lang
Returns:
The name of the script language used by this constraint as expected by the JSR 223 ScriptEngineManager. A ConstraintDeclarationException will be thrown upon script evaluation, if no engine for the given language could be found.

script

public abstract String script
Returns:
The script to be executed. The script must return Boolean.TRUE, if the annotated element could successfully be validated, otherwise Boolean.FALSE. Returning null or any type other than Boolean will cause a ConstraintDeclarationException 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 the script context using the name specified in the alias attribute.

message

public abstract String message
Default:
"{org.hibernate.validator.constraints.ScriptAssert.message}"

groups

public abstract Class<?>[] groups
Default:
{}

payload

public abstract Class<? extends Payload>[] payload
Default:
{}

alias

public abstract String alias
Returns:
The name, under which the annotated element shall be registered within the script context. Defaults to "_this".
Default:
"_this"


Copyright © 2007-2011 Red Hat Middleware, LLC. All Rights Reserved