Building Validation Logic

From FlexRule Wiki
Jump to: navigation, search

Building Validation Logic

There are two ways of building a validation logic document:

  1. Using XML API
  2. Using builder class

XML API for Order processing rules

In this sample rule, we validate two processing rules both based on date/time:

  1. Order request must be received on the same date that order document is created.
  2. Order can be received within two days
  1. <Validation name="DesktopComputerValidation">
  2.   <Declaration>
  3.     <Using path="System.DateTime"/>
  4.   </Declaration>
  5.  
  6.   <Logic name="Order_Date_SameDay">
  7.     <Declaration>
  8.       <Define direction="local" name="n"/>
  9.     </Declaration>
  10.     <And>
  11.       <Var name="n" value="(Date - DateTime.Today)"/>
  12.       <Check value="n == #{0.0:0:0}#" message="Order requires a valid Date"/>
  13.     </And>
  14.   </Logic>
  15.  
  16.   <Logic name="Order_Date_Next2Days">
  17.     <Declaration>
  18.       <Define direction="local" name="n"/>
  19.     </Declaration>
  20.     <And>
  21.       <Var name="n" value="(Date - DateTime.Today)"/>
  22.       <Check value="(n ge #{0.0:0:0}#) and (n le #{1.0:0:0}#)" message="Order requires a valid Date"/>
  23.     </And>
  24.   </Logic>
  25.  
  26. </Validation>

Builder for Order processing rules

When building validation logic, you may want to avoid dealing with an XML API. The builder class lets you programmatically create validation logic using a fluent API without dealing with an XML API specification.

To find details of the API, please follow Validation Builder

Here is an example of using ValidationBuilder to create the same Validation logic as above:

  1. var model = new ValidationBuilder("DesktopComputerValidation")
  2. .DeclarationStart()
  3.     .Using("System.DateTime")
  4. .End()
  5. .LogicStart().SetName("Order_Date_Next2Days")
  6.     .DeclarationStart()
  7.         .Define("n", ValidationBuilder.Direction.Local)
  8.     .End()
  9.     .AndStart()
  10.         .Var("n", "(Date - DateTime.Today)")
  11.         .Check("(n >= #{0.0:0:0}#) and (n <= #{1.0:0:0}#)").SetMessage("Order requires a valid Date")
  12.     .End()
  13. .End()
  14.  
  15. .LogicStart().SetName("Order_Date_SameDay")
  16.     .DeclarationStart()
  17.         .Define("n", ValidationBuilder.Direction.Local)
  18.     .End()
  19.     .AndStart()
  20.         .Var("n", "(Date - DateTime.Today)")
  21.         .Check("n == #{0.0:0:0}#").SetMessage("Order requires a valid Date")
  22.     .End()
  23. .End()
  24.  
  25. // build model
  26. .Build();

Next sections

In the next couple of articles, we will cover different aspects of modeling and executing the validation rules using 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