UNPKG

claritykit-svelte

Version:

A comprehensive Svelte component library focused on accessibility, ADHD-optimized design, developer experience, and full SSR compatibility

236 lines 7.34 kB
/** * Enhanced validation utilities for ClarityKit components * Provides comprehensive form validation with TypeScript support */ import { type Writable, type Readable } from 'svelte/store'; /** * Validates an email address * @param email - The email address to validate * @returns Boolean indicating if the email is valid */ export declare function isValidEmail(email: string): boolean; /** * Validates a URL * @param url - The URL to validate * @returns Boolean indicating if the URL is valid */ export declare function isValidUrl(url: string): boolean; /** * Validates a phone number (basic validation) * @param phone - The phone number to validate * @returns Boolean indicating if the phone number is valid */ export declare function isValidPhone(phone: string): boolean; /** * Validates a password against common strength requirements * @param password - The password to validate * @returns An object with validation results and a strength score */ export declare function validatePassword(password: string): { valid: boolean; hasMinLength: boolean; hasUppercase: boolean; hasLowercase: boolean; hasNumber: boolean; hasSpecialChar: boolean; strength: 'weak' | 'medium' | 'strong'; }; /** * Validates a date string * @param dateString - The date string to validate * @returns Boolean indicating if the date is valid */ export declare function isValidDate(dateString: string): boolean; /** * Validates a credit card number using Luhn algorithm * @param cardNumber - The credit card number to validate * @returns Boolean indicating if the card number is valid */ export declare function isValidCreditCard(cardNumber: string): boolean; /** * Enhanced Form Validation System */ export type ValidationResult = { valid: boolean; message?: string; code?: string; }; export type ValidatorFunction<T = any> = (value: T, context?: ValidationContext) => ValidationResult | Promise<ValidationResult>; export type ValidationRule<T = any> = { name: string; validator: ValidatorFunction<T>; message?: string; when?: (context: ValidationContext) => boolean; debounce?: number; }; export type ValidationContext = { fieldName: string; formData: Record<string, any>; touched: boolean; dirty: boolean; submitAttempted: boolean; }; export type FieldState<T = any> = { value: T; error: string | null; touched: boolean; dirty: boolean; validating: boolean; valid: boolean; }; export type FormState = { valid: boolean; submitting: boolean; submitAttempted: boolean; errors: Record<string, string>; touched: Record<string, boolean>; dirty: Record<string, boolean>; }; /** * Built-in validation rules */ export declare const ValidationRules: { required: <T>(message?: string) => ValidationRule<T>; minLength: (min: number, message?: string) => ValidationRule<string>; maxLength: (max: number, message?: string) => ValidationRule<string>; pattern: (regex: RegExp, message?: string) => ValidationRule<string>; email: (message?: string) => ValidationRule<string>; url: (message?: string) => ValidationRule<string>; phone: (message?: string) => ValidationRule<string>; min: (min: number, message?: string) => ValidationRule<number>; max: (max: number, message?: string) => ValidationRule<number>; custom: <T>(validator: (value: T, context?: ValidationContext) => boolean | Promise<boolean>, message?: string) => ValidationRule<T>; conditional: <T>(condition: (context: ValidationContext) => boolean, rule: ValidationRule<T>) => ValidationRule<T>; matchField: (fieldName: string, message?: string) => ValidationRule<string>; }; /** * Field validator class for managing validation of individual form fields */ export declare class FieldValidator<T = any> { readonly name: string; private rules; private _value; private _error; private _touched; private _dirty; private _validating; private debouncedValidators; readonly value: Writable<T | null>; readonly error: Readable<string | null>; readonly touched: Readable<boolean>; readonly dirty: Readable<boolean>; readonly validating: Readable<boolean>; readonly valid: Readable<boolean>; constructor(name: string, initialValue?: T, rules?: ValidationRule<any>[]); /** * Add a validation rule */ addRule(rule: ValidationRule<any>): this; /** * Add multiple validation rules */ addRules(rules: ValidationRule<any>[]): this; /** * Set the field value and trigger validation */ setValue(value: T, context?: ValidationContext): Promise<void>; /** * Mark the field as touched */ setTouched(touched?: boolean): void; /** * Validate the current value */ validate(context?: ValidationContext): Promise<boolean>; /** * Clear validation error */ clearError(): void; /** * Reset field to initial state */ reset(value?: T): void; /** * Get current field state */ getState(): Promise<FieldState<T>>; } /** * Form validator class for managing validation of entire forms */ export declare class FormValidator { private fields; private _submitting; private _submitAttempted; private _formValid; private _formData; readonly submitting: Readable<boolean>; readonly submitAttempted: Readable<boolean>; readonly valid: Readable<boolean>; readonly formData: Readable<Record<string, any>>; /** * Add a field validator to the form */ addField<T>(field: FieldValidator<T>): this; /** * Create and add a new field validator */ createField<T>(name: string, initialValue?: T, rules?: ValidationRule<any>[]): FieldValidator<T>; /** * Get a field validator by name */ getField<T = any>(name: string): FieldValidator<T> | undefined; /** * Remove a field validator */ removeField(name: string): boolean; /** * Get all field names */ getFieldNames(): string[]; /** * Validate all fields in the form */ validateForm(): Promise<boolean>; /** * Validate a specific field */ validateField(fieldName: string): Promise<boolean>; /** * Submit the form with validation */ submit<T = any>(submitFn: (data: Record<string, any>) => Promise<T> | T): Promise<T | null>; /** * Reset the entire form */ reset(): void; /** * Get form data as an object */ getFormData(): Promise<Record<string, any>>; /** * Get form state */ getFormState(): Promise<FormState>; /** * Get error count */ private getErrorCount; /** * Update form-level state based on field states (async version) */ private updateFormState; /** * Update form-level state based on field states (sync version for initial setup) */ private updateFormStateSync; } /** * Utility function to create a form validator */ export declare function createFormValidator(): FormValidator; /** * Utility function to create a field validator */ export declare function createFieldValidator<T>(name: string, initialValue?: T, rules?: ValidationRule<any>[]): FieldValidator<T>; //# sourceMappingURL=validation.d.ts.map