FunctionRegistryService
| Decorators: | @Injectable |
Registry service for custom functions and validators
This service maintains four separate registries:
Custom Functions - For conditional expressions (when/readonly/disabled)
- Used in: when conditions, readonly logic, disabled logic
- Return type: any value (typically boolean)
- Example:
isAdult: (ctx) => ctx.age >= 18
Custom Validators - For synchronous validation using Angular's public FieldContext API
- Used in: validators array on fields
- Return type: ValidationError | ValidationError[] | null
- Example:
noSpaces: (ctx) => ctx.value().includes(' ') ? { kind: 'noSpaces' } : null
Async Validators - For asynchronous validation (debouncing, database lookups, etc.)
- Used in: validators array on fields with type 'customAsync'
- Return type: Observable<ValidationError | ValidationError[] | null>
- Example:
checkUsername: (ctx) => userService.checkAvailability(ctx.value())
HTTP Validators - For HTTP-based validation with automatic request cancellation
- Used in: validators array on fields with type 'customHttp'
- Configuration object with url, method, mapResponse, etc.
- Example:
{ url: '/api/check', method: 'GET', mapResponse: ... }
Methods
clearAll() |
|---|
Clear everything (functions and all validators) |
Presentation |
Returnsvoid |
clearAsyncValidators() |
|---|
Clear all async validators |
Presentation |
Returnsvoid |
clearCustomFunctions() |
|---|
Clear all custom functions and their scopes |
Presentation |
Returnsvoid |
clearHttpValidators() |
|---|
Clear all HTTP validators |
Presentation |
Returnsvoid |
clearValidators() |
|---|
Clear all validators |
Presentation |
Returnsvoid |
getAsyncValidator() | ||||||
|---|---|---|---|---|---|---|
Get an async validator by name | ||||||
Presentation | ||||||
Parameters
Returns |
getCustomFunctions() |
|---|
Get all custom functions as an object |
Presentation |
ReturnsRecord<string, |
getCustomFunctionScope() | ||||||
|---|---|---|---|---|---|---|
Get the scope of a registered custom function. | ||||||
Presentation | ||||||
Parameters
Returns |
getHttpValidator() | ||||||
|---|---|---|---|---|---|---|
Get an HTTP validator by name | ||||||
Presentation | ||||||
Parameters
Returns |
getValidator() | ||||||
|---|---|---|---|---|---|---|
Get a validator by name | ||||||
Presentation | ||||||
Parameters
Returns |
isFieldScopedFunction() | ||||||
|---|---|---|---|---|---|---|
Check if a custom function is field-scoped (no cross-field dependencies). | ||||||
Presentation | ||||||
Parameters
Returnsboolean -true if the function is field-scoped, false if form-scoped or not registered |
registerAsyncValidator() | |||||||||
|---|---|---|---|---|---|---|---|---|---|
Register an async validator using Angular's public validateAsync() API Async validators return Observables for asynchronous validation logic. Use for debouncing, database lookups, or complex async business logic. | |||||||||
Presentation | |||||||||
Parameters
Returnsvoid | |||||||||
Example usageDebounced Username Check Async Cross-Field Validation |
registerCustomFunction() | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Register a custom function for conditional expressions Custom functions are used for control flow logic (when/readonly/disabled), NOT for validation. They return any value, typically boolean. | ||||||||||||
Presentation | ||||||||||||
Parameters
Returnsvoid | ||||||||||||
Example usage |
registerHttpValidator() | |||||||||
|---|---|---|---|---|---|---|---|---|---|
Register an HTTP validator configuration using Angular's public validateHttp() API HTTP validators provide optimized HTTP validation with automatic request cancellation, caching, and debouncing. Preferred over AsyncCustomValidator for HTTP requests. | |||||||||
Presentation | |||||||||
Parameters
Returnsvoid | |||||||||
Example usageUsername Availability Check POST Request with Body |
registerValidator() | |||||||||
|---|---|---|---|---|---|---|---|---|---|
Register a custom validator using Angular's public FieldContext API Validators receive the full FieldContext, allowing access to:
| |||||||||
Presentation | |||||||||
Parameters
Returnsvoid | |||||||||
Example usageSingle Field Validation Cross-Field Validation (Public API) Multiple Errors (Cross-Field Validation) |
setAsyncValidators() | ||||||
|---|---|---|---|---|---|---|
Set async validators from a config object Only updates validators if their references have changed | ||||||
Presentation | ||||||
Parameters
Returnsvoid |
setHttpValidators() | ||||||
|---|---|---|---|---|---|---|
Set HTTP validators from a config object Only updates validators if their references have changed | ||||||
Presentation | ||||||
Parameters
Returnsvoid |
setValidators() | ||||||
|---|---|---|---|---|---|---|
Set validators from a config object Only updates validators if their references have changed | ||||||
Presentation | ||||||
Parameters
Returnsvoid |
Example usage
// Register a custom function for expressions
registry.registerCustomFunction('isAdult', (ctx) => ctx.age >= 18);
// Use in field configuration
{
key: 'alcoholPreference',
when: { function: 'isAdult' }
}
// Register a custom validator
registry.registerValidator('noSpaces', (ctx) => {
const value = ctx.value();
return typeof value === 'string' && value.includes(' ')
? { kind: 'noSpaces' }
: null;
});
// Use in field configuration
{
key: 'username',
validators: [{ type: 'custom', functionName: 'noSpaces' }],
validationMessages: {
noSpaces: 'Spaces are not allowed'
}
}