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
class CustomerValidator implements EntityValidator {
@Override
public void validate(Entity customer, Attribute<?> attribute) throws AttributeValidationException {
// 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 AttributeValidationException(Customer.EMAIL, email, "Invalid email format");
}
}
}
private static boolean isValidEmail(String email) {
return email.contains("@") && email.contains(".");
}
}
// Usage in domain definition
Customer.TYPE.as()
.attributes(
Customer.ID.as()
.primaryKey(),
Customer.EMAIL.as()
.column()
.caption("Email")
.nullable(false))
.validator(new CustomerValidator())
.build();
The validator of an entity definition must be serializable where clients fetch the entity definitions
from a server, see EntityDefinition.Builder.
- 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 booleanReturns 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 booleanstrict()Specifies whether this validator performs strict validation or not.default booleanReturns true if the given entity contains only valid values according to this validator.default voidChecks if the values in the given entity are valid.default voidChecks if the value associated with the given attribute is valid, throws anAttributeValidationExceptionif notdefault booleanSpecifies whether the given attribute should be validatedReturns a warning for the value associated with the given attribute, a soft constraint.
-
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 class CustomerValidator implements EntityValidator { @Override public boolean nullable(Entity customer, Attribute<?> attribute) { // Normally nullable, but not for customers in the USA if (attribute.equals(Customer.STATE)) { return !"USA".equals(customer.get(Customer.COUNTRY)); } // Use default nullable behavior for other attributes return EntityValidator.super.nullable(customer, attribute); } } // Usage during validation EntityValidator validator = new CustomerValidator(); Entity customer = entities.entity(Customer.TYPE) .with(Customer.COUNTRY, "USA") .build(); // No state boolean nullable = validator.nullable(customer, Customer.STATE); // false- Parameters:
entity- the entity being validatedattribute- the attribute- Returns:
- true if the attribute is non-value based or 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_VALIDATIONconfiguration 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.FIRSTNAME, "John") .with(Customer.LASTNAME, "Doe") .with(Customer.EMAIL, "invalid-email") // Invalid format .build(); EntityValidator validator = entities.definition(Customer.TYPE).validator(); try { validator.validate(customer); // Validation passed connection.insert(customer); } catch (EntityValidationException e) { // Handle validation error System.err.println("Validation failed " + 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:
EntityValidationException- in case of one or more invalid values- See Also:
-
validate
Checks if the value associated with the given attribute is valid, throws anAttributeValidationExceptionif not- Parameters:
entity- the entity to validateattribute- the attribute the value is associated with- Throws:
AttributeValidationException- if the given value is not valid for the given attribute
-
warning
Returns a warning for the value associated with the given attribute, a soft constraint.
Where
validate(Entity, Attribute)rejects a value the entity may not carry, this reports one it may carry but probably should not, an unusually large measurement or an out-of-season record for example. A warning blocks nothing: insert and update proceed andvalid(Entity)stays true, the warning only shown to whoever is looking at the value.Called whenever the entity is revalidated, which is on every value change, so keep implementations cheap.
class TrackValidator implements EntityValidator { private static final int HOUR_MS = 3_600_000; @Override public Optional<String> warning(Entity track, Attribute<?> attribute) { if (attribute.equals(Track.MILLISECONDS)) { Integer milliseconds = track.get(Track.MILLISECONDS); if (milliseconds != null && milliseconds > HOUR_MS) { return Optional.of("Unusually long for a track - is this a whole album?"); } } return EntityValidator.super.warning(track, attribute); } }- Parameters:
entity- the entity being validatedattribute- the attribute- Returns:
- a warning for the value, or an empty
Optionalif there is nothing to say
-
validated
-
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:
-