Skip to content

Commit

Permalink
Merge pull request #76 from iway1/bug/hidden-id-unwrap
Browse files Browse the repository at this point in the history
fixes bug where hidden ids weren't found when using optional etc
  • Loading branch information
iway1 authored Mar 7, 2023
2 parents a2f3962 + 16f6f46 commit cc57a01
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/__tests__/isZodTypeEqual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
16 changes: 11 additions & 5 deletions src/unwrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
};
}

Expand Down

0 comments on commit cc57a01

Please sign in to comment.