/engine/src/main/java/org/hibernate/validator/constraints/ScriptAssert.java

https://github.com/gunnarmorling/hibernate-validator · Java · 122 lines · 29 code · 12 blank · 81 comment · 0 complexity · cf26e30e5bd2e45599ee86e1f34af35b MD5 · raw file

  1. /*
  2. * Hibernate Validator, declare and validate application constraints
  3. *
  4. * License: Apache License, Version 2.0
  5. * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
  6. */
  7. package org.hibernate.validator.constraints;
  8. import static java.lang.annotation.ElementType.TYPE;
  9. import static java.lang.annotation.RetentionPolicy.RUNTIME;
  10. import java.lang.annotation.Documented;
  11. import java.lang.annotation.Repeatable;
  12. import java.lang.annotation.Retention;
  13. import java.lang.annotation.Target;
  14. import javax.validation.Constraint;
  15. import javax.validation.Payload;
  16. import org.hibernate.validator.constraints.ScriptAssert.List;
  17. /**
  18. * <p>
  19. * A class-level constraint, that evaluates a script expression against the
  20. * annotated element. This constraint can be used to implement validation
  21. * routines, that depend on multiple attributes of the annotated element.
  22. * </p>
  23. * <p>
  24. * Script expressions can be written in any scripting or expression language,
  25. * for which a <a href="http://jcp.org/en/jsr/detail?id=223">JSR 223</a>
  26. * ("Scripting for the Java<sup>TM</sup> Platform") compatible engine can be
  27. * found on the classpath. The following listing shows an example using the
  28. * JavaScript engine, which comes with the JDK:
  29. * </p>
  30. * <pre>
  31. * {@code @ScriptAssert(lang = "javascript", script = "_this.startDate.before(_this.endDate)")
  32. * public class CalendarEvent {
  33. *
  34. * private Date startDate;
  35. *
  36. * private Date endDate;
  37. *
  38. * //...
  39. *
  40. * }
  41. * }
  42. * </pre>
  43. * <p>
  44. * Using a real expression language in conjunction with a shorter object alias
  45. * allows for very compact expressions:
  46. * </p>
  47. *
  48. * <pre>
  49. * {@code @ScriptAssert(lang = "jexl", script = "_.startDate > _.endDate", alias = "_")
  50. * public class CalendarEvent {
  51. *
  52. * private Date startDate;
  53. *
  54. * private Date endDate;
  55. *
  56. * //...
  57. *
  58. * }
  59. * }
  60. * </pre>
  61. * <p>
  62. * Accepts any type.
  63. * </p>
  64. *
  65. * @author Gunnar Morling
  66. */
  67. @Documented
  68. @Constraint(validatedBy = { })
  69. @Target({ TYPE })
  70. @Retention(RUNTIME)
  71. @Repeatable(List.class)
  72. public @interface ScriptAssert {
  73. String message() default "{org.hibernate.validator.constraints.ScriptAssert.message}";
  74. Class<?>[] groups() default { };
  75. Class<? extends Payload>[] payload() default { };
  76. /**
  77. * @return The name of the script language used by this constraint as
  78. * expected by the JSR 223 {@link javax.script.ScriptEngineManager}. A
  79. * {@link javax.validation.ConstraintDeclarationException} will be thrown upon script
  80. * evaluation, if no engine for the given language could be found.
  81. */
  82. String lang();
  83. /**
  84. * @return The script to be executed. The script must return
  85. * <code>Boolean.TRUE</code>, if the annotated element could
  86. * successfully be validated, otherwise <code>Boolean.FALSE</code>.
  87. * Returning null or any type other than Boolean will cause a
  88. * {@link javax.validation.ConstraintDeclarationException} upon validation. Any
  89. * exception occurring during script evaluation will be wrapped into
  90. * a ConstraintDeclarationException, too. Within the script, the
  91. * validated object can be accessed from the {@link javax.script.ScriptContext
  92. * script context} using the name specified in the
  93. * <code>alias</code> attribute.
  94. */
  95. String script();
  96. /**
  97. * @return The name, under which the annotated element shall be registered
  98. * within the script context. Defaults to "_this".
  99. */
  100. String alias() default "_this";
  101. /**
  102. * Defines several {@code @ScriptAssert} annotations on the same element.
  103. */
  104. @Target({ TYPE })
  105. @Retention(RUNTIME)
  106. @Documented
  107. public @interface List {
  108. ScriptAssert[] value();
  109. }
  110. }