// Validate string length (maximum 10 bytes)
DefaultDatabaseLengthValidationConfiguration config = new DefaultDatabaseLengthValidationConfiguration(Locale.GERMAN)
.withStringLengthConstraint(10);
DatabaseLengthValidation validation = DatabaseLengthValidation.with(config);
// Validate only the model object
validation.validateOnly(modelObject);
// Validate the model object and its children
validation.validateWithChildren(modelObject);
English Documentation
Relevance
Validation
Database Field Length Validation
The DatabaseLengthValidation
class provides functions for the generic validation of database field lengths. This validation makes it possible to check technical length constraints of the database independently from business-related value ranges. As a result, value ranges in the productcomponent can be reserved exclusively for business aspects, which makes product configuration clearer and more maintainable.
It is important to note that DatabaseLengthValidation
does not validate against constraints that may be defined through JPA annotations.
Motivation
-
Separation of technical and business validations
-
Avoidance of unclear value ranges in product configuration
-
Centralized definition of maximum database lengths instead of individual configuration per attribute
-
Explicit length validation for strings
Length Validation
Validation is configured using the DefaultDatabaseLengthValidationConfiguration
, which supports the builder pattern.
The configured validation can validate a model object directly or, using the visitor pattern, also the children of a model object. If the model does not implement IVisitorSupport
, GenericVisitorSupport
is used instead.
For example, a string length validation that ensures string values do not exceed the maximum byte length:
The actual byte length of a string is validated based on UTF-8 encoding (or another encoding specified in the configuration), not based on the number of characters. UTF-8 encodes Unicode characters variably in 1 to 4 bytes.
Numeric length validations can be configured either individually or for all numeric data types supported by Faktor-IPS. The following example configures all supported numeric attributes with the same definition of precision and scale:
// Validate all numeric attributes (precision 10, scale 2)
DefaultDatabaseLengthValidationConfiguration config = new DefaultDatabaseLengthValidationConfiguration(Locale.GERMAN)
.withNumericConstraintForAllNumbers(10, 2);
DatabaseLengthValidation validation = DatabaseLengthValidation.with(config);
// Validate only the model object
validation.validateOnly(modelObject);
// Validate the model object and its children
validation.validateWithChildren(modelObject);
The attributes to be validated can be selectively filtered using a BiPredicate<PolicyAttribute, IModelObject>
.
The following example validates only the attribute someString
on the Policy
object with a maximum length of 5; all other attributes are ignored:
DefaultDatabaseLengthValidationConfiguration config = new DefaultDatabaseLengthValidationConfiguration(Locale.GERMAN)
.withStringLengthConstraint(5)
.withAttributeFilter((a, m) -> Policy.PROPERTY_SOMESTRING.equals(a.getName()));
DatabaseLengthValidation validation = DatabaseLengthValidation.with(config);
// Validate only the model object
validation.validateOnly(modelObject);
Error Messages
If length restrictions are violated, the following message codes are generated:
-
DefaultDatabaseLengthValidationConfiguration.MSGCODE_STRING_TOO_LONG
– if the maximum string length is exceeded -
DefaultDatabaseLengthValidationConfiguration.MSGCODE_NUMBER_EXCEEDS_PRECISION
– if the precision (total number of digits) is exceeded -
DefaultDatabaseLengthValidationConfiguration.MSGCODE_NUMBER_EXCEEDS_SCALE
– if the scale (number of decimal places) is exceeded