DropWizard non-trivial configuration validation
I have a DropWizard configuration class that has two properties.
At least one must be set.
That means, both are @Nullable
and I need validation on the whole object.
public class MessagingStreamConfiguration extends Configuration
{
@Nullable
private URL baseUrl;
@Nullable
private LinkedHashMap<String, URL> baseUrls;
}
This configuration class is a property of the whole application's configuration.
public class ClaConfiguration extends Configuration
{
@Valid
@JsonProperty("messagingStream")
private MessagingStreamConfiguration messagingStreamConfiguration;
I set up a javax.validation
for that:
/**
* Additional validation for non-trivial cases.
*/
private boolean isValid() {
return (this.getBaseUrl() == null
&& (this.getBaseUrls() == null || this.getBaseUrls().isEmpty()));
}
/**
* javax.validation way of validating the whole class.
*/
public static class MessagingStreamConfigurationValidator implements ConstraintValidator<MessagingStreamConfigurationValid, MessagingStreamConfiguration>
{
@Override
public void initialize(MessagingStreamConfigurationValid constraintAnnotation) {
}
@Override
public boolean isValid(MessagingStreamConfiguration conf, ConstraintValidatorContext context) {
return conf.isValid();
}
}
@Constraint(validatedBy = {MessagingStreamConfigurationValidator.class})
@Target({ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MessagingStreamConfigurationValid
{
String message() default "Neither baseUrl nor baseUrls is set. Set baseUrls.";
}
But the validation of the object does not happen. Only of it's individual fields.
The only relevant DropWizard tests I found, 2nd also only deals with fields.
The DW documentation doesn't talk about config validation much.
How can I make DropWizard validate the whole object?
java configuration dropwizard bean-validation javax.validation
add a comment |
I have a DropWizard configuration class that has two properties.
At least one must be set.
That means, both are @Nullable
and I need validation on the whole object.
public class MessagingStreamConfiguration extends Configuration
{
@Nullable
private URL baseUrl;
@Nullable
private LinkedHashMap<String, URL> baseUrls;
}
This configuration class is a property of the whole application's configuration.
public class ClaConfiguration extends Configuration
{
@Valid
@JsonProperty("messagingStream")
private MessagingStreamConfiguration messagingStreamConfiguration;
I set up a javax.validation
for that:
/**
* Additional validation for non-trivial cases.
*/
private boolean isValid() {
return (this.getBaseUrl() == null
&& (this.getBaseUrls() == null || this.getBaseUrls().isEmpty()));
}
/**
* javax.validation way of validating the whole class.
*/
public static class MessagingStreamConfigurationValidator implements ConstraintValidator<MessagingStreamConfigurationValid, MessagingStreamConfiguration>
{
@Override
public void initialize(MessagingStreamConfigurationValid constraintAnnotation) {
}
@Override
public boolean isValid(MessagingStreamConfiguration conf, ConstraintValidatorContext context) {
return conf.isValid();
}
}
@Constraint(validatedBy = {MessagingStreamConfigurationValidator.class})
@Target({ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MessagingStreamConfigurationValid
{
String message() default "Neither baseUrl nor baseUrls is set. Set baseUrls.";
}
But the validation of the object does not happen. Only of it's individual fields.
The only relevant DropWizard tests I found, 2nd also only deals with fields.
The DW documentation doesn't talk about config validation much.
How can I make DropWizard validate the whole object?
java configuration dropwizard bean-validation javax.validation
add a comment |
I have a DropWizard configuration class that has two properties.
At least one must be set.
That means, both are @Nullable
and I need validation on the whole object.
public class MessagingStreamConfiguration extends Configuration
{
@Nullable
private URL baseUrl;
@Nullable
private LinkedHashMap<String, URL> baseUrls;
}
This configuration class is a property of the whole application's configuration.
public class ClaConfiguration extends Configuration
{
@Valid
@JsonProperty("messagingStream")
private MessagingStreamConfiguration messagingStreamConfiguration;
I set up a javax.validation
for that:
/**
* Additional validation for non-trivial cases.
*/
private boolean isValid() {
return (this.getBaseUrl() == null
&& (this.getBaseUrls() == null || this.getBaseUrls().isEmpty()));
}
/**
* javax.validation way of validating the whole class.
*/
public static class MessagingStreamConfigurationValidator implements ConstraintValidator<MessagingStreamConfigurationValid, MessagingStreamConfiguration>
{
@Override
public void initialize(MessagingStreamConfigurationValid constraintAnnotation) {
}
@Override
public boolean isValid(MessagingStreamConfiguration conf, ConstraintValidatorContext context) {
return conf.isValid();
}
}
@Constraint(validatedBy = {MessagingStreamConfigurationValidator.class})
@Target({ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MessagingStreamConfigurationValid
{
String message() default "Neither baseUrl nor baseUrls is set. Set baseUrls.";
}
But the validation of the object does not happen. Only of it's individual fields.
The only relevant DropWizard tests I found, 2nd also only deals with fields.
The DW documentation doesn't talk about config validation much.
How can I make DropWizard validate the whole object?
java configuration dropwizard bean-validation javax.validation
I have a DropWizard configuration class that has two properties.
At least one must be set.
That means, both are @Nullable
and I need validation on the whole object.
public class MessagingStreamConfiguration extends Configuration
{
@Nullable
private URL baseUrl;
@Nullable
private LinkedHashMap<String, URL> baseUrls;
}
This configuration class is a property of the whole application's configuration.
public class ClaConfiguration extends Configuration
{
@Valid
@JsonProperty("messagingStream")
private MessagingStreamConfiguration messagingStreamConfiguration;
I set up a javax.validation
for that:
/**
* Additional validation for non-trivial cases.
*/
private boolean isValid() {
return (this.getBaseUrl() == null
&& (this.getBaseUrls() == null || this.getBaseUrls().isEmpty()));
}
/**
* javax.validation way of validating the whole class.
*/
public static class MessagingStreamConfigurationValidator implements ConstraintValidator<MessagingStreamConfigurationValid, MessagingStreamConfiguration>
{
@Override
public void initialize(MessagingStreamConfigurationValid constraintAnnotation) {
}
@Override
public boolean isValid(MessagingStreamConfiguration conf, ConstraintValidatorContext context) {
return conf.isValid();
}
}
@Constraint(validatedBy = {MessagingStreamConfigurationValidator.class})
@Target({ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MessagingStreamConfigurationValid
{
String message() default "Neither baseUrl nor baseUrls is set. Set baseUrls.";
}
But the validation of the object does not happen. Only of it's individual fields.
The only relevant DropWizard tests I found, 2nd also only deals with fields.
The DW documentation doesn't talk about config validation much.
How can I make DropWizard validate the whole object?
java configuration dropwizard bean-validation javax.validation
java configuration dropwizard bean-validation javax.validation
edited Nov 14 '18 at 18:17
Ondra Žižka
asked Nov 14 '18 at 18:08
Ondra ŽižkaOndra Žižka
20k24136210
20k24136210
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have two options:
- annotate the class to be validated (
ClaConfiguration
) with your custom annotation (MessagingStreamConfigurationValid
) - in your annotation, target
ElementType.FIELD
, and annotate your field with your custom annotation instead of@Valid
Please note that you need two more attributes in your annotation to get it to work:
Class<?> groups() default { };
Class<? extends Payload> payload() default { };
Interesting, I thought the groups and payload were example-specific. Now I see they are actually needed. Will try.
– Ondra Žižka
Nov 19 '18 at 15:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306372%2fdropwizard-non-trivial-configuration-validation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have two options:
- annotate the class to be validated (
ClaConfiguration
) with your custom annotation (MessagingStreamConfigurationValid
) - in your annotation, target
ElementType.FIELD
, and annotate your field with your custom annotation instead of@Valid
Please note that you need two more attributes in your annotation to get it to work:
Class<?> groups() default { };
Class<? extends Payload> payload() default { };
Interesting, I thought the groups and payload were example-specific. Now I see they are actually needed. Will try.
– Ondra Žižka
Nov 19 '18 at 15:47
add a comment |
You have two options:
- annotate the class to be validated (
ClaConfiguration
) with your custom annotation (MessagingStreamConfigurationValid
) - in your annotation, target
ElementType.FIELD
, and annotate your field with your custom annotation instead of@Valid
Please note that you need two more attributes in your annotation to get it to work:
Class<?> groups() default { };
Class<? extends Payload> payload() default { };
Interesting, I thought the groups and payload were example-specific. Now I see they are actually needed. Will try.
– Ondra Žižka
Nov 19 '18 at 15:47
add a comment |
You have two options:
- annotate the class to be validated (
ClaConfiguration
) with your custom annotation (MessagingStreamConfigurationValid
) - in your annotation, target
ElementType.FIELD
, and annotate your field with your custom annotation instead of@Valid
Please note that you need two more attributes in your annotation to get it to work:
Class<?> groups() default { };
Class<? extends Payload> payload() default { };
You have two options:
- annotate the class to be validated (
ClaConfiguration
) with your custom annotation (MessagingStreamConfigurationValid
) - in your annotation, target
ElementType.FIELD
, and annotate your field with your custom annotation instead of@Valid
Please note that you need two more attributes in your annotation to get it to work:
Class<?> groups() default { };
Class<? extends Payload> payload() default { };
answered Nov 16 '18 at 10:23
vin59vin59
586
586
Interesting, I thought the groups and payload were example-specific. Now I see they are actually needed. Will try.
– Ondra Žižka
Nov 19 '18 at 15:47
add a comment |
Interesting, I thought the groups and payload were example-specific. Now I see they are actually needed. Will try.
– Ondra Žižka
Nov 19 '18 at 15:47
Interesting, I thought the groups and payload were example-specific. Now I see they are actually needed. Will try.
– Ondra Žižka
Nov 19 '18 at 15:47
Interesting, I thought the groups and payload were example-specific. Now I see they are actually needed. Will try.
– Ondra Žižka
Nov 19 '18 at 15:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306372%2fdropwizard-non-trivial-configuration-validation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown