javahibernate-validatorjsr223

Resolve: org.hibernate.validator.spi.scripting.ScriptEvaluatorNotFoundException: HV000232: No JSR 223 script engine found for language "javascript"


I'm encountering an issue with Hibernate Validator 6.2.3 while using Java 17. When attempting to validate entities using org.hibernate.validator.constraints.ScriptAssert, I'm getting the following error:

Caused by: org.hibernate.validator.spi.scripting.ScriptEvaluatorNotFoundException: HV000232: No JSR 223 script engine found for language "javascript".
    at org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory.createNewScriptEvaluator(DefaultScriptEvaluatorFactory.java:66)
    at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708)
    at org.hibernate.validator.spi.scripting.AbstractCachingScriptEvaluatorFactory.getScriptEvaluatorByLanguageName(AbstractCachingScriptEvaluatorFactory.java:41)
    at org.hibernate.validator.internal.engine.constraintvalidation.HibernateConstraintValidatorInitializationContextImpl.getScriptEvaluatorForLanguage(HibernateConstraintValidatorInitializationContextImpl.java:50)
    at org.hibernate.validator.internal.constraintvalidators.hv.AbstractScriptAssertValidator.initialize(AbstractScriptAssertValidator.java:38)

@ScriptAssert(lang=, script=, alias=, reportOn=)

Checks whether the given script can successfully be evaluated against the annotated element. In order to use this constraint, an implementation of the Java Scripting API as defined by JSR 223 ("Scripting for the JavaTM Platform") must be a part of the class path. The expressions to be evaluated can be written in any scripting or expression language, for which a JSR 223 compatible engine can be found in the class path. Even though this is a class-level constraint, one can use the reportOn attribute to report a constraint violation on a specific property rather than the whole object.

What are the standard implementations of JSR 223 API?

Any insights or guidance would be greatly appreciated. Thank you!


Solution

  • Nashorn (the built-in JDK javascript engine) was deprecated in JDK 11 and removed in JDK 15: https://openjdk.org/jeps/372

    As a result attempting to use @ScriptAssert on JDK 17 leads to the exception you see. You can try to add https://github.com/openjdk/nashorn:

    <dependency>
        <groupId>org.openjdk.nashorn</groupId>
        <artifactId>nashorn-core</artifactId>
        <version>15.4</version>
    </dependency>
    

    Or alternatively, you could try switching to a different script language like Groovy:

    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-jsr223</artifactId>
        <version>3.0.20</version>
    </dependency>
    

    but in this case, you'd need to update your validation script strings.

    BTW, while at it (adding dependencies) you may want to consider upgrading to Hibernate Validator 8.0 (it switched to jakarta.* APIs)