Dropwizard Validation Support - provides enhanced validation capabilities for Dropwizard applications
—
Annotations for validating string values against allowed sets and numeric values against port ranges, with support for case-insensitive and whitespace-tolerant matching. These validators provide flexible constraints for common validation scenarios.
Validates that an object's string representation is one of a predefined set of allowed values, with optional case and whitespace handling. Works with any object type by converting it to string.
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Constraint(validatedBy = OneOfValidator.class)
public @interface OneOf {
/**
* The set of valid values.
*
* @return an array containing the valid string values
*/
String[] value();
/**
* Whether to ignore case.
*
* @return if the case should be ignored
*/
boolean ignoreCase() default false;
/**
* Whether to ignore leading and trailing whitespace.
*
* @return if leading and trailing whitespaces should be ignored
*/
boolean ignoreWhitespace() default false;
/**
* The validation message for this constraint.
*
* @return the message
*/
String message() default "must be one of {value}";
/**
* The groups the constraint belongs to.
*
* @return an array of classes representing the groups
*/
Class<?>[] groups() default {};
/**
* The payloads of this constraint.
*
* @return the array of payload classes
*/
Class<? extends Payload>[] payload() default {};
}Usage Examples:
import io.dropwizard.validation.OneOf;
public class ApplicationConfig {
// Basic enum-like validation
@OneOf({"development", "staging", "production"})
private String environment;
// Case-insensitive matching
@OneOf(value = {"GET", "POST", "PUT", "DELETE"}, ignoreCase = true)
private String httpMethod;
// Ignore whitespace and case
@OneOf(
value = {"debug", "info", "warn", "error"},
ignoreCase = true,
ignoreWhitespace = true
)
private String logLevel;
// Custom error message
@OneOf(
value = {"mysql", "postgresql", "h2"},
message = "Database type must be one of: mysql, postgresql, h2"
)
private String databaseType;
}Validates that an integer value represents a valid port number within a specified range, with special handling for port 0 (dynamic allocation).
@Target({METHOD, FIELD, ANNOTATION_TYPE, TYPE_USE})
@Retention(RUNTIME)
@Constraint(validatedBy = PortRangeValidator.class)
public @interface PortRange {
/**
* The minimum value of the port range the validated int must be in.
*
* @return the minimum value
*/
int min() default 1;
/**
* The maximum value of the port range the validated int must be in.
*
* @return the maximum value
*/
int max() default 65535;
/**
* The validation message for this constraint.
*
* @return the message
*/
String message() default "{org.hibernate.validator.constraints.Range.message}";
/**
* The groups the constraint belongs to.
*
* @return an array of classes representing the groups
*/
Class<?>[] groups() default {};
/**
* The payloads of this constraint.
*
* @return the array of payload classes
*/
Class<? extends Payload>[] payload() default {};
}Usage Examples:
import io.dropwizard.validation.PortRange;
public class ServerConfig {
// Default port range (1-65535), allows 0 for dynamic allocation
@PortRange
private int serverPort;
// Custom port range for admin interface
@PortRange(min = 8080, max = 9999)
private int adminPort;
// High-numbered ports only
@PortRange(min = 49152, max = 65535)
private int ephemeralPort;
// Well-known ports range
@PortRange(min = 1, max = 1023)
private int systemPort;
}import io.dropwizard.validation.OneOf;
import io.dropwizard.validation.PortRange;
public class DatabaseConfig {
@OneOf({"mysql", "postgresql", "oracle", "sqlserver"})
private String databaseType;
@PortRange(min = 1024, max = 65535)
private int databasePort;
@OneOf(value = {"READ_UNCOMMITTED", "READ_COMMITTED", "REPEATABLE_READ", "SERIALIZABLE"})
private String isolationLevel;
@OneOf(value = {"true", "false"}, message = "SSL must be 'true' or 'false'")
private String sslEnabled;
}import io.dropwizard.validation.OneOf;
public class EnvironmentConfig {
// Accept various capitalizations of boolean values
@OneOf(
value = {"true", "false", "yes", "no", "on", "off", "1", "0"},
ignoreCase = true,
message = "Must be a valid boolean: true/false, yes/no, on/off, or 1/0"
)
private String featureEnabled;
// Accept common environment names with flexible formatting
@OneOf(
value = {"dev", "development", "test", "testing", "stage", "staging", "prod", "production"},
ignoreCase = true,
ignoreWhitespace = true
)
private String deploymentEnvironment;
}import io.dropwizard.validation.OneOf;
import io.dropwizard.validation.PortRange;
public interface Development {}
public interface Production {}
public class GroupedConfig {
// Different allowed values for different environments
@OneOf(value = {"h2", "hsqldb"}, groups = Development.class)
@OneOf(value = {"mysql", "postgresql"}, groups = Production.class)
private String databaseType;
// Different port ranges for different environments
@PortRange(min = 8080, max = 8090, groups = Development.class)
@PortRange(min = 80, max = 443, groups = Production.class)
private int httpPort;
}import io.dropwizard.validation.OneOf;
import io.dropwizard.validation.PortRange;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.NotBlank;
public class ValidatedConfig {
@NotNull
@NotBlank
@OneOf({"http", "https"})
private String protocol;
@NotNull
@PortRange(min = 1, max = 65535)
private Integer port;
// Combining with other validation annotations
@NotBlank
@OneOf(value = {"json", "xml", "yaml"}, ignoreCase = true)
private String responseFormat;
}ignoreCase = true for case-insensitive matching.ignoreWhitespace = true to trim leading/trailing whitespace before comparison.null values are considered valid. Use @NotNull to reject null values.null values are considered valid. Use @NotNull to reject null values.Install with Tessl CLI
npx tessl i tessl/maven-io-dropwizard--dropwizard-validation