111 lines
2.8 KiB
TypeScript
111 lines
2.8 KiB
TypeScript
import { z } from "zod";
|
|
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),
|
|
};
|
|
}
|