Closed langaads closed 4 months ago
Hey @langaads that is a very interesting case. However I do not think this has anything to do with our library.
I traced what happens in our code since we never call parse/safeParse. And I actually got to this
const testSchema = z.coerce.bigint();
console.log(testSchema.isOptional());
The isOptional getter of zod is throwing the error. And there is nothing we can do about that - I feel like this is a perfectly valid case and it is a bug on zod
's end.
With a quick look up I can see that there is an open issue there - https://github.com/colinhacks/zod/issues/1911
I am closing this issue on our end since there is not much that we can do in my opinion
Thanks @AGalabov for the feedback and research on our behalf, at least this issue will help as a documentation to anyone having the same problem. Using the schema.isOptional() is probably not a common usecase when handling zod schemas alone.
For now i'm hacking my way trough removing the coerce before passing schemas to the docs, but it's a laborous work as i use snowflake Ids for most my entities, and i need them coerced since they are strings on incoming jsons.
const coercedBigInt = z.object({
id: z.coerce.bigint(),
other: z.any(),
});
const coercedBigIntDocs = coercedBigInt
.omit({ id: true })
.extend({ id: z.bigint() });
When using a schema that coerces a bigint, it throws an exception. removing the coerce works.
Minimal reproduction script:
node index.js