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 usedMetadata 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 addedremovedResults: 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 changestarget: The HTML element associated with the bindingscope: The binding scope containing the binding contextrules: Optional property rules bound to this bindingpropertyInfo: 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:trueif validation passed,falseif it failedmessage: Error message (only present whenvalidisfalse)propertyName: Name of the property that was validatedobject: The object that was validatedrule: The individual rule that was evaluatedpropertyRule: The property rule containing this ruleisManual:trueif the result was added manually viaaddError()id: Auto-generated unique identifier for this result
ControllerValidateResult
The result returned by the validation controller's validate() method.
Structure:
Properties:
valid:trueif all validation passed,falseif any failedresults: Array of all validation resultsinstruction: 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 functionparser: Expression parser instance
Returns: A tuple containing:
The property name (string or number)
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:
Building Custom Rules: Implementing complex custom validation rules that need deep integration
Creating Plugins: Building validation extensions or integrations
Custom Presenters: Implementing custom error display logic
Analytics: Tracking validation events for analytics or monitoring
Testing: Writing advanced tests that need access to validation internals
When NOT to Use Internals
Avoid using internal APIs for:
Standard Validation: Use the fluent API instead
Simple Custom Rules: Use
.satisfies()or.satisfiesState()insteadBasic Error Display: Use the built-in presenters or
validation-errorsattributeConfiguration: 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
Defining Rules - Creating validation rules
Custom Rules - Building custom validation rules
Configuration and Customization - Customizing the validation system
Validation Result Presentation - Custom error presentation
Last updated
Was this helpful?