Files
no-copy-frontend/src/app/lib/validation-config-parser.ts
T

229 lines
6.7 KiB
TypeScript
Raw Normal View History

2026-01-10 15:12:51 +07:00
import * as z from 'zod';
2026-01-10 15:44:37 +07:00
//@ts-ignore
import validator from 'validator';
2026-01-10 15:12:51 +07:00
interface FieldConfig {
maxSize?: { value: number; message: string };
minSize?: { value: number; message: string };
allowedSymbols?: { value: string; message: string };
optional?: { value: boolean };
requiredDigit?: { value: boolean; message: string };
requiredLetter?: { value: boolean; message: string };
caseType?: { value: 'LOWER' | 'UPPER' | 'ANY'; message: string };
emailValidation?: { value: boolean; message: string };
}
interface ValidationConfig {
[key: string]: FieldConfig;
}
2026-01-10 15:44:37 +07:00
/* const EMAIL_REGEX = /^([a-z0-9.\-_]+|[а-яё0-9.\-_]+)@(([a-z0-9.\-]+)\.([a-z]{2,})|([a-z0-9.\-]+)\.([а-яё]{2,})|([а-яё0-9.\-]+)\.([a-z]{2,})|([а-яё0-9.\-]+)\.([а-яё]{2,}))$/i; */
2026-01-10 15:12:51 +07:00
2026-01-29 21:34:02 +07:00
export function createValidationSchema(config: ValidationConfig, accountType?: 'b2b' | 'b2c') {
2026-01-10 15:12:51 +07:00
const schema: Record<string, any> = {};
// fullName
if (config.fullName) {
schema.fullName = z
.string()
.min(config.fullName.minSize?.value || 1, {
error: config.fullName.minSize?.message || 'register-error-name-min'
})
.max(config.fullName.maxSize?.value || 100, {
error: config.fullName.maxSize?.message || 'register-error-name-max'
})
.regex(new RegExp(config.fullName.allowedSymbols?.value || '.*'), {
message: config.fullName.allowedSymbols?.message || 'register-error-name-not-allowed-symbols'
})
.trim();
}
2026-02-03 15:25:11 +07:00
// companyName
if (config.companyName && accountType === 'b2b') {
2026-01-29 14:34:04 +07:00
schema.companyName = z
.string()
.trim()
.min(config.companyName.minSize?.value || 1, {
error: config.companyName.minSize?.message || 'register-error-company-name-min'
})
.max(config.companyName.maxSize?.value || 100, {
error: config.companyName.maxSize?.message || 'register-error-company-name-max'
})
.regex(new RegExp(config.companyName.allowedSymbols?.value || '.*'), {
message: config.companyName.allowedSymbols?.message || 'register-error-company-name-not-allowed-symbols'
});
}
2026-01-10 15:12:51 +07:00
// email
if (config.email) {
let emailSchema = z
.string()
.min(config.email.minSize?.value || 1, {
error: config.email.minSize?.message || 'register-error-email-min'
})
.max(config.email.maxSize?.value || 254, {
error: config.email.maxSize?.message || 'register-error-email-max'
})
.trim();
// Проверка на нижний регистр
if (config.email.caseType?.value === 'LOWER') {
emailSchema = emailSchema.refine(
(value) => value === value.toLowerCase(),
{ message: config.email.caseType?.message || 'register-error-email-no-uppercase' }
);
}
// Проверка допустимых символов (опционально, если указано)
if (config.email.allowedSymbols?.value) {
emailSchema = emailSchema.regex(
new RegExp(config.email.allowedSymbols.value),
{ message: config.email.allowedSymbols.message }
);
}
if (config.email.emailValidation?.value) {
emailSchema = emailSchema.refine(
2026-01-10 15:44:37 +07:00
(value) => validator.isEmail(value, {
allow_utf8_local_part: true,
require_tld: true,
}),
{ message: config.email.emailValidation.message }
2026-01-10 15:12:51 +07:00
);
}
schema.email = emailSchema;
}
// phone
if (config.phone) {
let phoneSchema = z.string();
// Минимальная длина
if (config.phone.minSize) {
phoneSchema = phoneSchema.min(config.phone.minSize.value, {
error: config.phone.minSize.message
});
}
// Максимальная длина
if (config.phone.maxSize) {
phoneSchema = phoneSchema.max(config.phone.maxSize.value, {
error: config.phone.maxSize.message
});
}
// Допустимые символы
if (config.phone.allowedSymbols) {
phoneSchema = phoneSchema.regex(
new RegExp(config.phone.allowedSymbols.value),
{ message: config.phone.allowedSymbols.message }
);
}
schema.phone = phoneSchema;
}
// password
if (config.password) {
let passwordSchema = z
.string()
.min(config.password.minSize?.value || 8, {
error: config.password.minSize?.message || 'register-error-password-symbols'
})
.max(config.password.maxSize?.value || 124, {
error: config.password.maxSize?.message || 'register-error-max-password'
})
.trim();
// Проверка допустимых символов
if (config.password.allowedSymbols?.value) {
passwordSchema = passwordSchema.regex(
new RegExp(config.password.allowedSymbols.value),
{ message: config.password.allowedSymbols.message }
);
}
// Проверка наличия цифры
if (config.password.requiredDigit?.value) {
passwordSchema = passwordSchema.regex(
/[0-9]/,
{ message: config.password.requiredDigit.message }
);
}
// Проверка наличия буквы
if (config.password.requiredLetter?.value) {
passwordSchema = passwordSchema.regex(
/[a-zA-Zа-яёА-ЯЁ]/,
{ message: config.password.requiredLetter.message }
);
}
schema.password = passwordSchema;
}
// Дополнительные поля для формы регистрации
schema.confirm_password = z.string();
schema.agree = z.string().min(2, { error: 'register-error-agree' });
2026-02-02 15:00:56 +07:00
return z
.object(schema)
.refine((data) => data.password === data.confirm_password, {
message: 'repeat-password',
path: ['confirm_password'],
});
}
export function createValidationSchemaForPssword(config: ValidationConfig) {
const schema: Record<string, any> = {};
// password
if (config.password) {
let passwordSchema = z
.string()
.min(config.password.minSize?.value || 8, {
error: config.password.minSize?.message || 'register-error-password-symbols'
})
.max(config.password.maxSize?.value || 124, {
error: config.password.maxSize?.message || 'register-error-max-password'
})
.trim();
// Проверка допустимых символов
if (config.password.allowedSymbols?.value) {
passwordSchema = passwordSchema.regex(
new RegExp(config.password.allowedSymbols.value),
{ message: config.password.allowedSymbols.message }
);
}
// Проверка наличия цифры
if (config.password.requiredDigit?.value) {
passwordSchema = passwordSchema.regex(
/[0-9]/,
{ message: config.password.requiredDigit.message }
);
}
// Проверка наличия буквы
if (config.password.requiredLetter?.value) {
passwordSchema = passwordSchema.regex(
/[a-zA-Zа-яёА-ЯЁ]/,
{ message: config.password.requiredLetter.message }
);
}
schema.password = passwordSchema;
}
// Дополнительные поля для формы регистрации
schema.confirm_password = z.string();
2026-01-10 15:12:51 +07:00
return z
.object(schema)
.refine((data) => data.password === data.confirm_password, {
message: 'repeat-password',
path: ['confirm_password'],
});
}