◷ Reading Time: 4 minutes
Introduction
There are some cases in which you may need to pass some extra values to the validation rule and then use those additional passed values in the validation.
How to define
Validation rules support parameters and as a result, you can Define variables by using Declaration and Define commands in the Validation section.
<Validation name="PersonValidation">
<Declaration>
<Define name="min" direction="in"/>
<Define name="max" direction="in"/>
</Declaration>
<Logic name="test age">
<And>
<Check value="Age>min && Age<max" message="Age is not in the valid range." />
</And>
</Logic>
</Validation>
How to pass values
To pass 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 as just an array of objects. However, this can sometimes be hard, especially if some of the input values are themselves an array of objects. In this case, the second overload can be used that allows input values to be passed as a dictionary of the parameter`s name and its value.
If you use the 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 an engine to execute validation rules and call Validate with the first overload.
var model = LoadAdapterUtility.LoadNavigableSource("ValidationRuleWithMessage_Range_Validate.xml")
.GetElementModels(0).First();
var validator = new Validator(model);
var eng = new ValidatorEngine(validator);
int min = 10, max = 30;
Person per = new Person();
per.Age = 9;
bool result = eng.Validate(per, "test age", min, max);
Assert.IsFalse(result);
Parametrized Logic
It is possible that the logic requires some input parameters. That main variable of the logic can be defined using the variable setting of Logic, but if there are some supporting data that needs to be passed to the Logic then Declaration can be used to define the logic.
<Logic name="Range" variable="$input">
<Declaration>
<Define name="$min" direction="in"/>
<Define name="$max" direction="in"/>
</Declaration>
<And>
<Check value="$input > $min && $input < $max"
message="Input must be between '{0}' and '{1}'. But the provided input '{2}' was not.">
<MessageFormatter>
<Param value="$min"/>
<Param value="$max"/>
<Param value="$input"/>
</MessageFormatter>
</Check>
</And>
</Logic>
When a Logic is parameterized, the caller of the logic MUST be another Logic and the values of the input parameters can be passed using Param command.
<Logic name="test2">
<And>
<Validate logic="Range" value="Age">
<Param name="$min" value="10i"/>
<Param name="$max" value="12i"/>
</Validate>
</And>
</Logic>
Make sure Params provides name and value settings.
Next sections
In the next couple of articles, we will cover different aspects of modeling and executing the validation rules using Validation logic.
- Introduction to validation rules
- Validating hierarchy (Inheritance relation)
- Validating association (Aggregation, Composition)
- Validation rule execution and collecting results
- Pass extra input values to validation rules
- Extending validation conditions and actions
- How to apply rules under some conditions
- Referencing commonly used logic
- Sample for Order processing validation logic