Top Description Inners Methods
org.hibernate.validator.constraints

public @Interface ScriptAssert

extends Annotation
Annotations
@Documented
@Constraint
validatedBy:
@Target:TYPE
@Retention:RUNTIME
@Repeatable:List
Static Imports
java.lang.annotation.ElementType.TYPE, .RetentionPolicy.RUNTIME

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 and Inner Type Summary

Modifier and TypeClass and Description
public static @interface
ScriptAssert.List

Defines several @ScriptAssert annotations on the same element.

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

The name, under which the annotated element shall be registered within the script context. Defaults to "_this".
alias
()

public Class<?>[]
public String

Returns:

The name of the script language used by this constraint as expected by the JSR 223 javax.script.ScriptEngineManager. A jakarta.validation.ConstraintDeclarationException will be thrown upon script evaluation, if no engine for the given language could be found.
lang
()

public String
public Class<? extends Payload>[]
public String

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.
reportOn
()

public String

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 jakarta.validation.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.
script
()

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

aliasback to summary
public String alias()
Returns:String

The name, under which the annotated element shall be registered within the script context. Defaults to "_this".

groupsback to summary
public Class<?>[] groups()
langback to summary
public String lang()
Returns:String

The name of the script language used by this constraint as expected by the JSR 223 javax.script.ScriptEngineManager. A jakarta.validation.ConstraintDeclarationException will be thrown upon script evaluation, if no engine for the given language could be found.

messageback to summary
public String message()
payloadback to summary
public Class<? extends Payload>[] payload()
reportOnback to summary
public String reportOn()
Returns:String

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
scriptback to summary
public String script()
Returns:String

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 jakarta.validation.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.

org.hibernate.validator.constraints back to summary

public @Interface ScriptAssert.List

extends Annotation
Annotations
@Target:TYPE
@Retention:RUNTIME
@Documented

Defines several @ScriptAssert annotations on the same element.

Method Summary

Modifier and TypeMethod and Description
public ScriptAssert[]
Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

valueback to summary
public ScriptAssert[] value()