@senka-ai/ui
Version:
A modern, type-safe Svelte 5 UI component library with full theme support, accessibility standards, and robust state management patterns
115 lines • 3.56 kB
TypeScript
/**
* Unified form validation architecture for Senka components
* Provides reusable validation logic and state management
*/
export type ValidationRule<T = any> = {
message: string;
validator: (value: T) => boolean;
};
export type ValidationResult = {
isValid: boolean;
errors: string[];
};
export type FormFieldState<T = any> = {
value: T;
isDirty: boolean;
isTouched: boolean;
errors: string[];
isValid: boolean;
};
export type FormState<T extends Record<string, any> = Record<string, any>> = {
fields: {
[K in keyof T]: FormFieldState<T[K]>;
};
isValid: boolean;
isDirty: boolean;
isSubmitting: boolean;
};
/**
* Validates a single value against an array of validation rules
*/
export declare function validateValue<T>(value: T, rules?: ValidationRule<T>[]): ValidationResult;
/**
* Common validation rules for educational app forms
*/
export declare const validationRules: {
/**
* Validates required fields
*/
required: (message?: string) => ValidationRule<any>;
/**
* Validates minimum length for strings
*/
minLength: (min: number, message?: string) => ValidationRule<string>;
/**
* Validates maximum length for strings
*/
maxLength: (max: number, message?: string) => ValidationRule<string>;
/**
* Validates email format
*/
email: (message?: string) => ValidationRule<string>;
/**
* Validates numeric values
*/
numeric: (message?: string) => ValidationRule<string | number>;
/**
* Validates minimum value for numbers
*/
min: (minValue: number, message?: string) => ValidationRule<string | number>;
/**
* Validates maximum value for numbers
*/
max: (maxValue: number, message?: string) => ValidationRule<string | number>;
/**
* Validates patterns using regex
*/
pattern: (regex: RegExp, message?: string) => ValidationRule<string>;
/**
* Validates Romanian educational ID format (CNP)
*/
romanianCNP: (message?: string) => ValidationRule<string>;
};
/**
* Creates a reactive form validation state manager
*/
export declare function useFormValidation<T extends Record<string, any>>(initialValues: T, validationSchema?: {
[K in keyof T]?: ValidationRule<T[K]>[];
}): {
readonly formState: FormState<T>;
readonly isValid: boolean;
readonly isDirty: boolean;
readonly isSubmitting: boolean;
updateField: <K extends keyof T>(fieldName: K, value: T[K], touch?: boolean) => void;
touchField: <K extends keyof T>(fieldName: K) => void;
validateForm: () => boolean;
resetForm: () => void;
setSubmitting: (isSubmitting: boolean) => void;
getFormValues: () => T;
getFieldProps: <K extends keyof T>(fieldName: K) => {
value?: undefined;
error?: undefined;
onchange?: undefined;
onblur?: undefined;
} | {
value: T[K];
error: string | undefined;
onchange: (value: T[K]) => void;
onblur: () => void;
};
};
/**
* Simplified validation hook for individual form fields
*/
export declare function useFieldValidation<T>(initialValue: T, rules?: ValidationRule<T>[]): {
readonly state: FormFieldState<T>;
readonly value: T;
readonly error: string | undefined;
readonly isValid: boolean;
readonly isDirty: boolean;
readonly isTouched: boolean;
updateValue: (value: T, touch?: boolean) => void;
touch: () => void;
reset: () => void;
};
//# sourceMappingURL=validation.svelte.d.ts.map