Validating hierarchy

◷ Reading Time: 6 minutes

Assumptions

In this article, we discuss how an inheritance relation can be validated based on the concrete types of objects. Assume in your application you have the following model of a hierarchy: 

And assume you are going to design a single validation rule that contains multiple logic for your model.

<Validation name="ContactRule">
    ...
 
    <Logic name="Logic1">
    ...
    </Logic>
 
    ...
 
    <Logic name="Logic2">
    ...
    </Logic>
    ...
 
</Validation>

Application behaviour

When you have a inheritance hierarchy in the model, during the validation of your application you have two options:

  1. Manual validation
  2. Automatic validation

In the manual validation application, by knowing the type of the concrete type you can decide what logic of the validation rule needs to be invoked. On the other hand, in the automatic validation the decision of which logic is to be invoked is embedded into your validation rule. The validation finds the proper logic related to your concrete type and invokes it.

Externalizing this decision to the validation rule itself can help you to extend your model hierarchy without worrying about the validation and also enables you to extend the rule later on to support more types.

The good news is the automatic scenario still requires the same logic as the manual one. It has all the logic of the manual one, plus one more logic which routes the validation execution to the proper logic.

Rules

In this article we are going to define the following rules:

  1. 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, it has to be removed)
  2. Contact.Email must match: name@domain.ext

Dividing the logic based on type

The first step is creating two logics for your concrete types, one for each type that needs to be validated:

<Validation name="ContactRule">
  <Logic name="Person">
    <Or negate="true">
      <!--Check for name-->
      <Null value="Name"/>
      <Empty value="Name"/>
 
      <!--Check for email-->
      <Null value="Email"/>
      <Empty value="Email"/>
    </Or>
  </Logic>
 
  <Logic name="Company">
    <Or negate="true">
      <!--Check for name-->
      <Null value="Name"/>
      <Empty value="Name"/>
 
      <!--Check for email-->
      <Null value="Email"/>
      <Empty value="Email"/>
 
      <!--Check for business number-->
      <Null value="BusinessNumber"/>
      <Empty value="BusinessNumber"/>
    </Or>
  </Logic>
</Validation>

NullEmpty commands in the validation rules checks if the provided value on the command is null or empty. And Or commands combine the results as an ‘or’ Boolean operation. If they are null or empty then the values are not accepted. Therefore we set the negateproperty of Or to true. Which means it is a negative ‘or’ condition.

Logic reuse and enhancement

As you can see, the combination of checking for Null and Empty is repeated. In order to refactor this and reuse it, we can create a new logic named ValidString that checks both for neither being null nor empty.

<Logic name="ValidString" variable="str">
   <Or negate="true">
     <Null value="str" />
     <Empty value="str" />
   </Or>
</Logic>

As a result the validation rule becomes like this:

<Validation name="ContactRule">
  <Logic name="Person">
    <And>
      <Validate logic="ValidString" value="Name"/>
      <Validate logic="ValidString" value="Email"/>
      <Regex pattern="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" value="Email" message="Email is not valid" />
    </And>
  </Logic>
 
  <Logic name="Company">
    <And>
      <Validate logic="ValidString" value="Name"/>
      <Validate logic="ValidString" value="Email"/>
      <Validate logic="ValidString" value="BusinessNumber"/>
      <Regex pattern="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" value="Email" message="Email is not valid" />
    </And>
  </Logic>
 
  <Logic name="ValidString" variable="str">
    <Or negate="true">
      <Null value="str" />
      <Empty value="str" />
    </Or>
  </Logic>
</Validation>

As you may have noticed, you can call to a defined logic using the Validate command in validation logic.

Calling manual validation

At this point, the manual validation rule is ready to be called via your application.

// Loads rule engine
var engine = RuleEngine.FromXml(File.OpenRead(RuleFilePath));
 
// Prepare your data object to be validated
PersonContact contact = new PersonContact();
contact.Name = "aaaa";
contact.Family = "bbbb";
 
// now you are ready to validate the object
string logicNameToCall = "Person";
bool res = engine.Run(new RunParameter(logicNameToCall, contact));

As you can see in the highlighted lines, we specify the logic name to be called by using the proper overload of the Run method.

Automating validation for concrete type

Now the only part missing is the logic that routes the validation of Contact to its corresponding concrete types. We will be adding a logic akin to:

<Logic name="Contact">
  <Or>
    <!-- 
         adding multiple 'validate' commands here for all concrete types.
         If any of the concrete types passes then we do not need to continue processing the rest.
     -->
  </Or>
</Logic>

In order to make this happen, you can use When commands inside Validate.

<Logic name="Contact">
  <Or>
    <Validate logic="ValidatePersonContact">
      <When>
        <And>
          <Contains value="Family"/>
          <Contains value="Title"/>
        </And>
      </When>
    </Validate>
    <Validate logic="ValidateCompanyContact">
      <When>
        <And>
          <Contains value="BusinessNumber"/>
        </And>
      </When>
    </Validate>
  </Or>
</Logic>

In this example, ‘or’ using the Contains command to decide which type is which. The logic distinguishes different Contracts by their available properties in this case.

Final rule

The resulting rule looks similar to the following definition:

<Validation name="ContactRule">
  <Logic name="Contact">
    <Or>
      <Validate logic="ValidatePersonContact">
        <When>
          <And>
            <Contains value="Family"/>
            <Contains value="Title"/>
          </And>
        </When>
      </Validate>
      <Validate logic="ValidateCompanyContact">
        <When>
          <And>
            <Contains value="BusinessNumber"/>
          </And>
        </When>
      </Validate>
    </Or>
  </Logic>
 
  <Logic name="Person">
    <Or negate="true">
      <Validate logic="ValidString" value="Name"/>
      <Validate logic="ValidString" value="Email"/>
    </Or>
  </Logic>
 
  <Logic name="Company">
    <Or negate="true">
      <Validate logic="ValidString" value="Name"/>
      <Validate logic="ValidString" value="Email"/>
      <Validate logic="ValidString" value="BusinessNumber"/>
    </Or>
  </Logic>
 
  <Logic name="ValidString" variable="str">
    <And>
      <Null value="str" />
      <Empty value="str" />
    </And>
  </Logic>
</Validation>

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 April 6, 2020

Was this article helpful?

Related Articles