Uploaded image for project: 'Hibernate Validator'
  1. HV-714

Provide cross-parameter validator for @ScriptAssert

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 5.0.0.Beta1
    • Fix Version/s: 5.1.0.Alpha1
    • Component/s: engine
    • Labels:
      None
    • Last commented by a user?:
      true

      Description

      @ScriptAssert should be allowed to be used as cross-parameter constraint.

      We could also provide a dedicated constraint which makes the intention clearer and e.g. uses args as alias:

      @ParametersAssert(script="args[0] == args[1]", lang="javascript")
      public void resetPassword(String password, String repeated) { ... }
      

      with

      @Target({ METHOD, CONSTRUCTOR })
      @Retention(RUNTIME)
      @Constraint(validatedBy = {})
      @Documented
      @ScriptAssert(lang = "", script = "", alias = "args", validationAppliesTo = PARAMETERS)
      public @interface ParametersAssert {
      
          String message() default "{org.hibernate.validator.constraints.ParametersAssert.message}";
      
          Class<?>[] groups() default {};
      
          Class<? extends Payload>[] payload() default {};
      
          @OverridesAttribute(constraint = ScriptAssert.class, name = "script")
          String script();
      
          @OverridesAttribute(constraint = ScriptAssert.class, name = "lang")
          String lang();
      }
      

      In addition to accessing the parameters array directly, the parameters could also be bound to the script engine context under their names as retrieved from the current parameter name provider, allowing for even more concise expressions:

      @ParametersAssert(script="args0 == args1", lang="javascript")
      public void resetPassword(String password, String repeated) { ... }
      

        Attachments

          Issue links

            Activity

              People

              • Assignee:
                gunnar.morling Gunnar Morling
                Reporter:
                gunnar.morling Gunnar Morling
                Participants:
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: