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

value-validation.mddocs/

Value Validation

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.

Capabilities

One Of Validation

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

Port Range Validation

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

Advanced Usage

Complex Configuration Validation

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

Case-Insensitive Environment Variables

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

Validation Groups

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

Integration with Bean Validation

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

Validation Behavior

OneOf Validator

  • Case Sensitivity: By default, matching is case-sensitive. Set ignoreCase = true for case-insensitive matching.
  • Whitespace Handling: By default, whitespace is significant. Set ignoreWhitespace = true to trim leading/trailing whitespace before comparison.
  • Null Values: null values are considered valid. Use @NotNull to reject null values.
  • Empty Strings: Empty strings are treated as regular values and must be explicitly included in the allowed values array if permitted.

PortRange Validator

  • Port 0 Special Case: Port 0 is always considered valid regardless of the min/max range, as it represents dynamic port allocation.
  • Range Validation: Validates that the port number falls within the specified min/max range (inclusive).
  • Null Values: null values are considered valid. Use @NotNull to reject null values.
  • Default Range: Without parameters, validates ports 1-65535 plus port 0.

Integration Notes

  • Both validators integrate seamlessly with Dropwizard's Jersey resource validation
  • Validators work with standard Bean Validation groups for conditional validation
  • Custom error messages support standard Bean Validation message interpolation
  • These validators are commonly used in Dropwizard configuration classes

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