Files
clean-architecture-frontend…/src/adapters/http/schema-registry.ts
T
donghyeon-kaandClaude Opus 5.5 9dc95ca066 fix(contracts): set zod jitless before any schema is built so the production csp sees no eval (F-7, 13-08)
zod probed Function("") on its first object schema, which the production CSP
(no unsafe-eval) reported on every boot. Schema code now imports z from
src/contracts/zod.ts, enforced by a dependency-cruiser rule, and the CSP smoke
drops its init-script workaround.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 12:58:57 +09:00

111 lines
2.8 KiB
TypeScript

import { z } from "../../contracts/zod.ts";
import { projectValidationIssues } from "../../contracts/validation-issues.ts";
const metaSchema = z
.object({
requestId: z.string().regex(/^[A-Za-z0-9._:-]{1,128}$/),
traceId: z.string().regex(/^[A-Za-z0-9._:-]{1,128}$/),
correlationId: z.string().regex(/^[A-Za-z0-9._:-]{1,128}$/).optional(),
})
.strip();
export const successEnvelopeSchema = z
.object({
success: z.literal(true),
data: z.unknown(),
meta: metaSchema,
})
.strict();
export const failureEnvelopeSchema = z
.object({
success: z.literal(false),
error: z
.object({
code: z.string().regex(/^[A-Z0-9_]{1,64}$/),
category: z.string().min(1).max(64).optional(),
message: z.string().max(1_024).optional(),
retryable: z.boolean().optional(),
details: z.unknown().optional(),
})
.strict(),
meta: metaSchema,
})
.strict();
export const responseEnvelopeSchema = z.discriminatedUnion("success", [
successEnvelopeSchema,
failureEnvelopeSchema,
]);
const payloadSchemas: Readonly<Record<string, z.ZodType<unknown>>> =
Object.freeze({});
const requestSchemas: Readonly<Record<string, z.ZodType<unknown>>> =
Object.freeze({});
export type SchemaIssue = Readonly<{
path: string;
code: string;
schemaId?: string;
}>;
export type SchemaValidationResult =
| Readonly<{ success: true; data: unknown }>
| Readonly<{ success: false; issues: readonly SchemaIssue[] }>;
export function validateEnvelope(value: unknown): SchemaValidationResult {
return projectResult(responseEnvelopeSchema.safeParse(value));
}
export function validateOperationPayload(
schemaId: string,
value: unknown,
): SchemaValidationResult {
const schema = payloadSchemas[schemaId];
if (!schema) return missingSchema(schemaId);
return projectResult(schema.safeParse(value));
}
export function validateOperationRequest(
schemaId: string,
value: unknown,
): SchemaValidationResult {
const schema = requestSchemas[schemaId];
if (!schema) return missingSchema(schemaId);
return projectResult(schema.safeParse(value));
}
function missingSchema(schemaId: string): SchemaValidationResult {
return {
success: false,
issues: [{ path: "", code: "SCHEMA_NOT_REGISTERED", schemaId }],
};
}
function projectResult(
result:
| Readonly<{ success: true; data: unknown }>
| Readonly<{
success: false;
error: Readonly<{
issues: readonly Readonly<{
path: readonly PropertyKey[];
code: string;
}>[];
}>;
}>,
): SchemaValidationResult {
if (result.success) {
return {
success: true,
data: structuredClone(result.data),
};
}
return {
success: false,
issues: projectValidationIssues(result.error.issues),
};
}