try to do better

This commit is contained in:
arrelin
2026-01-29 15:17:54 +03:00
parent f00ddc7d10
commit 24f04a7e82
60 changed files with 5335 additions and 1254 deletions

View File

@@ -0,0 +1,52 @@
export const validation = {
isValidEmail(email: string): boolean {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
},
isValidAmount(amount: string | number): boolean {
const num = typeof amount === 'string' ? parseFloat(amount) : amount;
return !isNaN(num) && num > 0;
},
isNonEmpty(value: string): boolean {
return value.trim().length > 0;
},
minLength(value: string, min: number): boolean {
return value.trim().length >= min;
},
maxLength(value: string, max: number): boolean {
return value.trim().length <= max;
},
isPositiveNumber(value: string | number): boolean {
const num = typeof value === 'string' ? parseFloat(value) : value;
return !isNaN(num) && num > 0;
},
isNonNegativeNumber(value: string | number): boolean {
const num = typeof value === 'string' ? parseFloat(value) : value;
return !isNaN(num) && num >= 0;
},
validateForm<T extends Record<string, any>>(
values: T,
rules: Partial<Record<keyof T, (value: any) => string | null>>
): Partial<Record<keyof T, string>> {
const errors: Partial<Record<keyof T, string>> = {};
for (const field in rules) {
const validator = rules[field];
if (validator) {
const error = validator(values[field]);
if (error) {
errors[field] = error;
}
}
}
return errors;
},
};