Interface EntityValidator
Entity validators enforce business rules and data integrity constraints beyond basic database constraints. They validate entity values before insert/update operations and can provide context-aware validation that considers the entity's current state and relationships.
Custom validators can be implemented for complex business logic:
// Custom validator for Customer entity
public class CustomerValidator implements EntityValidator {
@Override
public <T> void validate(Entity customer, Attribute<T> attribute) {
// Start with super.validate(), which performs null validation
EntityValidator.super.validate(customer, attribute);
// Validate email format
if (attribute.equals(Customer.EMAIL)) {
String email = customer.get(Customer.EMAIL);
// Email is non-null, since super.validate() checks that
if (!isValidEmail(email)) {
throw new ValidationException(Customer.EMAIL, email, "Invalid email format");
}
}
}
private static boolean isValidEmail(String email) {
return email.contains("@") && email.contains(".");
}
}
// Usage in domain definition
Customer.TYPE.define(
Customer.EMAIL.define()
.column(),
Customer.ACTIVE.define()
.column(),
Customer.BIRTH_DATE.define()
.column())
.validator(new CustomerValidator())
.build();
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PropertyValue
<Boolean> Specifies whether the default validator performs strict validation or not. -
Method Summary
Modifier and TypeMethodDescriptiondefault boolean
Returns true if the value based on the given attribute accepts a null value for the given entity, by default this method simply returns the nullable state of the underlying attribute.default boolean
strict()
Specifies whether this validator performs strict validation or not.default boolean
Returns true if the given entity contains only valid values according to this validator.default void
Checks if the values in the given entity are valid.default void
Checks if the value associated with the give attribute is valid, throws a ValidationException if notdefault boolean
validated
(Entity entity, AttributeDefinition<?> definition) Specifies whether the given attribute should be validated
-
Field Details
-
STRICT_VALIDATION
Specifies whether the default validator performs strict validation or not. By default, all non-read-only attribute values are validated if the entity is being inserted (as in, when it does not exist according toEntity.exists()
). If the entity exists and is being updated, only modified values are validated by default. With strict validation enabled all values are always validated, regardless of whether the entity exists or not- Value type: Boolean
- Default value: false
-
-
Method Details
-
nullable
Returns true if the value based on the given attribute accepts a null value for the given entity, by default this method simply returns the nullable state of the underlying attribute.// Context-aware nullable validation public class OrderValidator implements EntityValidator { @Override public boolean nullable(Entity order, Attribute<?> attribute) { // Normally nullable, but not for shipped orders if (attribute.equals(Order.TRACKING_NUMBER)) { String status = order.get(Order.STATUS); return !"SHIPPED".equals(status); // Tracking number required when shipped } // Use default nullable behavior for other attributes return EntityValidator.super.nullable(order, attribute); } } // Usage during validation Entity order = entities.entity(Order.TYPE) .with(Order.STATUS, "SHIPPED") .build(); // No tracking number boolean nullable = validator.nullable(order, Order.TRACKING_NUMBER); // false
- Parameters:
entity
- the entity being validatedattribute
- the attribute- Returns:
- true if the attribute accepts a null value
- See Also:
-
valid
Returns true if the given entity contains only valid values according to this validator.- Parameters:
entity
- the entity- Returns:
- true if the given entity contains only valid values
-
validate
Checks if the values in the given entity are valid. Note that by default, if the entity instance does not exist according toEntity.exists()
all values are validated, otherwise only modified values are validated. Use theSTRICT_VALIDATION
configuration value to change the default behaviour or overridestrict()
in order to configure the strictness of a specific instance.// Validation during entity lifecycle Entity customer = entities.entity(Customer.TYPE) .with(Customer.NAME, "John Doe") .with(Customer.EMAIL, "invalid-email") // Invalid format .with(Customer.ACTIVE, true) .build(); EntityValidator validator = entities.definition(Customer.TYPE).validator(); try { validator.validate(customer); // Validation passed connection.insert(customer); } catch (ValidationException e) { // Handle validation error System.err.println("Validation failed for attribute " + e.attribute() + ": " + e.getMessage()); } // Check if entity is valid without throwing exception if (validator.valid(customer)) { connection.insert(customer); } else { // Handle invalid entity }
- Parameters:
entity
- the entity- Throws:
ValidationException
- in case of an invalid value- See Also:
-
validate
Checks if the value associated with the give attribute is valid, throws a ValidationException if not- Parameters:
entity
- the entity to validateattribute
- the attribute the value is associated with- Throws:
ValidationException
- if the given value is not valid for the given attribute
-
validated
Specifies whether the given attribute should be validated- Parameters:
entity
- the entitydefinition
- the attribute definition- Returns:
- true if the value of the given attribute should be validated
- See Also:
-
strict
default boolean strict()Specifies whether this validator performs strict validation or not. By default, all non-read-only attribute values are validated if the entity is being inserted (as in, when it does not exist according to
Entity.exists()
). If the entity exists and is being updated, only modified values are validated by default.With strict validation enabled all values are always validated, regardless of whether the entity exists or not.
The default implementation simply returns the value of
STRICT_VALIDATION
, override to specify validator specific strictness.- Returns:
- true if strict validation is enabled
- See Also:
-