Dropwizard Validation Support - provides enhanced validation capabilities for Dropwizard applications
—
Validation annotations and validators for Dropwizard DataSize objects, enabling constraints on file sizes, memory limits, and other size-based configurations. These annotations work specifically with io.dropwizard.util.DataSize instances and support all standard data size units.
Validates that a DataSize falls within a specified range, combining minimum and maximum constraints.
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Constraint(validatedBy = {})
@MinDataSize(0)
@MaxDataSize(value = Long.MAX_VALUE, unit = DataSizeUnit.PEBIBYTES)
@ReportAsSingleViolation
public @interface DataSizeRange {
/**
* The minimum value of the range the validated DataSize must be in.
*
* @return the minimum value
*/
long min() default 0;
/**
* The maximum value of the range the validated DataSize must be in.
*
* @return the maximum value
*/
long max() default Long.MAX_VALUE;
/**
* The unit of the validated range.
*
* @return the DataSizeUnit
*/
DataSizeUnit unit() default DataSizeUnit.BYTES;
/**
* 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 @DataSizeRange annotations on the same element.
*/
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Documented
@interface List {
DataSizeRange[] value();
}
}Usage Example:
import io.dropwizard.validation.DataSizeRange;
import io.dropwizard.util.DataSize;
import io.dropwizard.util.DataSizeUnit;
public class FileUploadConfig {
// Upload size between 1KB and 10MB
@DataSizeRange(min = 1, max = 10, unit = DataSizeUnit.MEGABYTES)
private DataSize maxUploadSize;
// Memory buffer between 512 bytes and 64KB
@DataSizeRange(min = 512, max = 64, unit = DataSizeUnit.KILOBYTES)
private DataSize bufferSize;
// Cache size between 1MB and 1GB
@DataSizeRange(min = 1, max = 1024, unit = DataSizeUnit.MEGABYTES)
private DataSize cacheSize;
}Validates that a DataSize meets or exceeds a minimum threshold.
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Constraint(validatedBy = MinDataSizeValidator.class)
public @interface MinDataSize {
/**
* 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
*/
DataSizeUnit unit() default DataSizeUnit.BYTES;
/**
* The validation message for this constraint.
*
* @return the message
*/
String message() default "must be greater than 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.MinDataSize;
import io.dropwizard.util.DataSize;
import io.dropwizard.util.DataSizeUnit;
public class StorageConfig {
// Minimum disk space requirement
@MinDataSize(value = 100, unit = DataSizeUnit.MEGABYTES)
private DataSize minDiskSpace;
// Memory pool must be at least 32MB
@MinDataSize(value = 32, unit = DataSizeUnit.MEGABYTES)
private DataSize memoryPoolSize;
// Log file size must be at least 1KB
@MinDataSize(value = 1024, unit = DataSizeUnit.BYTES)
private DataSize minLogFileSize;
}Validates that a DataSize does not exceed a maximum threshold.
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Constraint(validatedBy = MaxDataSizeValidator.class)
public @interface MaxDataSize {
/**
* 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 less than or equal to
*/
DataSizeUnit unit() default DataSizeUnit.BYTES;
/**
* The validation message for this constraint.
*
* @return the message
*/
String message() default "must be less than 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.MaxDataSize;
import io.dropwizard.util.DataSize;
import io.dropwizard.util.DataSizeUnit;
public class LimitConfig {
// Maximum file size for processing
@MaxDataSize(value = 500, unit = DataSizeUnit.MEGABYTES)
private DataSize maxFileSize;
// Heap size cannot exceed 2GB
@MaxDataSize(value = 2, unit = DataSizeUnit.GIGABYTES)
private DataSize maxHeapSize;
// Request body size limit
@MaxDataSize(value = 10, unit = DataSizeUnit.MEGABYTES)
private DataSize maxRequestSize;
}import io.dropwizard.validation.*;
import io.dropwizard.util.DataSize;
import io.dropwizard.util.DataSizeUnit;
public class AdvancedDataConfig {
// Multiple constraints on the same field
@MinDataSize(value = 1, unit = DataSizeUnit.MEGABYTES)
@MaxDataSize(value = 100, unit = DataSizeUnit.MEGABYTES)
private DataSize workingMemory;
// Using DataSizeRange is equivalent and more concise
@DataSizeRange(min = 1, max = 100, unit = DataSizeUnit.MEGABYTES)
private DataSize alternativeMemory;
// Complex validation with custom message
@DataSizeRange(
min = 512,
max = 65536,
unit = DataSizeUnit.BYTES,
message = "Buffer size must be between 512 bytes and 64KB"
)
private DataSize bufferSize;
}import io.dropwizard.validation.DataSizeRange;
import io.dropwizard.util.DataSize;
import io.dropwizard.util.DataSizeUnit;
public class MultiscaleConfig {
// Small values in bytes
@DataSizeRange(min = 1024, max = 65536, unit = DataSizeUnit.BYTES)
private DataSize packetSize;
// Medium values in kilobytes
@DataSizeRange(min = 1, max = 1024, unit = DataSizeUnit.KILOBYTES)
private DataSize cacheLineSize;
// Large values in megabytes
@DataSizeRange(min = 1, max = 512, unit = DataSizeUnit.MEGABYTES)
private DataSize heapSize;
// Very large values in gigabytes
@DataSizeRange(min = 1, max = 100, unit = DataSizeUnit.GIGABYTES)
private DataSize diskSpace;
}import io.dropwizard.validation.DataSizeRange;
import io.dropwizard.util.DataSize;
import io.dropwizard.util.DataSizeUnit;
public interface Development {}
public interface Production {}
public class EnvironmentDataConfig {
// Different size limits for different environments
@DataSizeRange(min = 1, max = 10, unit = DataSizeUnit.MEGABYTES, groups = Development.class)
@DataSizeRange(min = 100, max = 1000, unit = DataSizeUnit.MEGABYTES, groups = Production.class)
private DataSize databaseConnectionPool;
}
// Validate with specific group
Validator validator = BaseValidator.newValidator();
Set<ConstraintViolation<EnvironmentDataConfig>> violations =
validator.validate(config, Production.class);The following DataSizeUnit values are supported:
BYTES - Basic byte unitKILOBYTES - 1,000 bytes (decimal)MEGABYTES - 1,000,000 bytes (decimal)GIGABYTES - 1,000,000,000 bytes (decimal)TERABYTES - 1,000,000,000,000 bytes (decimal)PETABYTES - 1,000,000,000,000,000 bytes (decimal)KIBIBYTES - 1,024 bytes (binary)MEBIBYTES - 1,048,576 bytes (binary)GIBIBYTES - 1,073,741,824 bytes (binary)TEBIBYTES - 1,099,511,627,776 bytes (binary)PEBIBYTES - 1,125,899,906,842,624 bytes (binary)io.dropwizard.util.DataSize objectsnull values are considered valid (use @NotNull to reject null values)@DataSizeRange for most cases; use individual @MinDataSize/@MaxDataSize when you need different units or validation messagesInstall with Tessl CLI
npx tessl i tessl/maven-io-dropwizard--dropwizard-validation