Building Validation Logic

Print Friendly, PDF & Email

◷ Reading Time: 3 minutes

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
<Validation name="DesktopComputerValidation">
  <Declaration>
    <Using path="System.DateTime"/>
  </Declaration>
 
  <Logic name="Order_Date_SameDay">
    <Declaration>
      <Define direction="local" name="n"/>
    </Declaration>
    <And>
      <Var name="n" value="(Date - DateTime.Today)"/>
      <Check value="n == #{0.0:0:0}#" message="Order requires a valid Date"/>
    </And>
  </Logic>
 
  <Logic name="Order_Date_Next2Days">
    <Declaration>
      <Define direction="local" name="n"/>
    </Declaration>
    <And>
      <Var name="n" value="(Date - DateTime.Today)"/>
      <Check value="(n ge #{0.0:0:0}#) and (n le #{1.0:0:0}#)" message="Order requires a valid Date"/>
    </And>
  </Logic>
 
</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:

var model = new ValidationBuilder("DesktopComputerValidation")
.DeclarationStart()
    .Using("System.DateTime")
.End()
.LogicStart().SetName("Order_Date_Next2Days")
    .DeclarationStart()
        .Define("n", ValidationBuilder.Direction.Local)
    .End()
    .AndStart()
        .Var("n", "(Date - DateTime.Today)")
        .Check("(n >= #{0.0:0:0}#) and (n <= #{1.0:0:0}#)").SetMessage("Order requires a valid Date")
    .End()
.End()
 
.LogicStart().SetName("Order_Date_SameDay")
    .DeclarationStart()
        .Define("n", ValidationBuilder.Direction.Local)
    .End()
    .AndStart()
        .Var("n", "(Date - DateTime.Today)")
        .Check("n == #{0.0:0:0}#").SetMessage("Order requires a valid Date")
    .End()
.End()
 
// build model
.Build();

Previous sections

In the previous articles, we covered 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