From be67bd5f955c78f472228a56529b7d1292a6fc79 Mon Sep 17 00:00:00 2001 From: Isaac Way Date: Wed, 4 Jan 2023 07:08:46 -0600 Subject: [PATCH] now checks for array equality correctly --- src/__tests__/isZodTypeEqual.test.ts | 40 ++++++++++++++++++++++++++++ src/isZodTypeEqual.tsx | 8 ++++++ 2 files changed, 48 insertions(+) diff --git a/src/__tests__/isZodTypeEqual.test.ts b/src/__tests__/isZodTypeEqual.test.ts index 2a7a0c5..998a4c3 100644 --- a/src/__tests__/isZodTypeEqual.test.ts +++ b/src/__tests__/isZodTypeEqual.test.ts @@ -161,4 +161,44 @@ describe("isZodTypeEqual", () => { }); expect(isZodTypeEqual(A, B)).toStrictEqual(false); }); + it("should return true when an array schemas inner type are equal for primitive types", () => { + const A = z.string().array(); + const B = z.string().array(); + + expect(isZodTypeEqual(A, B)).toStrictEqual(true); + }); + it("should return false when an arrays inner types are not equal for primitive types.", () => { + const A = z.string().array(); + const B = z.boolean().array(); + + expect(isZodTypeEqual(A, B)).toStrictEqual(false); + }); + it("should return true when an arrays inner type are equal for object types", () => { + const A = z + .object({ + one: z.string(), + }) + .array(); + const B = z + .object({ + one: z.string(), + }) + .array(); + + expect(isZodTypeEqual(A, B)).toStrictEqual(true); + }); + it("should return false when an arrays inner types are not equal for object types", () => { + const A = z + .object({ + a: z.string(), + }) + .array(); + const B = z + .object({ + a: z.boolean(), + }) + .array(); + + expect(isZodTypeEqual(A, B)).toStrictEqual(false); + }); }); diff --git a/src/isZodTypeEqual.tsx b/src/isZodTypeEqual.tsx index 96510b0..4f0ba0a 100644 --- a/src/isZodTypeEqual.tsx +++ b/src/isZodTypeEqual.tsx @@ -19,6 +19,14 @@ export function isZodTypeEqual( if (a._def.typeName !== b._def.typeName) return false; + if ( + a._def.typeName === ZodFirstPartyTypeKind.ZodArray && + b._def.typeName === ZodFirstPartyTypeKind.ZodArray + ) { + if (isZodTypeEqual(a._def.type, b._def.type)) return true; + return false; + } + // Recursively check if objects are equal if ( a._def.typeName === ZodFirstPartyTypeKind.ZodObject &&