D-15. `RuntimeValidator` and `MappingResult` in
external-contract-runtime.ts are the canonical validator protocol and
mapping result. The other shapes stay as public names over them:
- schema-registry.ts: `RuntimeSchemaCodec` is documented as the
registry adapter and gains `runtimeSchemaCodecFromValidator`.
- boundary-mapper.ts: the flat `{ ok: false, code }` result is named
`BoundaryMappingResult`; `MappingResult` stays as an alias, and
`toContractMappingResult` converts to the canonical shape.
The reference feature wrapped each zod schema twice (a RuntimeValidator
in the contract contribution and a separate `project`/`codec` in
reference-schemas.ts). It now has one `zodValidator`; the registry
codecs are a one-liner over it, and validateReferencePayload/Request
look up the codec with Object.hasOwn.
Verified: characterization test
tests/features/reference-feature/reference-validator-protocol.test.ts
passed before and after (array paths, dotted paths, codec ids,
SCHEMA_NOT_REGISTERED); adapter case added to boundary-mapper.test.ts;
gates green except the known removal-fixture test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
3.0 KiB
TypeScript
89 lines
3.0 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
CREATE_REFERENCE_RESOURCE_CONTRACT,
|
|
LIST_REFERENCE_RESOURCES_CONTRACT,
|
|
} from "../../../src/features/reference-feature/contracts/reference-feature-contract-contribution.ts";
|
|
import {
|
|
REFERENCE_RUNTIME_SCHEMA_CODECS,
|
|
validateReferencePayload,
|
|
validateReferenceRequest,
|
|
} from "../../../src/features/reference-feature/contracts/reference-schemas.ts";
|
|
|
|
/**
|
|
* Q-6.7-8 characterization. The reference feature exposes one schema through
|
|
* two protocols: the canonical `RuntimeValidator` (array paths) for the HTTP
|
|
* contract and the registry-facing `RuntimeSchemaCodec` (dotted paths). Both
|
|
* must keep answering exactly this after they share one zod wrapper.
|
|
*/
|
|
describe("reference validator protocols", () => {
|
|
const badList = [{ id: "unsafe", name: 42 }];
|
|
|
|
it("answers the canonical validator with array paths", () => {
|
|
expect(
|
|
LIST_REFERENCE_RESOURCES_CONTRACT.contract.outputValidator.safeParse(
|
|
badList,
|
|
),
|
|
).toEqual({
|
|
success: false,
|
|
issues: [{ path: [0, "name"], code: "invalid_type" }],
|
|
});
|
|
expect(
|
|
CREATE_REFERENCE_RESOURCE_CONTRACT.contract.inputValidator.safeParse({
|
|
name: " Example ",
|
|
}),
|
|
).toEqual({ success: true, data: { name: "Example" } });
|
|
});
|
|
|
|
it("answers the schema codec with dotted paths", () => {
|
|
const codecs = REFERENCE_RUNTIME_SCHEMA_CODECS;
|
|
expect(codecs.ReferenceResourceListPayload.parse(badList)).toEqual({
|
|
success: false,
|
|
issues: [{ path: "0.name", code: "invalid_type" }],
|
|
});
|
|
expect(
|
|
codecs.CreateReferenceResourceCommand.parse({ name: "a", extra: 1 }),
|
|
).toEqual({
|
|
success: false,
|
|
issues: [{ path: "", code: "unrecognized_keys" }],
|
|
});
|
|
expect(
|
|
codecs.ReferenceResourceListQuery.parse({ limit: "5", tags: "open" }),
|
|
).toEqual({ success: true, data: { limit: 5, tags: ["open"] } });
|
|
expect(Object.keys(codecs)).toEqual([
|
|
"ReferenceResourceParams",
|
|
"ReferenceResourceListQuery",
|
|
"NoRequest",
|
|
"CreateReferenceResourceCommand",
|
|
"ReferenceResourceListPayload",
|
|
"ReferenceResourcePayload",
|
|
]);
|
|
for (const [schemaId, codec] of Object.entries(codecs)) {
|
|
expect(codec.schemaId).toBe(schemaId);
|
|
}
|
|
});
|
|
|
|
it("keeps the payload and request lookups apart", () => {
|
|
expect(validateReferencePayload("ReferenceResourceListPayload", badList))
|
|
.toEqual({
|
|
success: false,
|
|
issues: [{ path: "0.name", code: "invalid_type" }],
|
|
});
|
|
expect(validateReferencePayload("NoRequest", {})).toEqual({
|
|
success: false,
|
|
issues: [{ path: "", code: "SCHEMA_NOT_REGISTERED" }],
|
|
});
|
|
expect(
|
|
validateReferenceRequest("CreateReferenceResourceCommand", {
|
|
name: " Example ",
|
|
}),
|
|
).toEqual({ success: true, data: { name: "Example" } });
|
|
expect(
|
|
validateReferenceRequest("ReferenceResourcePayload", {}),
|
|
).toEqual({
|
|
success: false,
|
|
issues: [{ path: "", code: "SCHEMA_NOT_REGISTERED" }],
|
|
});
|
|
});
|
|
});
|