Dropwizard Validation Support - provides enhanced validation capabilities for Dropwizard applications
—
Core utilities for creating configured validators and formatting constraint violations. These classes provide the foundation for all validation operations in Dropwizard applications, integrating Hibernate Validator with Dropwizard-specific configurations and utilities.
Creates configured Validator instances with Dropwizard-specific value extractors and configurations.
public class BaseValidator {
/**
* Creates a new Validator based on newConfiguration().
*
* @return the new Validator instance
*/
public static Validator newValidator();
/**
* Creates a new HibernateValidatorConfiguration with the base custom unwrappers registered.
*
* @return the configured HibernateValidatorConfiguration
*/
public static HibernateValidatorConfiguration newConfiguration();
}Usage Example:
import io.dropwizard.validation.BaseValidator;
import javax.validation.Validator;
import javax.validation.ConstraintViolation;
import java.util.Set;
// Create a validator with Dropwizard's default configuration
Validator validator = BaseValidator.newValidator();
// Validate an object
MyConfig config = new MyConfig();
Set<ConstraintViolation<MyConfig>> violations = validator.validate(config);
// Or create a custom configuration
HibernateValidatorConfiguration configuration = BaseValidator.newConfiguration();
// Add custom configurations...
Validator customValidator = configuration.buildValidatorFactory().getValidator();Utilities for formatting constraint violations into human-readable messages, with special handling for ValidationMethod annotations.
public class ConstraintViolations {
/**
* Computes a string representation from a ConstraintViolation.
*
* @param v the constraint violation
* @return the formatted message
* @param <T> the generic type of the constraint violation
*/
public static <T> String format(ConstraintViolation<T> v);
/**
* Computes a set of formatted messages from the given typed set of ConstraintViolations.
*
* @param violations the constraint violations to format
* @return a new set containing the formatted messages
* @param <T> the generic type of the constraint violations
*/
public static <T> Collection<String> format(Set<ConstraintViolation<T>> violations);
/**
* Computes a set of formatted messages from the given untyped set of ConstraintViolations.
*
* @param violations the constraint violations to format
* @return a new set containing the formatted messages
*/
public static Collection<String> formatUntyped(Set<ConstraintViolation<?>> violations);
/**
* Copies a set of ConstraintViolations.
*
* @param violations the source set
* @return a new HashSet containing the violations from the source set
* @param <T> the generic type of the constraint violations
*/
public static <T> Set<ConstraintViolation<?>> copyOf(Set<ConstraintViolation<T>> violations);
}Usage Example:
import io.dropwizard.validation.ConstraintViolations;
import javax.validation.ConstraintViolation;
import java.util.Set;
import java.util.Collection;
// Format individual violation
ConstraintViolation<MyConfig> violation = // ... obtained from validation
String formatted = ConstraintViolations.format(violation);
// Returns: "propertyName violation message" or just "violation message" for ValidationMethod
// Format multiple violations
Set<ConstraintViolation<MyConfig>> violations = validator.validate(config);
Collection<String> messages = ConstraintViolations.format(violations);
// Returns a sorted collection of formatted violation messages
// Format untyped violations (e.g., from mixed validation)
Set<ConstraintViolation<?>> untypedViolations = // ... mixed violations
Collection<String> untypedMessages = ConstraintViolations.formatUntyped(untypedViolations);
// Copy violations for processing
Set<ConstraintViolation<?>> copy = ConstraintViolations.copyOf(violations);Utilities for escaping special characters in validation message parameters, ensuring proper message formatting.
public final class InterpolationHelper {
/**
* A constant representing the '{' character.
*/
public static final char BEGIN_TERM = '{';
/**
* A constant representing the '}' character.
*/
public static final char END_TERM = '}';
/**
* A constant representing the '$' character.
*/
public static final char EL_DESIGNATOR = '$';
/**
* A constant representing the '\' character.
*/
public static final char ESCAPE_CHARACTER = '\\';
/**
* Escapes a string with the ESCAPE_MESSAGE_PARAMETER_PATTERN.
*
* @param messageParameter the string to escape
* @return the escaped string or null, if the input was null
*/
@Nullable
public static String escapeMessageParameter(@Nullable String messageParameter);
}Usage Example:
import io.dropwizard.validation.InterpolationHelper;
import org.checkerframework.checker.nullness.qual.Nullable;
// Escape special characters in message parameters
String userInput = "Value with {special} $characters";
String escaped = InterpolationHelper.escapeMessageParameter(userInput);
// Returns: "Value with \\{special\\} \\$characters"
// Use in custom validation message parameters
Map<String, Object> messageParams = new HashMap<>();
messageParams.put("userValue", InterpolationHelper.escapeMessageParameter(userInput));BaseValidator.newValidator() automatically includes Guava Optional value extractorsConstraintViolations.format() provides special handling for @ValidationMethod annotationsInstall with Tessl CLI
npx tessl i tessl/maven-io-dropwizard--dropwizard-validation