2025-11-27 21:33:53 +07:00
|
|
|
import * as z from 'zod'
|
2026-02-24 17:22:01 +07:00
|
|
|
import { createValidationSchema, createValidationSchemaForPassword } from '@/app/lib/validation-config-parser';
|
2026-01-10 15:12:51 +07:00
|
|
|
import validationConfig from '@/app/lib/validation-config.json';
|
2025-11-27 21:33:53 +07:00
|
|
|
|
2025-11-28 15:28:15 +07:00
|
|
|
export type FormState =
|
|
|
|
|
| {
|
|
|
|
|
errors?: {
|
|
|
|
|
name?: string[]
|
|
|
|
|
email?: string[]
|
|
|
|
|
password?: string[]
|
|
|
|
|
}
|
|
|
|
|
message?: string
|
|
|
|
|
}
|
|
|
|
|
| undefined
|
|
|
|
|
|
|
|
|
|
export type FormAction = (
|
|
|
|
|
state: FormState,
|
|
|
|
|
formData: FormData
|
|
|
|
|
) => Promise<FormState>
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const defaultConfig = validationConfig as ValidationConfig;
|
|
|
|
|
|
2026-02-02 15:00:56 +07:00
|
|
|
async function loadConfigFromBackend(accountType?: 'b2b' | 'b2c'): Promise<any> {
|
2026-01-10 15:12:51 +07:00
|
|
|
try {
|
|
|
|
|
const response = await fetch('/api/validation-config');
|
|
|
|
|
if (!response.ok) throw new Error('Failed to load config');
|
|
|
|
|
return await response.json();
|
|
|
|
|
} catch (error) {
|
2026-01-29 21:34:02 +07:00
|
|
|
//сейчас выполняем это пока бека нету
|
2026-02-05 14:53:43 +07:00
|
|
|
/* console.error('Using default validation config:', error); */
|
2026-01-10 15:12:51 +07:00
|
|
|
return defaultConfig;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 15:00:56 +07:00
|
|
|
export async function getSignupFormSchema(schemaType?: 'b2b' | 'b2c' | 'resetPassword') {
|
2026-02-05 18:10:16 +07:00
|
|
|
let schemaPromise: Promise<any> | null = null;
|
|
|
|
|
let cachedSignupSchema: any = null;
|
2026-01-10 15:12:51 +07:00
|
|
|
|
2026-02-03 17:36:09 +07:00
|
|
|
if (!schemaPromise) {
|
|
|
|
|
schemaPromise = (async () => {
|
|
|
|
|
const config = await loadConfigFromBackend();
|
|
|
|
|
if (schemaType === 'resetPassword') {
|
2026-02-05 18:10:16 +07:00
|
|
|
cachedSignupSchema = createValidationSchemaForPassword(config);
|
2026-02-03 17:36:09 +07:00
|
|
|
} else {
|
|
|
|
|
cachedSignupSchema = createValidationSchema(config, schemaType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cachedSignupSchema;
|
|
|
|
|
})();
|
|
|
|
|
}
|
2026-01-10 15:12:51 +07:00
|
|
|
|
|
|
|
|
return await schemaPromise;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 21:34:02 +07:00
|
|
|
// для валидации в реально времени на клиентской странице
|
2026-01-10 15:12:51 +07:00
|
|
|
export const SignupFormSchema = createValidationSchema(defaultConfig);
|
2025-12-16 11:24:05 +07:00
|
|
|
|
2025-11-27 21:33:53 +07:00
|
|
|
|
2025-11-28 15:28:15 +07:00
|
|
|
export const loginFormSchema = z
|
|
|
|
|
.object({
|
|
|
|
|
email: z
|
|
|
|
|
.string()
|
2025-12-10 16:39:13 +07:00
|
|
|
.min(1, { error: 'login-error-email' })
|
2025-11-28 15:28:15 +07:00
|
|
|
.trim(),
|
|
|
|
|
password: z
|
|
|
|
|
.string()
|
2025-12-10 16:39:13 +07:00
|
|
|
.min(1, { error: 'login-error-password' })
|
2025-11-28 15:28:15 +07:00
|
|
|
.trim(),
|
2025-12-03 21:02:31 +07:00
|
|
|
})
|
|
|
|
|
|
2026-03-31 14:47:39 +07:00
|
|
|
|
|
|
|
|
export const createComplaintSchema = z
|
|
|
|
|
.object({
|
|
|
|
|
textArea: z
|
|
|
|
|
.string()
|
|
|
|
|
.min(6, { error: 'text-area-error-fill-complaint-text' })
|
|
|
|
|
.trim(),
|
|
|
|
|
})
|
|
|
|
|
|
2026-04-07 19:05:44 +07:00
|
|
|
export const createCaseSchema = z
|
|
|
|
|
.object({
|
|
|
|
|
textArea: z
|
|
|
|
|
.string()
|
|
|
|
|
.min(6, { error: 'text-area-error-fill-complaint-text' })
|
|
|
|
|
.trim(),
|
|
|
|
|
caseTitle: z
|
|
|
|
|
.string()
|
|
|
|
|
.min(6, { error: 'text-area-error-fill-complaint-text' })
|
|
|
|
|
.trim(),
|
|
|
|
|
})
|
|
|
|
|
|
2026-01-07 19:47:15 +07:00
|
|
|
export const localDevelopmentUrl = process.env.DEV_URL ? process.env.DEV_URL : 'http://localhost';
|
2025-12-11 18:28:35 +07:00
|
|
|
|
2025-12-26 17:56:57 +07:00
|
|
|
export const API_BASE_URL = process.env.NODE_ENV === 'development'
|
|
|
|
|
? localDevelopmentUrl
|
|
|
|
|
: 'http://app:8080';
|
|
|
|
|
|
2025-12-11 18:28:35 +07:00
|
|
|
export const testUserData = {
|
|
|
|
|
fullName: 'test',
|
|
|
|
|
email: 'test@mail.com',
|
|
|
|
|
subscriptionType: 'base'
|
2026-02-24 17:22:01 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const bankCardFormSchema = z
|
|
|
|
|
.object({
|
|
|
|
|
bankCard: z
|
|
|
|
|
.string()
|
|
|
|
|
.min(19, { error: 'error-bank-card' })
|
|
|
|
|
.trim()
|
|
|
|
|
})
|