The problem mapping had only kind and code and the reference problem schema dropped everything else, so a server field error reached the form as a generic message. Mappings may now return validationIssues and the reference schema keeps a bounded issues list (path and code only). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
4.8 KiB
TypeScript
174 lines
4.8 KiB
TypeScript
import { z } from "zod";
|
|
import type { RuntimeValidator } from "../../../contracts/external-contract-runtime.ts";
|
|
import { runtimeSchemaCodecFromValidator } from "../../../contracts/schema-registry.ts";
|
|
|
|
export const referenceResourceListQuerySchema = z
|
|
.object({
|
|
cursor: z.string().min(1).optional(),
|
|
limit: z.coerce.number().int().min(1).max(100).default(20),
|
|
tags: z
|
|
.preprocess(
|
|
(value) =>
|
|
value === undefined
|
|
? undefined
|
|
: Array.isArray(value)
|
|
? value
|
|
: [value],
|
|
z.array(z.string().trim().min(1)).readonly(),
|
|
)
|
|
.optional(),
|
|
})
|
|
.strict();
|
|
|
|
export const referenceResourceParamsSchema = z
|
|
.object({
|
|
resourceId: z.string().trim().min(1).max(120),
|
|
})
|
|
.strict();
|
|
|
|
export const referenceResourceDtoSchema = z
|
|
.object({
|
|
id: z.string().min(1).max(120),
|
|
name: z.string().min(1).max(240),
|
|
createdAt: z.string().datetime().optional(),
|
|
})
|
|
.strip();
|
|
|
|
export type ReferenceResourceDto = z.output<
|
|
typeof referenceResourceDtoSchema
|
|
>;
|
|
|
|
export const referenceProblemSchema = z
|
|
.object({
|
|
type: z.string().min(1).max(512),
|
|
title: z.string().min(1).max(240),
|
|
status: z.int().min(100).max(599),
|
|
code: z.string().min(1).max(120).optional(),
|
|
/** 03-04. 422 field issues; backend messages are stripped. */
|
|
issues: z
|
|
.array(
|
|
z
|
|
.object({
|
|
path: z.string().min(1).max(120),
|
|
code: z.string().min(1).max(80),
|
|
})
|
|
.strip(),
|
|
)
|
|
.max(50)
|
|
.optional(),
|
|
})
|
|
.strip();
|
|
|
|
export type ReferenceProblem = z.output<typeof referenceProblemSchema>;
|
|
|
|
export const createReferenceResourceCommandSchema = z
|
|
.object({
|
|
name: z.string().trim().min(1).max(120),
|
|
note: z.string().trim().max(500).optional(),
|
|
})
|
|
.strict();
|
|
|
|
const payloadSchemas = {
|
|
ReferenceResourceListPayload: z.array(referenceResourceDtoSchema).max(100),
|
|
ReferenceResourcePayload: referenceResourceDtoSchema,
|
|
} satisfies Record<string, z.ZodType>;
|
|
|
|
const requestSchemas = {
|
|
ReferenceResourceParams: referenceResourceParamsSchema,
|
|
ReferenceResourceListQuery: referenceResourceListQuerySchema,
|
|
NoRequest: z.object({}).strict(),
|
|
CreateReferenceResourceCommand: createReferenceResourceCommandSchema,
|
|
} satisfies Record<string, z.ZodType>;
|
|
|
|
/**
|
|
* D-15. The one zod wrapper: every reference schema is exposed through the
|
|
* canonical `RuntimeValidator`, and the registry codecs below are adapters
|
|
* over it.
|
|
*/
|
|
export function zodValidator<T>(
|
|
schemaId: string,
|
|
schema: z.ZodType<T>,
|
|
): RuntimeValidator<T> {
|
|
return Object.freeze({
|
|
schemaId,
|
|
safeParse(value: unknown) {
|
|
const result = schema.safeParse(value);
|
|
if (result.success) {
|
|
return Object.freeze({
|
|
success: true as const,
|
|
data: structuredClone(result.data),
|
|
});
|
|
}
|
|
return Object.freeze({
|
|
success: false as const,
|
|
issues: Object.freeze(
|
|
result.error.issues.map((issue) =>
|
|
Object.freeze({
|
|
path: Object.freeze(
|
|
issue.path.map((segment): string | number =>
|
|
typeof segment === "number" ? segment : String(segment),
|
|
),
|
|
),
|
|
code: String(issue.code),
|
|
}),
|
|
),
|
|
),
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
const codec = (schemaId: string, schema: z.ZodType) =>
|
|
runtimeSchemaCodecFromValidator(zodValidator(schemaId, schema));
|
|
|
|
export const REFERENCE_RUNTIME_SCHEMA_CODECS = Object.freeze({
|
|
ReferenceResourceParams: codec(
|
|
"ReferenceResourceParams",
|
|
referenceResourceParamsSchema,
|
|
),
|
|
ReferenceResourceListQuery: codec(
|
|
"ReferenceResourceListQuery",
|
|
referenceResourceListQuerySchema,
|
|
),
|
|
NoRequest: codec("NoRequest", requestSchemas.NoRequest),
|
|
CreateReferenceResourceCommand: codec(
|
|
"CreateReferenceResourceCommand",
|
|
requestSchemas.CreateReferenceResourceCommand,
|
|
),
|
|
ReferenceResourceListPayload: codec(
|
|
"ReferenceResourceListPayload",
|
|
payloadSchemas.ReferenceResourceListPayload,
|
|
),
|
|
ReferenceResourcePayload: codec(
|
|
"ReferenceResourcePayload",
|
|
payloadSchemas.ReferenceResourcePayload,
|
|
),
|
|
});
|
|
|
|
const NOT_REGISTERED = Object.freeze({
|
|
success: false as const,
|
|
issues: Object.freeze([
|
|
Object.freeze({ path: "", code: "SCHEMA_NOT_REGISTERED" }),
|
|
]),
|
|
});
|
|
|
|
function validateOwned(
|
|
owned: Readonly<Record<string, z.ZodType>>,
|
|
schemaId: string,
|
|
value: unknown,
|
|
) {
|
|
return Object.hasOwn(owned, schemaId)
|
|
? REFERENCE_RUNTIME_SCHEMA_CODECS[
|
|
schemaId as keyof typeof REFERENCE_RUNTIME_SCHEMA_CODECS
|
|
].parse(value)
|
|
: NOT_REGISTERED;
|
|
}
|
|
|
|
export function validateReferencePayload(schemaId: string, value: unknown) {
|
|
return validateOwned(payloadSchemas, schemaId, value);
|
|
}
|
|
|
|
export function validateReferenceRequest(schemaId: string, value: unknown) {
|
|
return validateOwned(requestSchemas, schemaId, value);
|
|
}
|