UNPKG

rune-form

Version:

Type-safe reactive form builder for Svelte 5

108 lines (107 loc) 3.83 kB
import type { z, ZodObject, ZodTypeAny } from 'zod'; import type { Paths, PathValue } from './types.js'; declare global { interface SymbolConstructor { readonly dispose: unique symbol; } } export interface Validator<T> { parse(data: unknown): T; safeParse(data: unknown): { success: true; data: T; } | { success: false; errors: Record<string, string[]>; }; safeParseAsync?(data: unknown): Promise<{ success: true; data: T; } | { success: false; errors: Record<string, string[]>; }>; resolveDefaults?(data: Partial<T>): T; getPaths?: () => string[]; getInputAttributes?: (path: string) => Record<string, unknown>; } export declare class RuneForm<T extends Record<string, unknown>> { private validator; private initialData; private _data; errors: Record<string, string[]>; customErrors: Partial<Record<string, string[]>>; touched: Record<string, boolean>; isValid: boolean; isValidating: boolean; private _errorCount; private _pathCache; private _fieldCache; private _validPaths; private _isInternalUpdate; private _proxyCache; private _methodCache; private static readonly _MUTATING_ARRAY_METHODS; private _pendingArrayValidation; private static readonly _MAX_PATH_CACHE_SIZE; private static readonly _MAX_FIELD_CACHE_SIZE; private static readonly _ARRAY_INDEX_PATTERN; private static readonly _ARRAY_FULL_PATTERN; constructor(validator: Validator<T>, initialData?: Partial<T>); private createReactiveData; private createReactiveArray; get data(): T; set data(value: T); static fromSchema<S extends ZodObject<Record<string, ZodTypeAny>>>(schema: S, initialData?: Partial<z.infer<S>>): RuneForm<z.core.output<S>>; getField<K extends Paths<T>>(path: K): { value: PathValue<T, K>; error: string | undefined; errors: string[]; touched: boolean; constraints: Record<string, unknown>; isValidating: boolean; }; private compilePath; private safePopulate; validateSchema(): Promise<void>; private _debouncedArrayValidation; markTouched(path: Paths<T>): void; markFieldAsPristine(path: Paths<T>): void; markAllTouched(): void; markAllAsPristine(): void; private _executeArrayOperation; reset(): void; setCustomError(path: Paths<T> | (string & {}), message: string): void; setCustomErrors(path: Paths<T> | (string & {}), messages: string[]): void; push<K extends ArrayPaths<T>>(path: K, value: PathValue<T, `${K}.${number}`>): void; swap<K extends ArrayPaths<T>>(path: K, i: number, j: number): void; splice<K extends ArrayPaths<T>>(path: K, start: number, deleteCount?: number, ...items: PathValue<T, `${K}.${number}`>[]): void; private _enhance; get enhance(): (el: HTMLFormElement) => { destroy(): void; }; private _createFieldObject; private _syncTouchedStateForArraySwap; private _syncTouchedStateForArrayRemoval; private _syncTouchedStateForArrayInsertion; private _ensureArrayElementsReactive; private _getTouchedKeysForArray; private _shiftArrayIndices; private _handleArrayMethodTouchedState; private _handleFieldChange; private _clearStaleFieldCacheEntries; /** * Cleanup method for Svelte's automatic resource disposal. * This method is called when the form instance goes out of scope. */ [Symbol.dispose](): void; /** * Manual cleanup method to free resources and prevent memory leaks. * Call this when you're done with the form instance. */ dispose(): void; } type ArrayPaths<T> = { [K in Paths<T>]: PathValue<T, K> extends Array<unknown> ? K : never; }[Paths<T>]; export {};