3

I have a Hibernate validator that validates a field against a given list of strings. I will put code for better clarity.

    @Target({ METHOD, FIELD, ANNOTATION_TYPE })
    @Retention(RUNTIME)
    @Constraint(validatedBy = AllowedValuesValidator.class)
    @Documented
    public @interface AllowedValues {
      ...
      String[] value();
    }

Previously we were using it as

  @AllowedValues("value1")
  private String method;

Now we need to use it for a range of values, method can have multiple values. I tried both:

  @AllowedValues("Standard", "One-Day", "Two-Day", "Three-Day")
  private String method;     

and

  @AllowedValues("Standard, One-Day, Two-Day, Three-Day")
  private String method;

First one doesn't compile and second one takes whole string as allowed value(which is obvious).

Any ideas how to specify multiple values here?

1
  • 1
    I cannot find @ AllowedValues where is it in? Commented Apr 18, 2017 at 9:38

1 Answer 1

6

Since it's a String[], you need to use array initializer syntax for multiple values:

@AllowedValues({"Standard", "One-Day", "Two-Day", "Three-Day"}) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.