it_solutions_test/src/config/validate-env.ts
Илья Братских bd22447bd3 Add a GraphQL digital card that migrates and seeds itself on startup.
The NestJS API serves the public profile through Apollo Sandbox and loads only the relations selected by the query.
2026-11-02 05:30:54 +03:00

31 lines
825 B
TypeScript

import { z } from 'zod';
const envSchema = z.object({
DATABASE_URL: z
.string()
.regex(
/^postgres(ql)?:\/\//,
'DATABASE_URL must be a PostgreSQL connection string',
),
PORT: z.preprocess(
(value) => (value === undefined || value === '' ? 3000 : value),
z.coerce.number().int().positive(),
),
});
export type AppConfig = z.infer<typeof envSchema>;
export function validateEnv(config: Record<string, unknown>): AppConfig {
const result = envSchema.safeParse({
DATABASE_URL: config.DATABASE_URL,
PORT: config.PORT,
});
if (!result.success) {
const details = result.error.issues
.map((issue) => `${issue.path.join('.') || 'env'}: ${issue.message}`)
.join('\n');
throw new Error(`Environment validation failed:\n${details}`);
}
return result.data;
}