el-form-core
Version:
Framework-agnostic form validation engine - schema-first validation core for TypeScript applications. Supports Zod, Yup, Valibot and custom validators.
181 lines (172 loc) • 6.94 kB
TypeScript
import { z } from 'zod';
declare function parseZodErrors(error: z.ZodError): Record<string, string>;
declare function flattenObject(obj: any, prefix?: string): Record<string, any>;
declare function setNestedValue(obj: any, path: string, value: any): any;
declare function getNestedValue(obj: any, path: string): any;
declare function removeArrayItem(obj: any, path: string, index: number): any;
interface ValidationResult {
isValid: boolean;
errors: Record<string, string>;
}
interface ValidatorContext<T = any> {
value: T;
values: Record<string, any>;
fieldName: string;
}
type ValidatorFunction<T = any> = (context: ValidatorContext<T>) => string | undefined;
type AsyncValidatorFunction<T = any> = (context: ValidatorContext<T>) => Promise<string | undefined>;
type SchemaValidator = any;
interface ValidatorConfig {
onChange?: ValidatorFunction | SchemaValidator;
onBlur?: ValidatorFunction | SchemaValidator;
onSubmit?: ValidatorFunction | SchemaValidator;
onChangeAsync?: AsyncValidatorFunction | SchemaValidator;
onBlurAsync?: AsyncValidatorFunction | SchemaValidator;
onSubmitAsync?: AsyncValidatorFunction | SchemaValidator;
onChangeAsyncDebounceMs?: number;
onBlurAsyncDebounceMs?: number;
onSubmitAsyncDebounceMs?: number;
asyncDebounceMs?: number;
asyncAlways?: boolean;
}
interface FieldValidatorConfig extends ValidatorConfig {
fieldName: string;
}
interface FormValidatorConfig extends ValidatorConfig {
onSubmit?: ValidatorFunction | SchemaValidator | FormLevelValidator;
onSubmitAsync?: AsyncValidatorFunction | SchemaValidator | FormLevelValidator;
}
type FormLevelValidator = (context: {
value: Record<string, any>;
}) => string | undefined | {
form?: string;
fields?: Record<string, string>;
};
interface ValidatorEvent {
type: "onChange" | "onBlur" | "onSubmit";
isAsync: boolean;
fieldName?: string;
}
declare function isZodSchema(schema: any): boolean;
declare function isYupSchema(schema: any): boolean;
declare function isValibotSchema(schema: any): boolean;
declare function isArkTypeSchema(schema: any): boolean;
declare function isEffectSchema(schema: any): boolean;
declare function isValidatorFunction(validator: any): boolean;
declare function isStandardSchema(schema: any): boolean;
declare class SchemaAdapter {
static validate(schema: any, value: any, context?: ValidatorContext): ValidationResult;
static validateAsync(schema: any, value: any, context?: ValidatorContext): Promise<ValidationResult>;
private static validateFunction;
private static validateAsyncFunction;
private static validateStandardSchema;
private static validateZod;
private static validateYup;
private static validateValibot;
private static validateArkType;
private static validateEffect;
}
declare class ValidationEngine {
private debounceTimers;
/**
* Validates a single field using the provided validator configuration
*/
validateField(fieldName: string, value: any, values: Record<string, any>, config: ValidatorConfig, event: ValidatorEvent): Promise<ValidationResult>;
/**
* Validates the entire form using form-level validators
*/
validateForm(values: Record<string, any>, config: ValidatorConfig, event: ValidatorEvent): Promise<ValidationResult>;
/**
* Validates multiple fields at once
*/
validateFields(fieldNames: string[], values: Record<string, any>, fieldConfigs: Record<string, ValidatorConfig>, event: ValidatorEvent): Promise<ValidationResult>;
/**
* Clears debounce timer for a specific field
*/
clearDebounce(fieldName: string, eventType: string): void;
/**
* Clears all debounce timers
*/
clearAllDebounce(): void;
private validateAsync;
private validateWithDebounce;
private validateFormSync;
private validateFormAsync;
private executeFormAsyncValidation;
}
declare const validationEngine: ValidationEngine;
interface FileValidationOptions {
maxSize?: number;
minSize?: number;
maxFiles?: number;
minFiles?: number;
acceptedTypes?: string[];
acceptedExtensions?: string[];
}
/**
* Validate a single file against options
*/
declare function validateFile(file: File, options: FileValidationOptions): string | undefined;
/**
* Validate multiple files against options
*/
declare function validateFiles(files: FileList | File[], options: FileValidationOptions): string | undefined;
/**
* Create a file validator function for el-form's validation system
*/
declare function createFileValidator(options: FileValidationOptions): ValidatorFunction;
/**
* Preset file validators for common use cases
*/
declare const fileValidators: {
/**
* Image files (JPEG, PNG, GIF, WebP) up to 5MB
*/
image: ValidatorFunction;
/**
* Avatar images (JPEG, PNG) up to 2MB, single file only
*/
avatar: ValidatorFunction;
/**
* Document files (PDF, Word, Text) up to 10MB
*/
document: ValidatorFunction;
/**
* Image gallery (multiple images) up to 5MB each, max 10 files
*/
gallery: ValidatorFunction;
/**
* Video files (MP4, WebM, MOV) up to 50MB
*/
video: ValidatorFunction;
/**
* Audio files (MP3, WAV, OGG) up to 20MB
*/
audio: ValidatorFunction;
};
/**
* Helper to create custom file validator with specific options
*/
declare function fileValidator(options: FileValidationOptions): ValidatorFunction;
/**
* @deprecated Use SchemaAdapter.validate instead
* Validates form data using a zod schema (backward compatibility)
*/
declare function validateForm<T>(schema: any, data: T): {
success: boolean;
data?: T;
errors?: Record<string, string>;
};
/**
* Creates a validator config from a zod schema for easy migration
*/
declare function createValidatorFromSchema(schema: any, events?: ("onChange" | "onBlur" | "onSubmit")[]): any;
/**
* Utility to check if a validation result has errors
*/
declare function hasValidationErrors(result: ValidationResult): boolean;
/**
* Utility to get the first validation error message
*/
declare function getFirstValidationError(result: ValidationResult): string | undefined;
export { type AsyncValidatorFunction, type FieldValidatorConfig, type FileValidationOptions, type FormLevelValidator, type FormValidatorConfig, SchemaAdapter, type SchemaValidator, ValidationEngine, type ValidationResult, type ValidatorConfig, type ValidatorContext, type ValidatorEvent, type ValidatorFunction, createFileValidator, createValidatorFromSchema, fileValidator, fileValidators, flattenObject, getFirstValidationError, getNestedValue, hasValidationErrors, isArkTypeSchema, isEffectSchema, isStandardSchema, isValibotSchema, isValidatorFunction, isYupSchema, isZodSchema, parseZodErrors, removeArrayItem, setNestedValue, validateFile, validateFiles, validateForm, validationEngine };