You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
But unfortunately this does not work with z.function():
constschema=z.object({foobar: z.function(),}).and(z.record(z.union([z.function(),z.string()])))// Fails with error "invalid_intersection_types: Intersection results could not be merged" :(schema.parse({foobar: ()=>{},test: ()=>{}})
My understanding/best guess of why it's failing:
When ZodIntersection is passed it calls the mergeValues function which determines that foobar is a key shared in both a and b. Because of this it will then recursively call mergeValues on the values of the foobar property from both a and b. In the case above where z.number() is used this will succeed because the number 100 from both a and b will === each other and thus the merge will be marked as valid. But in the case of z.function() both a.foobar and b.foobar are considered different objects (since it looks like zod creates a different object wrapped around the original () => {} object or something like that, I'm not really sure) and thus they are not consided equal and will fail.
Thanks for reading and for such an extremely useful library!
The text was updated successfully, but these errors were encountered:
I want to replicate the following type in zod:
I believe something like the following should do it:
And indeed that pattern does work fine if you replaced
z.function()
with something likez.number()
:But unfortunately this does not work with
z.function()
:My understanding/best guess of why it's failing:
When ZodIntersection is passed it calls the mergeValues function which determines that
foobar
is a key shared in botha
andb
. Because of this it will then recursively call mergeValues on the values of thefoobar
property from botha
andb
. In the case above wherez.number()
is used this will succeed because the number100
from botha
andb
will===
each other and thus the merge will be marked as valid. But in the case ofz.function()
botha.foobar
andb.foobar
are considered different objects (since it looks like zod creates a different object wrapped around the original() => {}
object or something like that, I'm not really sure) and thus they are not consided equal and will fail.Thanks for reading and for such an extremely useful library!
The text was updated successfully, but these errors were encountered: