Modeling validation rules

◷ 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.


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.

  1. Contact cannot be duplicated in the database
  2. Contact.Name, Contact.Email, For Person.Title and Company.BusinessNumber
    1. cannot be null or empty
    2. cannot start or end with space padding (if so, that has to be removed)
  3. Contact.Email must match: name@domain.ext
  4. Contact must have at least one Address
  5. Address.Line1+Address.Line2 cannot be duplicated
  6. For Address.Line1:
    1. cannot be null or empty
  7. For Address`s City, Country, Line1, Line2 and State:
    1. cannot start or end with space padding (if so, that has to be removed)
  8. Phone.Number (if exists)
    1. cannot be null or empty
    2. cannot start or end with space padding (if so, that has to be removed)
  9. Phone.Number+Phone.Type cannot be Duplicated
  10. 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.

  1. Introduction to validation rules
  2. Validating hierarchy (Inheritance relation)
  3. Validating association (Aggregation, Composition)
  4. Validation rule execution and collecting results
  5. Pass extra input values to validation rules
  6. Extending validation conditions and actions
  7. How to apply rules under some conditions
  8. Referencing commonly used logic
  9. Sample for Order processing validation logic
Updated on July 8, 2019

Was this article helpful?

Related Articles