import { default as React } from 'react'; import { BaseSchema, Infer } from './schema-validator'; /** * Field validation rule function */ export type FieldRule = (value: T, formData: Record, fieldName: string) => string | undefined | Promise; /** * Field validation configuration */ export interface FieldConfig { /** Validation rules */ rules: FieldRule[]; /** Async validation debounce delay (ms) */ debounce?: number; /** Dependencies for cross-field validation */ dependsOn?: string[]; /** Custom transform before validation */ transform?: (value: unknown) => T; /** When to validate */ validateOn?: ('change' | 'blur' | 'submit')[]; } /** * Validation error (same as FieldError) */ export type ValidationError = FieldError; /** * Form validation configuration */ export type FormConfig> = { [K in keyof T]?: FieldRule[] | FieldConfig; }; /** * Field error state */ export interface FieldError { message: string; rule?: string; } /** * Field state */ export interface FieldState { /** Current value */ value: unknown; /** Field errors */ errors: FieldError[]; /** Field has been touched (focused then blurred) */ touched: boolean; /** Field value has changed from initial */ dirty: boolean; /** Field is currently being validated */ validating: boolean; /** Field is valid (no errors and has been validated) */ valid: boolean; } /** * Form state */ export interface FormState> { /** Field states */ fields: { [K in keyof T]: FieldState; }; /** Form-level errors */ errors: FieldError[]; /** Any async validation in progress */ validating: boolean; /** Form has been touched */ touched: boolean; /** Form values have changed */ dirty: boolean; /** Form is valid */ valid: boolean; /** Form has been submitted */ submitted: boolean; /** Submit attempt count */ submitCount: number; } /** * Validation mode */ export type ValidationMode = 'onChange' | 'onBlur' | 'onSubmit' | 'all'; /** * Form validator options */ export interface FormValidatorOptions { /** Primary validation mode */ mode?: ValidationMode; /** Revalidate mode after first submit */ revalidateMode?: 'onChange' | 'onBlur'; /** Validate all fields on mount */ validateOnMount?: boolean; /** Show errors only after first submit */ showErrorsAfterSubmit?: boolean; /** Debounce time for async validation */ debounceMs?: number; /** Abort validation on unmount */ abortOnUnmount?: boolean; } /** * Form validator instance */ export interface FormValidator> { /** Validate a single field */ validateField: (field: keyof T, value: unknown, formData: T) => Promise; /** Validate all fields */ validateAll: (formData: T) => Promise<{ valid: boolean; errors: Record; }>; /** Get field configuration */ getFieldConfig: (field: keyof T) => FieldConfig | undefined; /** Check if field has async rules */ hasAsyncRules: (field: keyof T) => boolean; } /** * Create a required field rule */ export declare function required(message?: string): FieldRule; /** * Create a minimum length rule */ export declare function minLength(length: number, message?: string): FieldRule; /** * Create a maximum length rule */ export declare function maxLength(length: number, message?: string): FieldRule; /** * Create a minimum value rule */ export declare function min(minValue: number, message?: string): FieldRule; /** * Create a maximum value rule */ export declare function max(maxValue: number, message?: string): FieldRule; /** * Create a pattern matching rule */ export declare function pattern(regex: RegExp, message?: string): FieldRule; /** * Create an email validation rule */ export declare function email(message?: string): FieldRule; /** * Create a URL validation rule */ export declare function url(message?: string): FieldRule; /** * Create a matches (confirm) rule */ export declare function matches(fieldName: string, message?: string): FieldRule; /** * Create a custom validation rule */ export declare function custom(validator: (value: T, formData: Record) => boolean | string, message?: string): FieldRule; /** * Create an async validation rule */ export declare function asyncRule(validator: (value: T, formData: Record) => Promise, options?: { debounce?: number; }): FieldRule & { _async: true; _debounce?: number; }; /** * Create a schema validation rule */ export declare function schema>(schemaInstance: T, message?: string): FieldRule>; /** * Create a conditional rule */ export declare function when(condition: (formData: Record) => boolean, rules: FieldRule[]): FieldRule; /** * Create a form validator */ export declare function createFormValidator>(config: FormConfig): FormValidator; /** * Form validation React hook */ export declare function useFormValidation>(validator: FormValidator, initialValues: T, options?: FormValidatorOptions): { values: T; errors: Record; touched: Record; dirty: Record; isValid: boolean; isValidating: boolean; isSubmitting: boolean; isSubmitted: boolean; submitCount: number; formErrors: ValidationError[]; setValue: (field: keyof T, value: unknown, options?: { shouldValidate?: boolean; }) => void; setValues: (values: Partial, options?: { shouldValidate?: boolean; }) => void; setTouched: (field: keyof T, touched: boolean) => void; setError: (field: keyof T, error: string) => void; clearError: (field: keyof T) => void; clearAllErrors: () => void; reset: (values?: Partial) => void; validateField: (field: keyof T, value?: unknown) => Promise; validateAll: () => Promise; getValues: () => T; handleBlur: (field: keyof T) => void; handleSubmit: (onSubmit: (values: T) => void | Promise) => (e?: React.FormEvent) => Promise; registerField: (field: keyof T) => { value: T[keyof T]; onChange: (value: unknown) => void; onBlur: () => void; error: string | undefined; touched: boolean; dirty: boolean; }; }; /** * Hook for individual field validation */ export declare function useField(name: string, rules: FieldRule[], options?: { defaultValue?: T; validateOnChange?: boolean; validateOnBlur?: boolean; debounceMs?: number; }): { value: T | undefined; setValue: (val: T | undefined) => void; error: string | undefined; errors: FieldError[]; touched: boolean; setTouched: (val: boolean) => void; validating: boolean; onChange: (val: T | undefined) => void; onBlur: () => void; validate: (val?: T) => Promise; reset: (newValue?: T) => void; }; export declare const rules: { readonly required: typeof required; readonly minLength: typeof minLength; readonly maxLength: typeof maxLength; readonly min: typeof min; readonly max: typeof max; readonly pattern: typeof pattern; readonly email: typeof email; readonly url: typeof url; readonly matches: typeof matches; readonly custom: typeof custom; readonly async: typeof asyncRule; readonly schema: typeof schema; readonly when: typeof when; };