1. Home
  2. FlexRule Designer
  3. Validation
  4. Referencing external logic

Referencing external logic

◷ Reading Time: 3 minutes


In the application, you may need to define some commonly used logic and then start reusing this. This separation can happen in two layers:

  1. Splitting into different Logic inside a Validation
  2. Splitting into Logic outside the Validation

Common logic

you may find Common logic useful for multiple Validations. For instance, IsNullOrEmpty, IsInRange,… You may implement this and reuse it inside a Validation or cross multiple Validations. If you want to use it across multiple Validations, you need to use the Include command to reference external logic to a Validation.

RuleSet and ModelContainer

In order to create a RuleSet, you simply load your ModelContainer and add it to a RuleSet collection using the AddRule method.

private RuleSet GetRuleSet()
    RuleSet rs = RuleSet.HierarchicalRuleSet();
    rs.AddModel("Common/Strings", LoadAdapterUtility.LoadModel("Common.IsNullOrEmpty.xml"));
    return rs;

Then you can use the RuleSet to create your Validator

private Validator GetValidator()
    RuleSet rs = GetRuleSet();
    // append new logic to ruleset
    rs.AddModel("rules/validations", LoadAdapterUtility.LoadModel("Conditional_requiredField_UsingCommonRules.xml"));
    // creating Validator directly, or we could use RuntimeEngine instead
    var validator = new Validator(rs, "ruleset://rules/validations/PersonValidation");
    return validator;

Direct file reference

Final Sample

<Logic name="NullOrEmpty" variable="$input">
    <Null value="$input"/>
    <Empty value="$input"/>

Referencing use RuleSet and ModelContainer

<Validation name="PersonValidation">
  <!-- Referencing external logic -->
  <Include source="ruleset://Common/Strings/NullOrEmpty"/>
  <Logic name="check email using False">
    <False message="Adult must have an email">
        <And processAll="true">
          <Check value= "Age>=18"/>
          <Validate logic="NullOrEmpty" value="Email"/>

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