diff --git a/documentation/src/main/docbook/en-US/modules/method-constraints.xml b/documentation/src/main/docbook/en-US/modules/method-constraints.xml index a9acdac1ee..ad8e39743f 100644 --- a/documentation/src/main/docbook/en-US/modules/method-constraints.xml +++ b/documentation/src/main/docbook/en-US/modules/method-constraints.xml @@ -892,4 +892,40 @@ assertEquals( 0, parameterNode.getParameterIndex() ); arg0, arg1 etc. + +
+ Built-in method constraints + + In addition to the built-in bean and property-level constraints + discussed in , Hibernate + Validator currently provides one method-level constraint, + @ParameterScriptAssert. This is a generic + cross-parameter constraint which allows to implement validation routines + using any JSR 223 compatible ("Scripting for the + JavaTM Platform") scripting language, provided + an engine for this language is available on the classpath. + + To refer to the executable's parameters from within the expression, + use their name as obtained from the active parameter name provider (see + ). shows how the validation logic + of the @LuggageCountMatchesPassengerCount + constraint from + could be expressed with the help of + @ParameterScriptAssert. + + + Using <classname>@ParameterScriptAssert</classname> + + package org.hibernate.validator.referenceguide.chapter03.parametersscriptassert; + +public class Car { + + @ParameterScriptAssert(lang = "javascript", script = "arg1.size() <= arg0.size() * 2") + public void load(List<Person> passengers, List<PieceOfLuggage> luggage) { + //... + } +} + +
diff --git a/documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/parameterscriptassert/Car.java b/documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/parameterscriptassert/Car.java new file mode 100644 index 0000000000..db97c9bfb3 --- /dev/null +++ b/documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/parameterscriptassert/Car.java @@ -0,0 +1,13 @@ +package org.hibernate.validator.referenceguide.chapter03.parameterscriptassert; + +import java.util.List; + +import org.hibernate.validator.constraints.ParameterScriptAssert; + +public class Car { + + @ParameterScriptAssert(lang = "javascript", script = "arg1.size() <= arg0.size() * 2") + public void load(List passengers, List luggage) { + //... + } +} diff --git a/documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/parameterscriptassert/ParameterScriptAssertTest.java b/documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/parameterscriptassert/ParameterScriptAssertTest.java new file mode 100644 index 0000000000..4070825ee7 --- /dev/null +++ b/documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/parameterscriptassert/ParameterScriptAssertTest.java @@ -0,0 +1,53 @@ +package org.hibernate.validator.referenceguide.chapter03.parameterscriptassert; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.ValidatorFactory; +import javax.validation.executable.ExecutableValidator; + +import org.junit.BeforeClass; +import org.junit.Test; + +import org.hibernate.validator.constraints.ParameterScriptAssert; + +import static org.junit.Assert.assertEquals; + +public class ParameterScriptAssertTest { + + private static ExecutableValidator executableValidator; + + @BeforeClass + public static void setUpValidator() { + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + executableValidator = factory.getValidator().forExecutables(); + } + + @Test + public void validateParameters() throws Exception { + Car object = new Car(); + Method method = Car.class.getMethod( "load", List.class, List.class ); + Object[] parameterValues = { + Arrays.asList( new Person() ), + Arrays.asList( new PieceOfLuggage(), new PieceOfLuggage(), new PieceOfLuggage() ) + }; + + Set> violations = executableValidator.validateParameters( + object, + method, + parameterValues + ); + + assertEquals( 1, violations.size() ); + Class constraintType = violations.iterator() + .next() + .getConstraintDescriptor() + .getAnnotation() + .annotationType(); + assertEquals( ParameterScriptAssert.class, constraintType ); + } +} diff --git a/documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/parameterscriptassert/Person.java b/documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/parameterscriptassert/Person.java new file mode 100644 index 0000000000..129f4459f4 --- /dev/null +++ b/documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/parameterscriptassert/Person.java @@ -0,0 +1,4 @@ +package org.hibernate.validator.referenceguide.chapter03.parameterscriptassert; + +public class Person { +} diff --git a/documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/parameterscriptassert/PieceOfLuggage.java b/documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/parameterscriptassert/PieceOfLuggage.java new file mode 100644 index 0000000000..d28f693599 --- /dev/null +++ b/documentation/src/test/java/org/hibernate/validator/referenceguide/chapter03/parameterscriptassert/PieceOfLuggage.java @@ -0,0 +1,4 @@ +package org.hibernate.validator.referenceguide.chapter03.parameterscriptassert; + +public class PieceOfLuggage { +}