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.

Anthon
  • 69,918
  • 32
  • 186
  • 246
Yantes
  • 251
  • 3
  • 18
  • The approaches you linked should be capable of doing what you want. Please show what you have tried. – flyx Oct 06 '17 at 12:54
  • @flyx I have tried several combinations of the linked approaches also exactly the same approches on a `List` 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. – Yantes Oct 10 '17 at 12:25

0 Answers0