Hibernate Validator is the reference implementation of Jakarta Validation 3.1 providing annotation-based validation for JavaBeans and method parameters
Hibernate Validator provides an extensive collection of constraint annotations beyond the standard Jakarta Validation constraints. These include string validators, financial validators, product identifiers, time constraints, script-based validation, and country-specific validators.
All constraints in this document are from the org.hibernate.validator.constraints package unless otherwise noted. All constraints support null values as valid (use @NotNull to disallow null).
String length and Unicode normalization validators providing alternatives and extensions to standard @Size constraint.
package org.hibernate.validator.constraints;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
/**
* Validates string length is between min and max (inclusive).
* Hibernate-specific alternative to @Size for CharSequence.
*
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface Length {
/**
* Minimum length (inclusive). Default: 0
*/
int min() default 0;
/**
* Maximum length (inclusive). Default: Integer.MAX_VALUE
*/
int max() default Integer.MAX_VALUE;
String message() default "{org.hibernate.validator.constraints.Length.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
/**
* Validates code point length of character sequence with Unicode normalization support.
* Code points are Unicode characters, where some characters may be composed of multiple
* Java char values (e.g., emojis, surrogate pairs).
*
* Supported types: CharSequence
* @since 6.0.3
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface CodePointLength {
/**
* Minimum code point length (inclusive). Default: 0
*/
int min() default 0;
/**
* Maximum code point length (inclusive). Default: Integer.MAX_VALUE
*/
int max() default Integer.MAX_VALUE;
/**
* Unicode normalization strategy to apply before counting.
* Default: NONE (no normalization)
*/
NormalizationStrategy normalizationStrategy() default NormalizationStrategy.NONE;
String message() default "{org.hibernate.validator.constraints.CodePointLength.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
/**
* Unicode normalization strategies.
*/
enum NormalizationStrategy {
/** No normalization */
NONE,
/** Canonical Decomposition (NFD) */
NFD,
/** Canonical Decomposition followed by Canonical Composition (NFC) */
NFC,
/** Compatibility Decomposition (NFKD) */
NFKD,
/** Compatibility Decomposition followed by Canonical Composition (NFKC) */
NFKC
}
}
/**
* Validates that character sequence is normalized according to specified form.
*
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface Normalized {
/**
* Unicode normalization form to check against.
*/
java.text.Normalizer.Form form();
String message() default "{org.hibernate.validator.constraints.Normalized.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
/**
* Validates that character sequence is a valid UUID.
*
* Validation characteristics:
* - Consists only of numbers, hex characters and dashes
* - Has exact length of 36 characters
* - Number of hex digits in every group (8-4-4-4-12)
* - Configurable nil UUID, versions, variants, and letter case
*
* Supported types: CharSequence
* @since 8.0.0
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface UUID {
/**
* Allow empty strings. Default: false
*/
boolean allowEmpty() default false;
/**
* Allow nil UUID (00000000-0000-0000-0000-000000000000). Default: true
*/
boolean allowNil() default true;
/**
* Accepted UUID versions (1-15 corresponding to hex 1-f).
* Default: {1, 2, 3, 4, 5}
*/
int[] version() default {1, 2, 3, 4, 5};
/**
* Accepted UUID variants (0-2).
* Default: {0, 1, 2}
*/
int[] variant() default {0, 1, 2};
/**
* Required letter case for hex characters.
* Default: LOWER_CASE
*/
LetterCase letterCase() default LetterCase.LOWER_CASE;
String message() default "{org.hibernate.validator.constraints.UUID.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
/**
* Required letter case for UUID hex characters.
*/
enum LetterCase {
/** Only lower case is valid */
LOWER_CASE,
/** Only upper case is valid */
UPPER_CASE,
/** Any letter case is valid */
INSENSITIVE
}
}
/**
* Validates that collection contains only unique elements.
*
* Supported types: Collection
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface UniqueElements {
String message() default "{org.hibernate.validator.constraints.UniqueElements.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}Usage Example:
import org.hibernate.validator.constraints.*;
public class Article {
@Length(min = 10, max = 200)
private String title;
@CodePointLength(min = 1, max = 100, normalizationStrategy = CodePointLength.NormalizationStrategy.NFC)
private String displayName; // Handles emojis and special Unicode correctly
@Normalized(form = java.text.Normalizer.Form.NFC)
private String normalizedText;
@UUID(letterCase = UUID.LetterCase.LOWER_CASE, allowNil = false, version = {4})
private String articleId;
@UniqueElements
private List<String> tags;
}URL validation with protocol, host, and port constraints.
package org.hibernate.validator.constraints;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
import java.util.regex.Pattern;
/**
* Validates that annotated string is a valid URL.
* Uses java.net.URL constructor by default. Can be configured
* to use RegexpURLValidator for non-standard protocols.
*
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface URL {
/**
* Required protocol (e.g., "http", "https", "ftp").
* Empty string allows any protocol. Default: ""
*/
String protocol() default "";
/**
* Required host (e.g., "localhost", "example.com").
* Empty string allows any host. Default: ""
*/
String host() default "";
/**
* Required port. -1 allows any port. Default: -1
*/
int port() default -1;
/**
* Additional regex pattern the URL must match.
* Default: ".*" (matches everything)
*/
String regexp() default ".*";
/**
* Flags for regexp pattern matching.
* Default: {} (no flags)
*/
Pattern.Flag[] flags() default {};
String message() default "{org.hibernate.validator.constraints.URL.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}Usage Example:
import org.hibernate.validator.constraints.URL;
public class WebResource {
@URL(protocol = "https", host = "api.example.com")
private String apiEndpoint;
@URL(protocol = "http", regexp = ".*/api/.*")
private String restUrl;
@URL
private String homepage; // Any valid URL
}Numeric range validation combining min and max constraints.
package org.hibernate.validator.constraints;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
/**
* Validates that value is within specified range (inclusive).
* Composed of @Min and @Max constraints.
*
* Supported types: BigDecimal, BigInteger, CharSequence, byte, short, int, long, and wrappers
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface Range {
/**
* Minimum value (inclusive). Default: 0
*/
long min() default 0;
/**
* Maximum value (inclusive). Default: Long.MAX_VALUE
*/
long max() default Long.MAX_VALUE;
String message() default "{org.hibernate.validator.constraints.Range.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}Usage Example:
import org.hibernate.validator.constraints.Range;
public class Product {
@Range(min = 0, max = 100)
private int discountPercent;
@Range(min = 1, max = 999999)
private long productId;
}Currency validation for monetary amounts.
package org.hibernate.validator.constraints;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
/**
* Validates that MonetaryAmount has the right CurrencyUnit.
* Requires javax.money API on classpath.
*
* Supported types: javax.money.MonetaryAmount
* @since 5.4
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface Currency {
/**
* Currency codes that are accepted (e.g., "USD", "EUR", "GBP").
* Must match the currency of the validated MonetaryAmount.
*/
String[] value();
String message() default "{org.hibernate.validator.constraints.Currency.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}Usage Example:
import org.hibernate.validator.constraints.Currency;
import javax.money.MonetaryAmount;
import javax.money.Monetary;
public class Payment {
@Currency({"USD", "EUR"})
private MonetaryAmount price; // Only accepts USD or EUR
@Currency("GBP")
private MonetaryAmount britishPrice; // Only accepts GBP
}Credit card and cryptocurrency validation.
package org.hibernate.validator.constraints;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
/**
* Validates credit card number using Luhn algorithm (mod 10 checksum).
* Composed of @LuhnCheck constraint.
*
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface CreditCardNumber {
/**
* Ignore non-digit characters (spaces, dashes, etc.). Default: false
*/
boolean ignoreNonDigitCharacters() default false;
String message() default "{org.hibernate.validator.constraints.CreditCardNumber.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
/**
* Validates Luhn checksum algorithm (mod 10).
* Used for credit cards, IMEI numbers, etc.
*
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface LuhnCheck {
/**
* Start index for checksum calculation (inclusive). Default: 0
*/
int startIndex() default 0;
/**
* End index for checksum calculation (exclusive). Default: Integer.MAX_VALUE
*/
int endIndex() default Integer.MAX_VALUE;
/**
* Check digit index. -1 means last digit. Default: -1
*/
int checkDigitIndex() default -1;
/**
* Ignore non-digit characters. Default: false
*/
boolean ignoreNonDigitCharacters() default false;
String message() default "{org.hibernate.validator.constraints.LuhnCheck.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
/**
* Validates Bitcoin address format.
* P2PK, P2MS and Nested SegWit (P2SH-P2WPKH and P2SH-P2WSH) addresses are not valid.
*
* Supported types: CharSequence
* @since 9.0.0
*/
@Incubating
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface BitcoinAddress {
/**
* Bitcoin address types to validate.
* Default: ANY (accept any valid Bitcoin address type)
*/
BitcoinAddressType[] value() default BitcoinAddressType.ANY;
String message() default "{org.hibernate.validator.constraints.BitcoinAddress.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
/**
* Bitcoin address types.
*/
enum BitcoinAddressType {
/** Any valid Bitcoin address type */
ANY,
/** Pay to Public Key Hash (legacy address, starts with 1) */
P2PKH,
/** Pay to Script Hash (legacy address, starts with 3) */
P2SH,
/** Bech32 format (SegWit v0 for any witness program, starts with bc1) */
BECH32,
/** Pay to Witness Script Hash (SegWit v0, starts with bc1) */
P2WSH,
/** Pay to Witness Public Key Hash (SegWit v0, starts with bc1) */
P2WPKH,
/** Pay to Taproot (SegWit v1, starts with bc1p) */
P2TR
}
}Usage Example:
import org.hibernate.validator.constraints.*;
public class Payment {
@CreditCardNumber(ignoreNonDigitCharacters = true)
private String cardNumber; // Accepts "1234-5678-9012-3456"
@BitcoinAddress(BitcoinAddress.BitcoinAddressType.BECH32)
private String bitcoinWallet;
}Generic checksum validators for various algorithms.
package org.hibernate.validator.constraints;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
/**
* Validates generic Mod10 checksum algorithm.
* Used for various identification numbers.
*
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface Mod10Check {
/**
* Multiplier for Mod10 algorithm. Default: 3
*/
int multiplier() default 3;
/**
* Weight for Mod10 algorithm. Default: 1
*/
int weight() default 1;
/**
* Start index for checksum calculation. Default: 0
*/
int startIndex() default 0;
/**
* End index for checksum calculation. Default: Integer.MAX_VALUE
*/
int endIndex() default Integer.MAX_VALUE;
/**
* Check digit index. -1 means last digit. Default: -1
*/
int checkDigitIndex() default -1;
/**
* Ignore non-digit characters. Default: false
*/
boolean ignoreNonDigitCharacters() default false;
String message() default "{org.hibernate.validator.constraints.Mod10Check.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
/**
* Validates Mod11 checksum algorithm.
* Used for various identification numbers.
*
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface Mod11Check {
/**
* Threshold for Mod11 remainder. Default: Integer.MAX_VALUE
*/
int threshold() default Integer.MAX_VALUE;
/**
* Character to use when check digit is 10. Default: 'X'
*/
char treatCheck10As() default 'X';
/**
* Character to use when check digit is 11. Default: '0'
*/
char treatCheck11As() default '0';
/**
* Processing direction for checksum calculation.
* Default: RIGHT_TO_LEFT
*/
ProcessingDirection processingDirection() default ProcessingDirection.RIGHT_TO_LEFT;
/**
* Start index for checksum calculation. Default: 0
*/
int startIndex() default 0;
/**
* End index for checksum calculation. Default: Integer.MAX_VALUE
*/
int endIndex() default Integer.MAX_VALUE;
/**
* Check digit index. -1 means last digit. Default: -1
*/
int checkDigitIndex() default -1;
/**
* Ignore non-digit characters. Default: false
*/
boolean ignoreNonDigitCharacters() default false;
String message() default "{org.hibernate.validator.constraints.Mod11Check.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
/**
* Processing direction for Mod11 calculation.
*/
enum ProcessingDirection {
/** Process digits from left to right */
LEFT_TO_RIGHT,
/** Process digits from right to left */
RIGHT_TO_LEFT
}
}ISBN and EAN validators for book and product identification.
package org.hibernate.validator.constraints;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
/**
* Validates ISBN (International Standard Book Number).
*
* Supported types: CharSequence
* @since 6.0.6
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface ISBN {
/**
* ISBN format to validate.
* Default: ISBN_13
*/
Type type() default Type.ISBN_13;
String message() default "{org.hibernate.validator.constraints.ISBN.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
/**
* ISBN format types.
*/
enum Type {
/** ISBN-10 format (10 digits) */
ISBN_10,
/** ISBN-13 format (13 digits) */
ISBN_13,
/** Accept either ISBN-10 or ISBN-13 */
ANY
}
}
/**
* Validates EAN (European Article Number).
* Composed of @Mod10Check constraint.
*
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface EAN {
/**
* EAN format to validate.
* Default: EAN13
*/
Type type() default Type.EAN13;
String message() default "{org.hibernate.validator.constraints.EAN.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
/**
* EAN format types.
*/
enum Type {
/** EAN-13 format (13 digits) */
EAN13,
/** EAN-8 format (8 digits) */
EAN8
}
}Usage Example:
import org.hibernate.validator.constraints.*;
public class Book {
@ISBN(type = ISBN.Type.ISBN_13)
private String isbn;
@EAN(type = EAN.Type.EAN13)
private String barcode;
}Duration validation for java.time.Duration.
package org.hibernate.validator.constraints.time;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
/**
* Validates that duration is greater than or equal to specified minimum.
*
* Supported types: java.time.Duration
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface DurationMin {
/** Days component. Default: 0 */
long days() default 0;
/** Hours component. Default: 0 */
long hours() default 0;
/** Minutes component. Default: 0 */
long minutes() default 0;
/** Seconds component. Default: 0 */
long seconds() default 0;
/** Milliseconds component. Default: 0 */
long millis() default 0;
/** Nanoseconds component. Default: 0 */
long nanos() default 0;
/** Include boundary (minimum is valid). Default: true */
boolean inclusive() default true;
String message() default "{org.hibernate.validator.constraints.time.DurationMin.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
/**
* Validates that duration is less than or equal to specified maximum.
*
* Supported types: java.time.Duration
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface DurationMax {
/** Days component. Default: 0 */
long days() default 0;
/** Hours component. Default: 0 */
long hours() default 0;
/** Minutes component. Default: 0 */
long minutes() default 0;
/** Seconds component. Default: 0 */
long seconds() default 0;
/** Milliseconds component. Default: 0 */
long millis() default 0;
/** Nanoseconds component. Default: 0 */
long nanos() default 0;
/** Include boundary (maximum is valid). Default: true */
boolean inclusive() default true;
String message() default "{org.hibernate.validator.constraints.time.DurationMax.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}Usage Example:
import org.hibernate.validator.constraints.time.*;
import java.time.Duration;
public class Task {
@DurationMin(minutes = 5)
private Duration minimumDuration;
@DurationMax(hours = 8, inclusive = true)
private Duration maximumDuration;
@DurationMin(seconds = 30)
@DurationMax(hours = 2)
private Duration estimatedTime;
}Execute script expressions for complex validation logic.
package org.hibernate.validator.constraints;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
/**
* Class-level constraint that evaluates script expression against annotated element.
* Requires JSR 223 compatible script engine on classpath.
*
* Target: TYPE
* Supported script languages: javascript, groovy, etc.
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface ScriptAssert {
/**
* Script language name (e.g., "javascript", "groovy").
*/
String lang();
/**
* Script to execute. Must return Boolean.TRUE or Boolean.FALSE.
*/
String script();
/**
* Name for validated object in script context.
* Default: "_this"
*/
String alias() default "_this";
/**
* Property name to report violation on.
* Empty string reports on class level.
* Default: "" (class level)
* @since 5.4
*/
String reportOn() default "";
String message() default "{org.hibernate.validator.constraints.ScriptAssert.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
/**
* Cross-parameter constraint that evaluates script expression for method parameters.
* Requires JSR 223 compatible script engine on classpath.
*
* Target: CONSTRUCTOR, METHOD
*/
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface ParameterScriptAssert {
/**
* Script language name.
*/
String lang();
/**
* Script to execute. Must return Boolean.TRUE or Boolean.FALSE.
*/
String script();
/**
* Name for parameters array in script context.
* Default: "_args"
*/
String alias() default "_args";
String message() default "{org.hibernate.validator.constraints.ParameterScriptAssert.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}Usage Example:
import org.hibernate.validator.constraints.*;
@ScriptAssert(
lang = "javascript",
script = "_this.startDate.before(_this.endDate)",
reportOn = "endDate",
message = "End date must be after start date"
)
public class DateRange {
private Date startDate;
private Date endDate;
}
public class Calculator {
@ParameterScriptAssert(
lang = "javascript",
script = "_args[0] < _args[1]",
message = "First parameter must be less than second"
)
public int subtract(int a, int b) {
return b - a;
}
}Validators for country-specific identification numbers.
// Brazilian Validators
package org.hibernate.validator.constraints.br;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
/**
* Validates CPF (Cadastro de Pessoa Física) - Brazilian individual taxpayer number.
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface CPF {
String message() default "{org.hibernate.validator.constraints.br.CPF.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
/**
* Validates CNPJ (Cadastro Nacional da Pessoa Jurídica) - Brazilian company registry number.
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface CNPJ {
/**
* Format type of the CNPJ number to be considered as valid.
* Default: NUMERIC
*/
@Incubating
Format format() default Format.NUMERIC;
String message() default "{org.hibernate.validator.constraints.br.CNPJ.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
/**
* CNPJ format types.
*/
enum Format {
/**
* Older, original CNPJ format constructed from digits only.
* Format: dd.ddd.ddd/dddd-dd (where d represents a digit)
*/
NUMERIC,
/**
* New CNPJ format with digits and ASCII letters.
* Format: ss.sss.sss/ssss-dd (where s represents a digit or letter, d represents a digit)
* Adoption starts in January 2026.
*/
ALPHANUMERIC
}
}
/**
* Validates Título Eleitoral - Brazilian voter ID card number.
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface TituloEleitoral {
String message() default "{org.hibernate.validator.constraints.br.TituloEleitoral.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
// Polish Validators
package org.hibernate.validator.constraints.pl;
/**
* Validates NIP (Numer Identyfikacji Podatkowej) - Polish Tax Identification Number.
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface NIP {
String message() default "{org.hibernate.validator.constraints.pl.NIP.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
/**
* Validates PESEL - Polish national identification number.
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface PESEL {
String message() default "{org.hibernate.validator.constraints.pl.PESEL.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
/**
* Validates REGON (Rejestr Gospodarki Narodowej) - Polish business registry number.
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface REGON {
String message() default "{org.hibernate.validator.constraints.pl.REGON.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
// Russian Validators
package org.hibernate.validator.constraints.ru;
/**
* Validates INN (Идентификационный номер налогоплательщика) - Russian Taxpayer ID.
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface INN {
String message() default "{org.hibernate.validator.constraints.ru.INN.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
// Korean Validators
package org.hibernate.validator.constraints.kor;
/**
* Validates Korean Resident Registration Number.
* Supported types: CharSequence
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@interface KorRRN {
String message() default "{org.hibernate.validator.constraints.kor.KorRRN.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}Usage Example:
import org.hibernate.validator.constraints.br.*;
import org.hibernate.validator.constraints.pl.*;
import org.hibernate.validator.constraints.ru.*;
import org.hibernate.validator.constraints.kor.*;
public class InternationalPerson {
@CPF
private String brazilianCPF;
@CNPJ
private String brazilianCompanyNumber;
@NIP
private String polishTaxId;
@PESEL
private String polishNationalId;
@INN
private String russianTaxId;
@KorRRN
private String koreanResidentNumber;
}Control how composed constraints are evaluated.
package org.hibernate.validator.constraints;
import java.lang.annotation.*;
/**
* Defines boolean operator for composing constraint annotations.
* Applied to constraint annotation types.
*
* Target: ANNOTATION_TYPE
*/
@Target({ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface ConstraintComposition {
/**
* Composition type for constraint evaluation.
* Default: AND (all constraints must pass)
*/
CompositionType value() default CompositionType.AND;
}
/**
* Composition types for constraint evaluation.
*/
enum CompositionType {
/**
* All constraints must be satisfied (conjunction).
* This is the default behavior.
*/
AND,
/**
* At least one constraint must be satisfied (disjunction).
*/
OR,
/**
* All constraints must fail (negation of conjunction).
* Used for "not valid" scenarios.
*/
ALL_FALSE
}Usage Example:
import org.hibernate.validator.constraints.*;
import jakarta.validation.constraints.*;
// Custom composed constraint using OR logic
@ConstraintComposition(CompositionType.OR)
@Pattern(regexp = "[0-9]+")
@Size(min = 5)
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
public @interface NumericOrLong {
String message() default "Must be numeric OR at least 5 characters";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
public class Example {
@NumericOrLong // Valid if: all digits OR length >= 5
private String value;
}Install with Tessl CLI
npx tessl i tessl/maven-org-hibernate-validator--hibernate-validator