Utilities & Internals

This guide covers the internal APIs and utilities of the Aurelia validation system. These are primarily intended for plugin authors, advanced users, and those building custom validation extensions.

Overview

The validation system exposes several utilities and internal APIs that can be useful when building:

  • Custom validation rules

  • Custom validation result presenters

  • Integration plugins

  • Advanced validation scenarios

Metadata Registry

validationRulesRegistrar

The validationRulesRegistrar is a metadata registry that stores and retrieves validation rules associated with objects and classes.

Purpose: Manages the association between objects/classes and their validation rules using metadata annotations.

API:

const validationRulesRegistrar = {
  // Store validation rules for a target
  set(target: IValidateable, rules: IPropertyRule[], tag?: string): void;

  // Retrieve validation rules for a target
  get(target: IValidateable, tag?: string): PropertyRule[] | undefined;

  // Remove validation rules from a target
  unset(target: IValidateable, tag?: string): void;

  // Check if validation rules are set for a target
  isValidationRulesSet(target: IValidateable): boolean;

  // Default rule set name
  defaultRuleSetName: '__default';
}

Example Usage:

Key Concepts:

  • Tags: Validation rules can be organized using tags, allowing multiple rule sets for the same object

  • Default Tag: When no tag is specified, the default tag '__default' is used

  • Metadata Storage: Rules are stored using Aurelia's metadata system, attached to objects or their constructors

  • Inheritance: Rules can be retrieved from an object instance or its constructor

Validation Event System

ValidationEvent

The ValidationEvent class describes validation events that are sent to subscribers when validation state changes.

Structure:

Properties:

  • kind: Type of event - either 'validate' (validation occurred) or 'reset' (validation was cleared)

  • addedResults: New validation results that were added

  • removedResults: Previous validation results that were removed

ValidationResultTarget

Links a validation result with the DOM elements it affects.

Structure:

Properties:

  • result: The validation result (contains valid/invalid state, message, etc.)

  • targets: Array of DOM elements associated with this validation result

Example: Custom Subscriber:

BindingInfo

Describes a binding that's registered with the validation controller. This class is used internally by the & validate binding behavior.

Structure:

Properties:

  • sourceObserver: Observer for the binding source that detects changes

  • target: The HTML element associated with the binding

  • scope: The binding scope containing the binding context

  • rules: Optional property rules bound to this binding

  • propertyInfo: Cached information about the property being validated

Use Case:

This class is primarily used internally, but can be useful when building custom validation triggers or integrating with the validation controller at a low level.

Validation Result Classes

ValidationResult

Represents the result of validating a single rule.

Structure:

Properties:

  • valid: true if validation passed, false if it failed

  • message: Error message (only present when valid is false)

  • propertyName: Name of the property that was validated

  • object: The object that was validated

  • rule: The individual rule that was evaluated

  • propertyRule: The property rule containing this rule

  • isManual: true if the result was added manually via addError()

  • id: Auto-generated unique identifier for this result

ControllerValidateResult

The result returned by the validation controller's validate() method.

Structure:

Properties:

  • valid: true if all validation passed, false if any failed

  • results: Array of all validation results

  • instruction: Optional instruction that was passed to validate

Example:

Utility Functions

parsePropertyName

Parses a property name string or accessor function into a property name and expression.

Signature:

Parameters:

  • property: A property name string (e.g., 'name', 'address.city') or a property accessor function

  • parser: Expression parser instance

Returns: A tuple containing:

  1. The property name (string or number)

  2. The parsed expression

Example:

Use Cases:

  • Parsing nested property paths

  • Converting property accessor functions to expressions

  • Building custom validation rule implementations

Property Accessor

PropertyAccessor

A type that represents a function for accessing properties. Used in the validation API to allow type-safe property selection.

Type Definition:

Example:

Interfaces for Plugin Authors

IValidationVisitor

Interface for implementing visitors that process validation rules. Used for serialization, transformation, or analysis of rules.

Example: Custom Rule Inspector:

ValidationResultsSubscriber

Interface for receiving validation event notifications.

Example: Analytics Tracker:

Best Practices

When to Use Internals

Use these internal APIs when:

  1. Building Custom Rules: Implementing complex custom validation rules that need deep integration

  2. Creating Plugins: Building validation extensions or integrations

  3. Custom Presenters: Implementing custom error display logic

  4. Analytics: Tracking validation events for analytics or monitoring

  5. Testing: Writing advanced tests that need access to validation internals

When NOT to Use Internals

Avoid using internal APIs for:

  1. Standard Validation: Use the fluent API instead

  2. Simple Custom Rules: Use .satisfies() or .satisfiesState() instead

  3. Basic Error Display: Use the built-in presenters or validation-errors attribute

  4. Configuration: Use the configuration API instead

Important Considerations

  • Stability: Internal APIs may change between minor versions

  • Documentation: Internal APIs have less documentation than public APIs

  • Support: Internal API usage may not be supported in community forums

  • Alternatives: Always check if a public API can accomplish your goal first

See Also

Last updated

Was this helpful?