Conditional validation

From FlexRule Wiki
Jump to: navigation, search

Introduction

When you define a rule model it is easy to implement rules that always require validation. However, in a complex system, rules do not need to always be applicable. Sometimes these need to be part of the validation only under specific circumstances. You may need to exclude or include them based on some other logic.

Rules

Assume we have a Person entity that has both Age and Email properties. We are going to write two validation rules:

  1. Person's email address must NOT be null or empty
  2. An adult person (age greater or equal to 18) must NOT provide an empty or null email address

These two validation rules are similar, the only difference is that we do not want to run the Email validation if the person is not an Adult (age>18)


Completed rule

  1. <Validation name="PersonValidation">
  2.  
  3.   <Declaration>
  4.     <Using path="System.DateTime"/>
  5.   </Declaration>
  6.   <!--
  7.     Rule: Adult (age>=18) MUST provide email address when registering
  8.     HasEmail     IsAdult       Result
  9.     T            T             T
  10.     T            F             T
  11.     F            T             F
  12.     F            F             T
  13.     -->
  14.  
  15.   <Logic name="check email using False">
  16.     <False message="Adult must have an email">
  17.       <When>
  18.         <And>
  19.           <Check value= "(DateTime.Today.Year - Birthday.Year) >= 18"/>
  20.           <Validate logic="EmptyOrNull" value="Email" />
  21.         </And>
  22.       </When>
  23.     </False>
  24.   </Logic>
  25.  
  26.   <Logic name="EmptyOrNull" variable="$input">
  27.     <Or>
  28.       <Null value="$input"/>
  29.       <Empty value="$input"/>
  30.     </Or>
  31.   </Logic>
  32.  
  33. </Validation>

Next sections

In the next couple of articles we will cover different aspects of modelling and executing the validation rules using a Validation engine.

  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