Skip to content

Commit

Permalink
HV-714 Updating reference guide
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Jul 2, 2013
1 parent b3d2b89 commit f7c8aa9
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
Expand Up @@ -892,4 +892,40 @@ assertEquals( 0, parameterNode.getParameterIndex() ); </programlisting>
<literal>arg0</literal>, <literal>arg1</literal> etc.</para>
</section>
</section>

<section id="section-builtin-method-constraints">
<title>Built-in method constraints</title>

<para>In addition to the built-in bean and property-level constraints
discussed in <xref linkend="section-builtin-constraints"/>, Hibernate
Validator currently provides one method-level constraint,
<classname>@ParameterScriptAssert</classname>. This is a generic
cross-parameter constraint which allows to implement validation routines
using any JSR 223 compatible ("Scripting for the
Java<superscript>TM</superscript> Platform") scripting language, provided
an engine for this language is available on the classpath.</para>

<para>To refer to the executable's parameters from within the expression,
use their name as obtained from the active parameter name provider (see
<xref linkend="section-parameter-name-provider"/>). <xref
linkend="example-parameterscriptassert"/> shows how the validation logic
of the <classname>@LuggageCountMatchesPassengerCount</classname>
constraint from <xref linkend="example-using-cross-parameter-constraint"/>
could be expressed with the help of
<classname>@ParameterScriptAssert</classname>.</para>

<example id="example-parameterscriptassert">
<title>Using <classname>@ParameterScriptAssert</classname></title>

<programlisting language="JAVA" role="JAVA">package org.hibernate.validator.referenceguide.chapter03.parametersscriptassert;

public class Car {

@ParameterScriptAssert(lang = "javascript", script = "arg1.size() &lt;= arg0.size() * 2")
public void load(List&lt;Person&gt; passengers, List&lt;PieceOfLuggage&gt; luggage) {
//...
}
} </programlisting>
</example>
</section>
</chapter>
@@ -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<Person> passengers, List<PieceOfLuggage> luggage) {
//...
}
}
@@ -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<ConstraintViolation<Car>> violations = executableValidator.validateParameters(
object,
method,
parameterValues
);

assertEquals( 1, violations.size() );
Class<? extends Annotation> constraintType = violations.iterator()
.next()
.getConstraintDescriptor()
.getAnnotation()
.annotationType();
assertEquals( ParameterScriptAssert.class, constraintType );
}
}
@@ -0,0 +1,4 @@
package org.hibernate.validator.referenceguide.chapter03.parameterscriptassert;

public class Person {
}
@@ -0,0 +1,4 @@
package org.hibernate.validator.referenceguide.chapter03.parameterscriptassert;

public class PieceOfLuggage {
}

1 comment on commit f7c8aa9

@WangJi92
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/bin/java -ea -Duser.language=en -Didea.test.cyclic.buffer.size=1048576 "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=54566:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit-rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit5-rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/tools.jar:/Users/wangji/Documents/study/hibernate-validator-example/documentation/target/test-classes:/Users/wangji/Documents/study/hibernate-validator-example/documentation/target/classes:/Users/wangji/.m2/repository/org/hibernate/validator/hibernate-validator/6.1.0-SNAPSHOT/hibernate-validator-6.1.0-SNAPSHOT.jar:/Users/wangji/.m2/repository/javax/validation/validation-api/2.0.1.Final/validation-api-2.0.1.Final.jar:/Users/wangji/.m2/repository/org/jboss/logging/jboss-logging/3.3.2.Final/jboss-logging-3.3.2.Final.jar:/Users/wangji/.m2/repository/com/fasterxml/classmate/1.3.4/classmate-1.3.4.jar:/Users/wangji/.m2/repository/org/glassfish/javax.el/3.0.1-b09/javax.el-3.0.1-b09.jar:/Users/wangji/.m2/repository/org/hibernate/validator/hibernate-validator-cdi/6.1.0-SNAPSHOT/hibernate-validator-cdi-6.1.0-SNAPSHOT.jar:/Users/wangji/.m2/repository/javax/enterprise/cdi-api/2.0.SP1/cdi-api-2.0.SP1.jar:/Users/wangji/.m2/repository/javax/inject/javax.inject/1/javax.inject-1.jar:/Users/wangji/.m2/repository/com/google/guava/guava/23.0/guava-23.0.jar:/Users/wangji/.m2/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar:/Users/wangji/.m2/repository/com/google/errorprone/error_prone_annotations/2.0.18/error_prone_annotations-2.0.18.jar:/Users/wangji/.m2/repository/com/google/j2objc/j2objc-annotations/1.1/j2objc-annotations-1.1.jar:/Users/wangji/.m2/repository/org/codehaus/mojo/animal-sniffer-annotations/1.14/animal-sniffer-annotations-1.14.jar:/Users/wangji/.m2/repository/org/codehaus/groovy/groovy-jsr223/2.4.12/groovy-jsr223-2.4.12.jar:/Users/wangji/.m2/repository/org/codehaus/groovy/groovy/2.4.12/groovy-2.4.12.jar:/Users/wangji/.m2/repository/org/osgi/org.osgi.core/6.0.0/org.osgi.core-6.0.0.jar:/Users/wangji/.m2/repository/org/springframework/spring-expression/4.3.10.RELEASE/spring-expression-4.3.10.RELEASE.jar:/Users/wangji/.m2/repository/org/springframework/spring-core/4.3.10.RELEASE/spring-core-4.3.10.RELEASE.jar:/Users/wangji/.m2/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar:/Users/wangji/.m2/repository/junit/junit/4.12/junit-4.12.jar:/Users/wangji/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/wangji/.m2/repository/org/assertj/assertj-core/3.8.0/assertj-core-3.8.0.jar:/Users/wangji/.m2/repository/javax/annotation/javax.annotation-api/1.2/javax.annotation-api-1.2.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 org.hibernate.validator.referenceguide.chapter03.parameterscriptassert.ParameterScriptAssertTest,validateParameters
Dec 06, 2018 9:52:27 AM org.hibernate.validator.internal.util.Version
INFO: HV000001: Hibernate Validator 6.1.0-SNAPSHOT

