Skip to content

Commit

Permalink
Merge pull request #14 from iway1/array-equality
Browse files Browse the repository at this point in the history
now checks for array equality correctly
  • Loading branch information
iway1 authored Jan 4, 2023
2 parents d194472 + be67bd5 commit 27b255b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/__tests__/isZodTypeEqual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
8 changes: 8 additions & 0 deletions src/isZodTypeEqual.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down

0 comments on commit 27b255b

Please sign in to comment.