From 26337835418408cc574140303d598258dbdd3359 Mon Sep 17 00:00:00 2001 From: Shawn Morreau Date: Fri, 17 May 2024 14:32:34 -0400 Subject: [PATCH] add unit error message special case for operands that serialize to same value (#954) --- ark/schema/roots/unit.ts | 4 +++- ark/type/__tests__/objectLiteral.test.ts | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ark/schema/roots/unit.ts b/ark/schema/roots/unit.ts index 76c0675966..35878b9f3b 100644 --- a/ark/schema/roots/unit.ts +++ b/ark/schema/roots/unit.ts @@ -48,7 +48,9 @@ export const unitImplementation: nodeImplementationOf = }, normalize: schema => schema, defaults: { - description: node => printable(node.unit) + description: node => printable(node.unit), + problem: ({ expected, actual }) => + `${expected === actual ? `must be reference equal to ${expected} (serialized to the same value)` : `must be ${expected} (was ${actual})`}` }, intersections: { unit: (l, r) => Disjoint.from("unit", l, r), diff --git a/ark/type/__tests__/objectLiteral.test.ts b/ark/type/__tests__/objectLiteral.test.ts index bf0f077931..be3843fb15 100644 --- a/ark/type/__tests__/objectLiteral.test.ts +++ b/ark/type/__tests__/objectLiteral.test.ts @@ -54,6 +54,13 @@ contextualize( }) }) + it("serializes to same value but not reference equal", () => { + const t = type("===", {}) + attest(t({}).toString()).snap( + "must be reference equal to {} (serialized to the same value)" + ) + }) + it("error in obj that has tuple that writes error at proper path", () => { // @ts-expect-error attest(() => type({ "a?": ["string", ["stringx", "?"]] }))