2025-11-27 21:33:53 +07:00
|
|
|
import * as z from 'zod'
|
2026-01-28 18:53:08 +07:00
|
|
|
import { createValidationSchema } 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;
|
|
|
|
|
|
|
|
|
|
let cachedSignupSchema: any = null;
|
|
|
|
|
let schemaPromise: Promise<any> | null = null;
|
|
|
|
|
|
|
|
|
|
async function loadConfigFromBackend(): Promise<any> {
|
|
|
|
|
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() {
|
|
|
|
|
console.log('get scheme');
|
|
|
|
|
if (cachedSignupSchema) {
|
|
|
|
|
return cachedSignupSchema;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!schemaPromise) {
|
|
|
|
|
schemaPromise = (async () => {
|
|
|
|
|
const config = await loadConfigFromBackend();
|
|
|
|
|
cachedSignupSchema = createValidationSchema(config);
|
|
|
|
|
return cachedSignupSchema;
|
|
|
|
|
})();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await schemaPromise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-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'
|
|
|
|
|
}
|