Dropwizard Validation Support - provides enhanced validation capabilities for Dropwizard applications
—
Specialized validation annotations and validators for Dropwizard Duration objects, supporting minimum, maximum, and range constraints with configurable time units. These annotations work specifically with io.dropwizard.util.Duration instances and provide flexible time-based validation.
Validates that a Duration falls within a specified range, combining minimum and maximum constraints.
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Constraint(validatedBy = {})
@MinDuration(0)
@MaxDuration(value = Long.MAX_VALUE, unit = TimeUnit.DAYS)
@ReportAsSingleViolation
public @interface DurationRange {
/**
* The minimum value of the range the validated Duration must be in.
*
* @return the minimum value
*/
long min() default 0;
/**
* The maximum value of the range the validated Duration must be in.
*
* @return the maximum value
*/
long max() default Long.MAX_VALUE;
/**
* The unit of the validated range.
*
* @return the TimeUnit
*/
TimeUnit unit() default TimeUnit.SECONDS;
/**
* The validation message for this constraint.
*
* @return the message
*/
String message() default "must be between {min} {unit} and {max} {unit}";
/**
* 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 {};
/**
* Defines several @DurationRange annotations on the same element.
*/
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Documented
@interface List {
DurationRange[] value();
}
}Usage Example:
import io.dropwizard.validation.DurationRange;
import io.dropwizard.util.Duration;
import java.util.concurrent.TimeUnit;
public class ServiceConfig {
// Connection timeout between 1 and 30 seconds
@DurationRange(min = 1, max = 30, unit = TimeUnit.SECONDS)
private Duration connectionTimeout;
// Retry delay between 100ms and 5 seconds
@DurationRange(min = 100, max = 5000, unit = TimeUnit.MILLISECONDS)
private Duration retryDelay;
// Session timeout between 1 minute and 24 hours
@DurationRange(min = 1, max = 24, unit = TimeUnit.HOURS)
private Duration sessionTimeout;
}Validates that a Duration meets or exceeds a minimum threshold.
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Constraint(validatedBy = MinDurationValidator.class)
public @interface MinDuration {
/**
* The annotation's value.
*
* @return value the element must be higher or equal to
*/
long value();
/**
* The unit of the annotation.
*
* @return unit of the value the element must be higher or equal to
*/
TimeUnit unit() default TimeUnit.SECONDS;
/**
* If the boundary value is inclusive or not.
*
* @return true if the validation is to allow values equal to value().
* False if the validation is to be exclusive.
* Defaults to true.
*/
boolean inclusive() default true;
/**
* The validation message for this constraint.
*
* @return the message
*/
String message() default "must be greater than ${inclusive == true ? 'or equal to ' : ''}{value} {unit}";
/**
* 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 Example:
import io.dropwizard.validation.MinDuration;
import io.dropwizard.util.Duration;
import java.util.concurrent.TimeUnit;
public class DatabaseConfig {
// Connection timeout must be at least 5 seconds
@MinDuration(value = 5, unit = TimeUnit.SECONDS)
private Duration connectionTimeout;
// Query timeout must be greater than 100ms (exclusive)
@MinDuration(value = 100, unit = TimeUnit.MILLISECONDS, inclusive = false)
private Duration queryTimeout;
// Health check interval must be at least 30 seconds
@MinDuration(value = 30, unit = TimeUnit.SECONDS)
private Duration healthCheckInterval;
}Validates that a Duration does not exceed a maximum threshold.
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Constraint(validatedBy = MaxDurationValidator.class)
public @interface MaxDuration {
/**
* The annotation's value.
*
* @return value the element must be less than or equal to
*/
long value();
/**
* The unit of the annotation.
*
* @return unit of the value the element must be higher or equal to
*/
TimeUnit unit() default TimeUnit.SECONDS;
/**
* If the boundary value is inclusive or not.
*
* @return true if the validation is to allow values equal to value().
* False if the validation is to be exclusive.
* Defaults to true.
*/
boolean inclusive() default true;
/**
* The validation message for this constraint.
*
* @return the message
*/
String message() default "must be less than ${inclusive == true ? 'or equal to ' : ''}{value} {unit}";
/**
* 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 Example:
import io.dropwizard.validation.MaxDuration;
import io.dropwizard.util.Duration;
import java.util.concurrent.TimeUnit;
public class RequestConfig {
// Request timeout cannot exceed 60 seconds
@MaxDuration(value = 60, unit = TimeUnit.SECONDS)
private Duration requestTimeout;
// Cache TTL must be less than 1 day (exclusive)
@MaxDuration(value = 1, unit = TimeUnit.DAYS, inclusive = false)
private Duration cacheTtl;
// Lock timeout cannot exceed 10 minutes
@MaxDuration(value = 10, unit = TimeUnit.MINUTES)
private Duration lockTimeout;
}import io.dropwizard.validation.*;
import io.dropwizard.util.Duration;
import java.util.concurrent.TimeUnit;
public class AdvancedConfig {
// Multiple constraints on the same field
@MinDuration(value = 1, unit = TimeUnit.SECONDS)
@MaxDuration(value = 300, unit = TimeUnit.SECONDS)
private Duration operationTimeout;
// Using DurationRange is equivalent and more concise
@DurationRange(min = 1, max = 300, unit = TimeUnit.SECONDS)
private Duration alternativeTimeout;
// Complex validation with custom message
@DurationRange(
min = 500,
max = 30000,
unit = TimeUnit.MILLISECONDS,
message = "Response timeout must be between 500ms and 30 seconds"
)
private Duration responseTimeout;
}import io.dropwizard.validation.DurationRange;
import io.dropwizard.util.Duration;
import java.util.concurrent.TimeUnit;
public interface Development {}
public interface Production {}
public class EnvironmentConfig {
// Different constraints for different environments
@DurationRange(min = 1, max = 10, unit = TimeUnit.SECONDS, groups = Development.class)
@DurationRange(min = 5, max = 60, unit = TimeUnit.SECONDS, groups = Production.class)
private Duration timeout;
}
// Validate with specific group
Validator validator = BaseValidator.newValidator();
Set<ConstraintViolation<EnvironmentConfig>> violations =
validator.validate(config, Production.class);io.dropwizard.util.Duration objectsnull values are considered valid (use @NotNull to reject null values)TimeUnit values (NANOSECONDS through DAYS)@DurationRange for most cases; use individual @MinDuration/@MaxDuration when you need different time units or inclusive/exclusive boundariesInstall with Tessl CLI
npx tessl i tessl/maven-io-dropwizard--dropwizard-validation