public interface Validator
Validate plain Java objects that have been annotated with validation constraints.
// create a validator
final Validator validator = Validator.builder()
.setDefaultLocale(Locale.CANADA)
.addLocales(Locale.GERMAN)
.build();
// validate a pojo
Customer customer = ...;
validator.validate(customer);
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceFunction to build a ValidationAdapter from a Validation Contextstatic interfaceFunction to build a ValidationAdapter that needs Validator.static interfaceBuild the Validator instance adding ValidationAdapter, Factory or AdapterBuilder.static interfaceComponents register ValidationAdapters with the Validator.Builder -
Method Summary
Modifier and TypeMethodDescriptionstatic Validator.Builderbuilder()Return the Builder used to build the Validator.Validate the object returning the constraint violations.Validate the object returning the constraint violations.context()Return the validation context used to create adaptersvoidValidate the object using the default locale throwing ConstraintViolationException when there are constraint violations.voidValidate the object with a given locale throwing ConstraintViolationException when there are constraint violations.
-
Method Details
-
validate
Validate the object using the default locale throwing ConstraintViolationException when there are constraint violations.- Parameters:
any- The object to validategroups- The groups targeted for validation- Throws:
ConstraintViolationException- when there are constraint violations
-
validate
void validate(Object any, @Nullable Locale locale, @Nullable Class<?>... groups) throws ConstraintViolationException Validate the object with a given locale throwing ConstraintViolationException when there are constraint violations.If the locale is not one of the supported locales then the default locale will be used.
This is expected to be used when the Validator is configured to support multiple locales.
- Parameters:
any- The object to validatelocale- The locale to use for constraint messagesgroups- The groups targeted for validation- Throws:
ConstraintViolationException- when there are constraint violations
-
check
Validate the object returning the constraint violations.- Parameters:
any- The object to validategroups- The groups targeted for validation- Returns:
- The constraint violations
-
check
Validate the object returning the constraint violations.- Parameters:
any- The object to validatelocale- The locale to use for constraint messagesgroups- The groups targeted for validation- Returns:
- The constraint violations
-
context
ValidationContext context()Return the validation context used to create adapters -
builder
Return the Builder used to build the Validator.final Validator validator = Validator.builder() .setDefaultLocale(Locale.CANADA) .addLocales(Locale.GERMAN) .build();
-