javax.validation.ConstraintDeclarationException: HV000023: Error during execution of script "luggage.size() <= passengers.size() * 2" occurred.

at org.hibernate.validator.internal.constraintvalidators.hv.ScriptAssertContext.evaluateScriptAssertExpression(ScriptAssertContext.java:52)
at org.hibernate.validator.internal.constraintvalidators.hv.ParameterScriptAssertValidator.isValid(ParameterScriptAssertValidator.java:58)
at org.hibernate.validator.internal.constraintvalidators.hv.ParameterScriptAssertValidator.isValid(ParameterScriptAssertValidator.java:33)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateSingleConstraint(ConstraintTree.java:181)
at org.hibernate.validator.internal.engine.constraintvalidation.SimpleConstraintTree.validateConstraints(SimpleConstraintTree.java:62)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:73)
at org.hibernate.validator.internal.metadata.core.MetaConstraint.doValidateConstraint(MetaConstraint.java:127)
at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:120)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:530)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraints(ValidatorImpl.java:512)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateParametersForSingleGroup(ValidatorImpl.java:929)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateParametersForGroup(ValidatorImpl.java:903)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateParametersInContext(ValidatorImpl.java:833)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateParameters(ValidatorImpl.java:264)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateParameters(ValidatorImpl.java:222)
at org.hibernate.validator.referenceguide.chapter03.parameterscriptassert.ParameterScriptAssertTest.validateParameters(ParameterScriptAssertTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: org.hibernate.validator.spi.scripting.ScriptEvaluationException: HV000233: An error occurred while executing the script: "luggage.size() <= passengers.size() * 2".
at org.hibernate.validator.spi.scripting.ScriptEngineScriptEvaluator.doEvaluate(ScriptEngineScriptEvaluator.java:70)
at org.hibernate.validator.spi.scripting.ScriptEngineScriptEvaluator.evaluate(ScriptEngineScriptEvaluator.java:60)
at org.hibernate.validator.internal.constraintvalidators.hv.ScriptAssertContext.evaluateScriptAssertExpression(ScriptAssertContext.java:49)
... 38 more
Caused by: javax.script.ScriptException: ReferenceError: "luggage" is not defined in at line number 1
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:454)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233)
at org.hibernate.validator.spi.scripting.ScriptEngineScriptEvaluator.doEvaluate(ScriptEngineScriptEvaluator.java:67)
... 40 more
Caused by: :1 ReferenceError: "luggage" is not defined
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291)
at jdk.nashorn.internal.objects.Global.noSuchProperty(Global.java:1441)
at jdk.nashorn.internal.scripts.Script$^eval_.:program(:1)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
... 45 more

when I change ParameterScriptAssert script is ok !change luggage arg0 ?
@ParameterScriptAssert(lang = "javascript", script = "arg1.size() <= arg0.size() * 2")
public void load(List passengers, List luggage) {
//...
}

Please sign in to comment.