import * as z from 'zod' import { createValidationSchema, createValidationSchemaForPassword } from '@/app/lib/validation-config-parser'; import validationConfig from '@/app/lib/validation-config.json'; export type FormState = | { errors?: { name?: string[] email?: string[] password?: string[] } message?: string } | undefined export type FormAction = ( state: FormState, formData: FormData ) => Promise 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; async function loadConfigFromBackend(accountType?: 'b2b' | 'b2c'): Promise { try { const response = await fetch('/api/validation-config'); if (!response.ok) throw new Error('Failed to load config'); return await response.json(); } catch (error) { //сейчас выполняем это пока бека нету /* console.error('Using default validation config:', error); */ return defaultConfig; } } export async function getSignupFormSchema(schemaType?: 'b2b' | 'b2c' | 'resetPassword') { let schemaPromise: Promise | null = null; let cachedSignupSchema: any = null; if (!schemaPromise) { schemaPromise = (async () => { const config = await loadConfigFromBackend(); if (schemaType === 'resetPassword') { cachedSignupSchema = createValidationSchemaForPassword(config); } else { cachedSignupSchema = createValidationSchema(config, schemaType); } return cachedSignupSchema; })(); } return await schemaPromise; } // для валидации в реально времени на клиентской странице export const SignupFormSchema = createValidationSchema(defaultConfig); export const loginFormSchema = z .object({ email: z .string() .min(1, { error: 'login-error-email' }) .trim(), password: z .string() .min(1, { error: 'login-error-password' }) .trim(), }) export const createComplaintSchema = z .object({ textArea: z .string() .min(6, { error: 'text-area-error-fill-complaint-text' }) .trim(), }) 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(), }) export const localDevelopmentUrl = process.env.DEV_URL ? process.env.DEV_URL : 'http://localhost'; export const API_BASE_URL = process.env.NODE_ENV === 'development' ? localDevelopmentUrl : 'http://app:8080'; export const testUserData = { fullName: 'test', email: 'test@mail.com', subscriptionType: 'base' } export const bankCardFormSchema = z .object({ bankCard: z .string() .min(19, { error: 'error-bank-card' }) .trim() })