CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-dropwizard--dropwizard-validation

Dropwizard Validation Support - provides enhanced validation capabilities for Dropwizard applications

Pending
Overview
Eval results
Files

data-size-validation.mddocs/

Data Size Validation

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.

Capabilities

Data Size Range Validation

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;
}

Minimum Data Size Validation

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;
}

Maximum Data Size Validation

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;
}

Advanced Usage

Combining Multiple Data Size Constraints

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;
}

Working with Different Data Size Units

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;
}

Validation Groups for Different Environments

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);

Supported Data Size Units

The following DataSizeUnit values are supported:

  • BYTES - Basic byte unit
  • KILOBYTES - 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)

Integration Notes

  • All data size validators work exclusively with io.dropwizard.util.DataSize objects
  • null values are considered valid (use @NotNull to reject null values)
  • Validators handle both decimal (KB, MB, GB) and binary (KiB, MiB, GiB) units
  • Data size constraints integrate seamlessly with Dropwizard configuration validation
  • Use @DataSizeRange for most cases; use individual @MinDataSize/@MaxDataSize when you need different units or validation messages

Install with Tessl CLI

npx tessl i tessl/maven-io-dropwizard--dropwizard-validation

docs

base-validation.md

data-size-validation.md

duration-validation.md

index.md

method-validation.md

self-validation.md

value-validation.md

tile.json