Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 package org.hibernate.validator.constraints;
 
 
 import static java.lang.annotation.ElementType.TYPE;
 import static java.lang.annotation.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 class path. 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(s):
Gunnar Morling
 
 @Target({  })
 @Constraint(validatedBy = { })
 public @interface ScriptAssert {
 
 	String message() default "{org.hibernate.validator.constraints.ScriptAssert.message}";
 
 	Class<?>[] groups() default { };
 
 	Class<? extends Payload>[] payload() default { };

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

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 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 script context using the name specified in the alias attribute.

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

Defines several @ScriptAssert annotations on the same element.
	@Target({  })
	public @interface List {
	}
New to GrepCode? Check out our FAQ X