Validation input values

From FlexRule Wiki
Jump to: navigation, search

Introduction

There are some cases that you may need to pass some extra values to the validation rule and then use those extra passed values in the validation.

How to define

Validation rules support parameters and as the result you can define parameters by using Declaration and Define commands in the Validation section.

  1. <Validation name="PersonValidation">
  2.   <Declaration>
  3.     <Define name="min" direction="in"/>
  4.     <Define name="max" direction="in"/>
  5.   </Declaration>
  6.   <Logic name="test age">
  7.     <And>
  8.       <Check value="Age&gt;min && Age&lt;max" message="Age is not in the valid range." />
  9.     </And>
  10.   </Logic>
  11. </Validation>

How to pass values

To pass the values to the validation context, you can use the Validate overload of the ValidationEngine that accepts input values;

bool Validate(object defaultObject, string logicName, 
               params object[] inputParameters)

or

bool Validate(object defaultObject, string logicName, 
               IDictionary<string, object> inputParameters)

The first overload allows you to pass the input values just as a array of objects. However sometimes this can be hard specially if some of the input values are themselves an array of objects. In this case the second overload cab be used that allows input values to be passed as a dictionary of parameter`s name and its value.

If you use RuntimeEngine class you have similar methods:

RuntimeResult Run(RunParameter parameter, 
                     params object[] inputParameters)

or

RuntimeResult Run(RunParameter parameter, 
                     IDictionary<string, object> inputParameters)

Sample Code to Run the Rule

In this example we use manual plumbing of components to build and engine to execute validation rules and call Validate with the first overload.

  1. var model = LoadAdapterUtility.LoadNavigableSource("ValidationRuleWithMessage_Range_Validate.xml")
  2.                                           .GetElementModels(0).First();
  3. var validator = new Validator(model);
  4. var eng = new ValidatorEngine(validator);
  5. int min = 10, max = 30;
  6.  
  7. Person per = new Person();
  8. per.Age = 9;
  9.  
  10. bool result = eng.Validate(per, "test age",  min, max);
  11. Assert.IsFalse(result);

Parametrized Logic

It is possible that a logic require some input parameter. That main variable of a logic can be defined using variable setting of Logic but if there are some supporting data needs to be passed to the Logic then Declaration can be used to define the logic.

  1. <Logic name="Range" variable="$input">
  2.   <Declaration>
  3.     <Define name="$min" direction="in"/>
  4.     <Define name="$max" direction="in"/>
  5.   </Declaration>
  6.   <And>
  7.     <Check value="$input &gt; $min &amp;&amp; $input &lt; $max"
  8.            message="Input must be between '{0}' and '{1}'. But the provided input '{2}' was not.">
  9.       <MessageFormatter>
  10.         <Param value="$min"/>
  11.         <Param value="$max"/>
  12.         <Param value="$input"/>
  13.       </MessageFormatter>
  14.     </Check>
  15.   </And>
  16. </Logic>

When a Logic is parametrized, the caller of the logic MUST be another Logic and the values of the input parameters can be passed using Param command.

  1. <Logic name="test2">
  2.   <And>
  3.     <Validate logic="Range" value="Age">
  4.       <Param name="$min" value="10i"/>
  5.       <Param name="$max" value="12i"/>
  6.     </Validate>
  7.   </And>
  8. </Logic>

Make sure Params must provide name and value settings.

Next sections

In the next couple of articles we will cover different aspect of modelling 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