- 15 Feb 2023
- 2 Minutes to read
- Print
- DarkLight
SDK: Custom Validation(Advanced)
- Updated on 15 Feb 2023
- 2 Minutes to read
- Print
- DarkLight
Version 7.x .NET Architecture Change
- Versions 7.0.1 - 7.1 require .NET Core 3.1
- Versions 7.2 - 7.9 require .NET 5
- Versions 7.10+ require .NET 6
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 the 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.
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.
- 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.
- Decorate the class with the ValidationRules attribute. This attribute alerts the Decisions framework that there are validation rules on this object.
- Decorate the property you want validation on with one of the following attributes:
- EmptyStringRule - This 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)); } }
- NullRule - This shows a validation issue if the property is null.
[NullRule(BreakLevel.Warning)] public DecisionsType Type { get => _type; set { _type = value; OnPropertyChanged(nameof(Type)); } }
- NumberAboveRule - This 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)); } }
- 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)); } }
- EmptyStringRule - This shows a validation issue if the property string value is empty (this check counts white space as empty).
Custom (method-based) Validation
The following describes how to do more complex validations using custom methods.
- In the class, inherit from and implement IValidationSource.
- Within this method, a developer can write code that evaluates any 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(); }