@uplink-protocol/form-controller
Version:
Reactive multi-step form controller with dynamic validation and state management
54 lines (53 loc) • 2.01 kB
TypeScript
import { Field } from "../interfaces/field.interface";
/**
* Context provided to dynamic validators.
* Gives validators access to the current field, form data, and location information.
*/
export interface ValidatorContext {
/** The field configuration of the field being validated */
field: Field;
/** The entire form data flattened into a single object */
formData: Record<string, any>;
/** The ID of the step containing the field being validated */
stepId: string;
/** The ID of the field being validated */
fieldId: string;
}
/**
* Dynamic validator function signature.
*
* @param value The value to validate
* @param context The validation context with form state
* @returns true if valid, or an error message string if invalid
*/
export type DynamicValidator = (value: any, context: ValidatorContext) => boolean | string;
/**
* Register a new dynamic validator
* @param name Unique name for the validator
* @param validator The validator function
*/
export declare const registerValidator: (name: string, validator: DynamicValidator) => void;
/**
* Unregister a dynamic validator
* @param name Name of the validator to remove
*/
export declare const unregisterValidator: (name: string) => boolean;
/**
* Get a registered validator
* @param name Name of the validator
* @returns The validator function or undefined if not found
*/
export declare const getValidator: (name: string) => DynamicValidator | undefined;
/**
* Run a named validator
* @param name Name of the validator to run
* @param value The value to validate
* @param context Context information for validation
* @param params Optional parameters to pass to the validator
* @returns The result of the validation - true/string for valid/invalid
*/
export declare const runValidator: (name: string, value: any, context: ValidatorContext, params?: Record<string, any>) => boolean | string;
export declare const predefinedValidators: {
requiredIf: string;
equals: string;
};