All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.validator.constraints.ScriptAssert Maven / Gradle / Ivy

/*
 * Hibernate Validator, declare and validate application constraints
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or .
 */
package org.hibernate.validator.constraints;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

import org.hibernate.validator.constraints.ScriptAssert.List;

/**
 * 

* 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: *

*
 * {@code @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: *

* *
 * {@code @ScriptAssert(lang = "jexl", script = "_.startDate > _.endDate", alias = "_")
 * public class CalendarEvent {
 *
 * 	private Date startDate;
 *
 * 	private Date endDate;
 *
 * 	//...
 *
 * }
 * }
 * 
*

* Accepts any type. *

* * @author Gunnar Morling */ @Documented @Constraint(validatedBy = { }) @Target({ TYPE }) @Retention(RUNTIME) @Repeatable(List.class) public @interface ScriptAssert { String message() default "{org.hibernate.validator.constraints.ScriptAssert.message}"; Class[] groups() default { }; Class[] payload() default { }; /** * @return The name of the script language used by this constraint as * expected by the JSR 223 {@link javax.script.ScriptEngineManager}. A * {@link javax.validation.ConstraintDeclarationException} will be thrown upon script * evaluation, if no engine for the given language could be found. */ String lang(); /** * @return The script to be executed. The script must return * {@code Boolean.TRUE}, if the annotated element could * successfully be validated, otherwise {@code Boolean.FALSE}. * Returning null or any type other than Boolean will cause a * {@link javax.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 {@link javax.script.ScriptContext * script context} using the name specified in the * {@code alias} attribute. */ String script(); /** * @return The name, under which the annotated element shall be registered * within the script context. Defaults to "_this". */ String alias() default "_this"; /** * @return 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 */ String reportOn() default ""; /** * Defines several {@code @ScriptAssert} annotations on the same element. */ @Target({ TYPE }) @Retention(RUNTIME) @Documented public @interface List { ScriptAssert[] value(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy