diff --git a/src/contracts/external-contract-runtime.ts b/src/contracts/external-contract-runtime.ts index ca930d11..31e1333f 100644 --- a/src/contracts/external-contract-runtime.ts +++ b/src/contracts/external-contract-runtime.ts @@ -72,22 +72,29 @@ export function invokeValidator( validator: RuntimeValidator, value: unknown, ): ValidatorInvocation { - let result: RuntimeValidationResult; + // 07-06. Reading the result stays inside the try: a hand-written validator + // may return any shape, including throwing getters. try { - result = validator.safeParse(value); + const result: unknown = validator.safeParse(value); + if (!result || typeof result !== "object") throw new TypeError(); + const candidate = result as Readonly>; + if (candidate.success === true) { + return Object.freeze({ + outcome: "VALID" as const, + value: candidate.data as T, + }); + } + const issues = candidate.issues; + if (candidate.success !== false || !Array.isArray(issues)) { + throw new TypeError(); + } + return Object.freeze({ + outcome: "INVALID" as const, + issues: Object.freeze([...(issues as RuntimeValidationIssue[])]), + }); } catch { return Object.freeze({ outcome: "THROWN" as const }); } - if (!result || typeof result !== "object" || !("success" in result)) { - return Object.freeze({ outcome: "THROWN" as const }); - } - if (result.success) { - return Object.freeze({ outcome: "VALID" as const, value: result.data }); - } - return Object.freeze({ - outcome: "INVALID" as const, - issues: Object.freeze([...(result.issues ?? [])]), - }); } export type MappingResult = diff --git a/tests/unit/external-contract-runtime.test.ts b/tests/unit/external-contract-runtime.test.ts index 24a26110..421dfe87 100644 --- a/tests/unit/external-contract-runtime.test.ts +++ b/tests/unit/external-contract-runtime.test.ts @@ -3,7 +3,9 @@ import { describe, expect, it } from "vitest"; import { ContractContributionError, composeContractContributions, + invokeValidator, type InstalledContractContribution, + type RuntimeValidator, } from "../../src/contracts/external-contract-runtime.ts"; import { TEST_CONTRACT_CONTRIBUTION } from "../helpers/external-contract-fixture.ts"; @@ -83,3 +85,34 @@ describe("external contract contribution composition", () => { expect(error.reason).toContain("method"); }); }); + +describe("validator invocation (07-06)", () => { + const returning = (result: unknown): RuntimeValidator => ({ + schemaId: "Malformed", + safeParse: () => result as never, + }); + + it.each([ + ["non-array issues", { success: false, issues: 5 }], + ["object issues", { success: false, issues: {} }], + ["non-boolean success", { success: "no", data: "unchecked" }], + ["throwing issues getter", { + success: false, + get issues(): never { + throw new Error("hostile"); + }, + }], + ])("reports THROWN for a result with %s", (_label, result) => { + expect(invokeValidator(returning(result), {})).toEqual({ outcome: "THROWN" }); + }); + + it("keeps well-formed results", () => { + expect(invokeValidator(returning({ success: true, data: 1 }), {})).toEqual({ + outcome: "VALID", + value: 1, + }); + expect( + invokeValidator(returning({ success: false, issues: [{ path: [], code: "X" }] }), {}), + ).toEqual({ outcome: "INVALID", issues: [{ path: [], code: "X" }] }); + }); +});