import { describe, expect, it } from "vitest"; import { invokeValidator } from "../../src/contracts/external-contract-runtime.ts"; import { unvalidatedPayload } from "../../src/contracts/unvalidated-payload.ts"; import { z } from "../../src/contracts/zod.ts"; import { zodValidator } from "../../src/contracts/zod-validator.ts"; /** * 1101/07-03. The one zod adapter lives in src/contracts, so every feature * imports it instead of copying it, and it outlives the sample feature. */ describe("zodValidator", () => { const validator = zodValidator( "Entity", z.object({ id: z.string().min(1), tags: z.array(z.string()) }).strip(), ); it("returns a copy of the parsed value, not the caller's object", () => { const source = { id: "a", tags: ["x"] }; const result = validator.safeParse(source); expect(result).toEqual({ success: true, data: source }); if (!result.success) throw new Error("expected success"); expect(result.data.tags).not.toBe(source.tags); expect(Object.isFrozen(result)).toBe(true); }); it("reports issues with array paths and string codes", () => { expect(validator.safeParse({ id: "", tags: [1] })).toEqual({ success: false, issues: [ { path: ["id"], code: "too_small" }, { path: ["tags", 0], code: "invalid_type" }, ], }); }); it("checks its input, so it carries no skip marker", () => { expect(validator.schemaId).toBe("Entity"); expect(validator.validation).toBeUndefined(); }); }); describe("unvalidatedPayload", () => { const skipped = unvalidatedPayload<{ id: string }>( "EntityPayload", "generated client types are trusted", ); it("names the skip on the validator itself", () => { expect(skipped).toMatchObject({ schemaId: "EntityPayload", validation: "NONE", unvalidatedReason: "generated client types are trusted", }); }); it("accepts any value as a copy", () => { const source = { anything: [1] }; const invocation = invokeValidator(skipped, source); expect(invocation).toEqual({ outcome: "VALID", value: source }); if (invocation.outcome !== "VALID") throw new Error("expected VALID"); expect(invocation.value).not.toBe(source); }); });