SDK: Custom Validation(Advanced)
  • 20 Aug 2024
  • 2 Minutes to read
  • Dark
    Light

SDK: Custom Validation(Advanced)

  • Dark
    Light

Article summary

Version 7.x .NET Architecture Change

Overview

Developers can cause validation messages to appear on the properties and inputs of steps at design time. These validations can evaluate only the value of a given property (e.g., show validation error if property X is empty) or they can be dependent on values in other properties (e.g., show validation error if property X is true AND property Y empty). Decisions includes built-in validation attributes that can be assigned to properties to check simple conditions like the value of a number or emptiness of a string. For more complex validation checks (and for validation checks of inputs as opposed to properties), a custom validation method is needed.

Custom codes are located here: C:\Program Files\Decisions\Decisions Server\modules\Decisions.Local\CoreServicesDlls
Decisions has a Public GitHub Repository with various SDK examples available.

Example

Simple Property Validation

This example will demonstrate the configuration for simple property validation.

  1. On the class, inherit and implement INotifyPropertyChanged. This will allow the validation messages to clear when the property value has changed to a valid value.
  2. Decorate the class with the ValidationRules attribute. This attribute alerts the Decisions framework that there are validation rules on this object.
  3. Decorate the property you want validation on with one of the following attributes:
    1. EmptyStringRule - Shows a validation issue if the property string value is empty (this check counts white space as empty).
      [EmptyStringRule]
              public string Name
              {
                  get => _name;
                  set
                  {
                      _name = value;
                      OnPropertyChanged(nameof(Name));
                  }
              }
      
    2. NullRule - Shows a validation issue if the property is null.
      [NullRule(BreakLevel.Warning)]
              public DecisionsType Type
              {
                  get => _type;
                  set
                  {
                      _type = value;
                      OnPropertyChanged(nameof(Type));
                  }
              }
      
    3. NumberAboveRule - Shows a validation issue if the property value is below a given number. Must be given at least one argument to define the minimum numeric value that this property is allowed to have.
      [NumberBelowRule(5)]
              public int SmallNumber
              {
                  get => _smallNumber;
                  set
                  {
                      _smallNumber = value;
                      OnPropertyChanged(nameof(SmallNumber));
                  }
              }
      
    4. NumberBelowRule - Shows a validation issue if the property value is above a given number. Must be given at least one argument to define the maximum numeric value that this property is allowed to have.
      [NumberAboveRule(5, "Number must be above 5")]
              public int LargeNumber
              {
                  get => _largeNumber;
                  set
                  {
                      _largeNumber = value;
                      OnPropertyChanged(nameof(LargeNumber));
                  }
              }
Each of these properties can be overridden to provide a custom error message and to change the break level. The break level can be specified as either Warning or Fatal. If no break level override is supplied the Fatal level is used by default. Warning break level causes a Yellow warning message and exclamation point icon to be shown while a Fatal break level causes a red message and exclamation point to be shown. Below is a screenshot showing how the Fatal and Warning level validations display in the designer.


Custom (method based) Validation

The following describes how to do more complex validations using custom methods.

  1. In the class, inherit from and implement  IValidationSource.
  2. Within this method, a developer can write code that evaluates any kind of conditions desired. Below is an example showing how this method would be written to evaluate the value of a date field based on the value of a Boolean field.
     public ValidationIssue[] GetValidationIssues()
            {
                List<ValidationIssue> issues = new List<ValidationIssue>();
                if (ForceFutureDate && MyDate < DateTime.Now)
                {
                    issues.Add(new ValidationIssue(this, "Date must be in the future", string.Empty, BreakLevel.Fatal, nameof(MyDate)));
                }
                return issues.ToArray();
            }



Was this article helpful?