Skip to content

Commit

Permalink
fixes bug where hidden ids weren't found when using optional etc
Browse files Browse the repository at this point in the history
  • Loading branch information
iway1 committed Mar 7, 2023
1 parent a2f3962 commit 16f6f46
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 16f6f46

Please sign in to comment.