From 16f6f46a6a84befdb07e7559a584fda144b619c0 Mon Sep 17 00:00:00 2001 From: Isaac Way Date: Mon, 6 Mar 2023 20:59:40 -0600 Subject: [PATCH] fixes bug where hidden ids weren't found when using optional etc --- src/__tests__/isZodTypeEqual.test.ts | 7 +++++++ src/unwrap.tsx | 16 +++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/__tests__/isZodTypeEqual.test.ts b/src/__tests__/isZodTypeEqual.test.ts index fc0f3a4..8dda77e 100644 --- a/src/__tests__/isZodTypeEqual.test.ts +++ b/src/__tests__/isZodTypeEqual.test.ts @@ -259,4 +259,11 @@ describe("isZodTypeEqual", () => { expect(isZodTypeEqual(A, B)).toStrictEqual(true); }); + it("should return true if a unique field schema is compared with its optional", () => { + const A = createUniqueFieldSchema(z.string(), "text"); + console.log("A def: "); + console.log(A._def); + + expect(isZodTypeEqual(A, A.optional())).toStrictEqual(true); + }); }); diff --git a/src/unwrap.tsx b/src/unwrap.tsx index 1c06e1b..9177b16 100644 --- a/src/unwrap.tsx +++ b/src/unwrap.tsx @@ -25,12 +25,12 @@ export function unwrap(type: RTFSupportedZodTypes): { // Realized zod has a built in "unwrap()" function after writing this. // Not sure if it's super necessary. let r = type; - let hiddenId: null | string = null; - if (isSchemaWithHiddenProperties(type)) { - hiddenId = type._def[HIDDEN_ID_PROPERTY]; - } + let unwrappedHiddenId: null | string = null; while (unwrappable.has(r._def.typeName)) { + if (isSchemaWithHiddenProperties(r)) { + unwrappedHiddenId = r._def[HIDDEN_ID_PROPERTY]; + } switch (r._def.typeName) { case z.ZodFirstPartyTypeKind.ZodOptional: r = r._def.innerType; @@ -49,9 +49,15 @@ export function unwrap(type: RTFSupportedZodTypes): { } } + let innerHiddenId: null | string = null; + + if (isSchemaWithHiddenProperties(r)) { + innerHiddenId = r._def[HIDDEN_ID_PROPERTY]; + } + return { type: r, - [HIDDEN_ID_PROPERTY]: hiddenId, + [HIDDEN_ID_PROPERTY]: innerHiddenId || unwrappedHiddenId, }; }