@Documented @Constraint(validatedBy={}) @Target(value=TYPE) @Retention(value=RUNTIME) @Repeatable(value=ScriptAssert.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.
public abstract String lang
ScriptEngineManager
. A
ConstraintDeclarationException
will be thrown upon script
evaluation, if no engine for the given language could be found.public abstract String script
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.public abstract String message
public abstract Class<?>[] groups
public abstract String alias
public abstract String reportOn
Copyright © 2019 JBoss by Red Hat. All rights reserved.