Validation input values

◷ Reading Time: 4 minutes


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">
    <Define name="min" direction="in"/>
    <Define name="max" direction="in"/>
  <Logic name="test age">
      <Check value="Age>min && Age<max" message="Age is not in the valid range." />

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)


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)


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")
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);

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">
    <Define name="$min" direction="in"/>
    <Define name="$max" direction="in"/>
    <Check value="$input > $min && $input < $max"
           message="Input must be between '{0}' and '{1}'. But the provided input '{2}' was not.">
        <Param value="$min"/>
        <Param value="$max"/>
        <Param value="$input"/>

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">
    <Validate logic="Range" value="Age">
      <Param name="$min" value="10i"/>
      <Param name="$max" value="12i"/>

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.

  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 April 6, 2020

Was this article helpful?

Related Articles