jeudi 26 septembre 2019

how to validate two fields in separate classes?

I am trying to validate two fields in separate @ConfigurationProperties classes. Properties in these classes are set by spring container. I can't merge these two classes because of application design. How can achieve this validation?

I tried Observer pattern but its for one-to-many not many-to-one.

Trying to validate if multipleQueuesEnabled is set to false then url should not be null and urls should be null. And if multipleQueuesEnabled is set to true then urls should not be null and url should be null.

First Properties Class:-

@ConfigurationProperties(prefix = "aws.sqs")
@Validated
public class SqsProducerStarterProperties

    @NotNull
    private boolean multipleQueuesEnabled;


    public boolean isMultipleQueuesEnabled() {
        return multipleQueuesEnabled;
    }

    public void setMultipleQueuesEnabled(boolean multipleQueuesEnabled) {

        this.multipleQueuesEnabled = multipleQueuesEnabled;
    }

}

Second Properties Class:-

@ConfigurationProperties(prefix = "aws.sqs.fifo")
@Validated
public class SqsFifoStarterProperties {

    private Map<String, String> urls;

    private String url;

    @Override
    public Map<String, String> getUrls() {
        return urls;
    }

    public void setUrls(Map<String, String> urls) {
        this.urls = urls;
    }

    @Override
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
       this.url = url;
    }
}

Any thoughts about how can I achieve validation in above scenario. Properties will be set by spring container so I don't have control over it.

Aucun commentaire:

Enregistrer un commentaire