Angular Business Rules & Validation Strategies

Angular Business Rules and Validation Strategies

Game Plan and Strategy

Type of Validation and Rules

  1. form input (synchronous)
  2. asynchronous validation of form input that may include an API operation
  3. business logic with business rules
  4. data validation before an API call


  • Quickly start using out-of-the-box rules that are already implemented.
  • Create custom rules that are either simple or composite.
  • Create rules that can be reused throughout the application. Code reuse eliminates copy/paste of common rules.
  • Use a single ValidationContext to add rules, execute rules, and evaluate the rule results.
  • Use a consistent pattern and mechanism to implement your business rules and data validation.
  • Integrate rules with Reactive Form custom validators (async or sync).
Rule Engine :: Composite Design Pattern Diagram

International JavaScript Conference (New York 2021)

2021 International JavaScript Conference:: New York:: Matt Vaughn

Why use a rule engine?

  • Provides a consistent way to implement business and validation rules and provides a consistent mechanism to retrieve the results.
  • AreEqual
  • AreNotEqual
  • GuidIsValid
  • IsFalse
  • IsTrue
  • IsNullOrUndefined
  • IsNotNullOrUndefined
  • Max
  • Min
  • Range
  • StringIsRegExMatch
  • StringIsNotNullEmptyRange
  • StringIsNullEmptyRange
  • You can create a reusable library of custom rules and use them in one or more applications.
  • Combine default with one or more custom rules to create a CompositeRule - a rule that contains other rules (ruleset).
  • Each rule has a Priority property to execute rule sets in a specified sequence.
  • Take advantage of Typescript classes to quickly create simple or composite (nested) rules using the API that is part of the framework.
  • Use the ValidationContext to simply add, execute, and retrieve rule results.
  • Code faster using Fluent API style syntax — be more productive.
  • Using the CompositeRule base class, you can create a rule that contains other rules of either simple or composite types. The rule execution algorithm manages the complexity - now you can create rules and reuse rules to match your business logic.


  • Add rules by calling the addRule() function.
  • Execute rules by calling the renderRules() function.
  • Retrieve the results (a list of RuleResult items) using the results public property.
  1. addRule(..): Add rules that you want to evaluate.
  2. renderRules(): Renders all rules added to the ValidationContext.
  3. Determine the state of the validation by using either: hasRuleViolations() or isValid(). Each returns a boolean value indicating the status of the validation context.
  4. Retrieve the ValidationContext.results which is a array of RuleResult items.


Adding Rules to the ValidationContext

  • Name: the name of the rule.
  • Message: the text to display if the rule fails.

Custom Rules

Rules with Rules (Composite)

Executing Rules


Evaluation Rule Results


  1. Simple
  2. Composite.
  • isValid: Use to indicate the status of the rule after evaluation.
  • message: Use to provide a message for failed rules.
  • name: Use to create a name that identifies the specified rule.
  • priority: Use to assign a numeric value to the rule. Rules are sorted by priority and executed in the same sort sequence.
  • result: The output of an executed rule. It contains the result and RulePolicy information.
  • isDisplayable: Use to indicate if the rule result is displayable to the caller. Default value is false. You must explicitly provide a [true] value for this when initializing a new rule.
  • renderType: Currently the only option is RenderType.EvaluateAllRules.
  • severity: Use to indicate the severity (Exception, Warning, or Information) if the rule evaluation is not valid.
  • source: Use to indicate the source or location of the rule.


export interface IRuleComponent {
execute(): RuleResult;


Simple Rules

Composite Rules





Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store