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
When using other tooling with Zod, it would be great if that tooling could introspect the schema for z.instanceof to determine the constructor that was provided to it. This would allow such tooling to act on that constructor in many ways; for example, a serialization library may want to support some instances of certain built-ins for which Zod does not have dedicated schema factories.
Solution
Create a dedicated z.ZodInstanceOf class to handle instanceof validation. Add a ctor or other appropriate name to store a reference to the constructor or class. Note that constructor cannot be used as it would collide with the existing shape of the prototype of any instance.
Rational
Currently, z.instanceof creates a schema via z.custom which is just a superRefine against z.ZodAny. This eliminates any data that could be used for introspection since non is passed on to the z.ZodAny. Creating a dedicated class for the instanceof schema would allow introspection of the what constructor it was created with.
Caveat
Changing current implementation of z.instanceof could be a major breaking change, but it is also likely very little of the tooling community built around Zod really does much with z.ZodAny in relation to custom schema since introspection is very limited. This could be a Zod v4 feature where breaking changes are expected.
The text was updated successfully, but these errors were encountered:
Introduction
When using other tooling with Zod, it would be great if that tooling could introspect the schema for
z.instanceof
to determine the constructor that was provided to it. This would allow such tooling to act on that constructor in many ways; for example, a serialization library may want to support some instances of certain built-ins for which Zod does not have dedicated schema factories.Solution
Create a dedicated
z.ZodInstanceOf
class to handleinstanceof
validation. Add actor
or other appropriate name to store a reference to the constructor or class. Note thatconstructor
cannot be used as it would collide with the existing shape of the prototype of any instance.Rational
Currently,
z.instanceof
creates a schema viaz.custom
which is just asuperRefine
againstz.ZodAny
. This eliminates any data that could be used for introspection since non is passed on to thez.ZodAny
. Creating a dedicated class for theinstanceof
schema would allow introspection of the what constructor it was created with.Caveat
Changing current implementation of
z.instanceof
could be a major breaking change, but it is also likely very little of the tooling community built around Zod really does much withz.ZodAny
in relation to custom schema since introspection is very limited. This could be a Zod v4 feature where breaking changes are expected.The text was updated successfully, but these errors were encountered: