2

I am trying to create my own validator for validating a List<String> read from a YAML configuration file. What I want to do, is validate it against a Enum class that I have created which looks like the following:

public enum BundleName {
    DK("dk.bundle"),
    UK("uk.bundle"),
    US("us.bundle"),
    DK_DEBUG("dk.bundle.debug"),
    UK_DEBUG("uk.bundle.debug"),
    US_DEBUG("us.bundle.debug"),
    ;

    private String bundleName;

    BundleName(String bundleName) {
        this.bundleName = bundleName;
    }

    public String getBundleName() {
      return this.bundleName
    }
}

My YAML file has the following defined:

bundleNames:
  - ${DK}
  - ${UK}
  - ${US}
  - ${DK_DEBUG}
  - ${UK_DEBUG}
  - ${US_DEBUG}

Now either some of these environment variables might be set or all of them might be set or just one is set. Different environment different combo. Anyway what I want is to be able to validate these environment variables against the bundleName in enum BundleName.class. Now Hibernate have a nice example, and also all other examples I find on the net is, where validation is done against a specific simple enum. I do not want to check on the enum name but the enum value, and I simply cannot figure out how. All what I have tried so fare is some combinations of what I found in some other posts like this and this and many more.

In my ApiConfiguration.java I would like to end up with something like the following on my list read from YAML:

@NotNull
@BundleNameValidation(acceptedValues = BundleName.class)
private List<String> bundleNames;

If that is possible somehow.

2
  • The approaches you linked should be capable of doing what you want. Please show what you have tried. Commented Oct 6, 2017 at 12:54
  • @flyx I have tried several combinations of the linked approaches also exactly the same approches on a List<String> but the linked approcahes do the validation on a single String field which is not of my interest. The only thing I have not tried which might work is defining the values as they do like for example @MyValidator(MyEnum.STRING) or @MyValidator(accpectedValues={MyEnum.VALUE1, MyEnum.VALUE2}. This approach is not desirable as I would have to add this to my validator each time I add a new Enum value. I am trying a new approach currently, if I find a solution I will post it. Commented Oct 10, 2017 at 12:25

0

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.