◷ Reading Time: 3 minutes
Validation rules are intended to validate the structure of objects to make sure they are in a valid state before processing any requests. Using validation commands you can build a tree to decoratively model the validation logic of a hierarchy.
Take the example of a complex system model such as the one shown below:

As you can see in this model, different types of relationship exist:
- Inheritance
- Aggregation
- Composition
Validation logic must validate all of these relations, associations and values.
Rules
The system must maintain the consistency of the model by applying some business rules. These rules must be validated for different scenarios supported by the system.
- Contact cannot be duplicated in the database
- Contact.Name, Contact.Email, For Person.Title and Company.BusinessNumber
- cannot be null or empty
- cannot start or end with space padding (if so, that has to be removed)
- Contact.Email must match: name@domain.ext
- Contact must have at least one Address
- Address.Line1+Address.Line2 cannot be duplicated
- For Address.Line1:
- cannot be null or empty
- For Address`s City, Country, Line1, Line2 and State:
- cannot start or end with space padding (if so, that has to be removed)
- Phone.Number (if exists)
- cannot be null or empty
- cannot start or end with space padding (if so, that has to be removed)
- Phone.Number+Phone.Type cannot be Duplicated
- Phone.Number must match: +DDD (DD)DDDD-DDDD
How to model the validation rule
A validation rule may have a couple of logic types. For example, we can create logic to validate that a string is not null or empty. Another one may remove any padding of a string.
The validation rule can be modeled in the programming language or a high-level language. If it is defined in a high-level language, the advantage is it can be changed later without changing the application. However, while it may not seem necessary in this application, but for example the Phone.Number format in some countries may not be supported in the first rule version, or the string length constraint may need to be changed or added to some values later.
Next sections
In the next couple of articles, we will cover different aspects of modeling and executing the validation rules using Validation logic.
- Introduction to validation rules
- Validating hierarchy (Inheritance relation)
- Validating association (Aggregation, Composition)
- Validation rule execution and collecting results
- Pass extra input values to validation rules
- Extending validation conditions and actions
- How to apply rules under some conditions
- Referencing commonly used logic
- Sample for Order processing validation logic