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
I'm currently working on getting a few of my OSS libraries updated to Zod v4 in preparation of the final release.
One of my libraries is zod-joda, which supplies multiple Zod schemas for the different JS-Joda types. Each take either a given JS Joda type as input or alternatively (more commonly used) a string, and then tries to parse it into the given JS Joda type.
In Zod v3, the library peer-depended on zod and simply constructed a union between z.custom (for instanceof check) and z.string().transform().
Now I could follow the exact same approach again in the next version and I wouldn't have to change much, but this has several downsides:
If I understand the v4 docs correctly, library authors are encouraged to peer-depend on @zod/core.
The transform would fail to pass through toJSONSchema().
I already made the observation that $ZodTypeInternals has a toJSONSchema property, which I can set on my own internal type to generate a fitting JSON schema, so that would solve the second issue.
I tried several ways to create a custom type for this, looking at both the core and the main Zod v4 library for inspiration, but I keep running into issues:
When defining my own def, the "type" parameter seems to be limited to core-declared types. I could use an existing type, but then my def would not match the given "type".
Eventually I looked into just using $ZodTransform from core directly and not messing about with a custom type at all. This is what I came up with:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm currently working on getting a few of my OSS libraries updated to Zod v4 in preparation of the final release.
One of my libraries is
zod-joda
, which supplies multiple Zod schemas for the different JS-Joda types. Each take either a given JS Joda type as input or alternatively (more commonly used) a string, and then tries to parse it into the given JS Joda type.In Zod v3, the library peer-depended on
zod
and simply constructed a union between z.custom (for instanceof check) and z.string().transform().Now I could follow the exact same approach again in the next version and I wouldn't have to change much, but this has several downsides:
toJSONSchema()
.I already made the observation that
$ZodTypeInternals
has atoJSONSchema
property, which I can set on my own internal type to generate a fitting JSON schema, so that would solve the second issue.I tried several ways to create a custom type for this, looking at both the core and the main Zod v4 library for inspiration, but I keep running into issues:
When defining my own
def
, the "type" parameter seems to be limited to core-declared types. I could use an existing type, but then mydef
would not match the given "type".Eventually I looked into just using
$ZodTransform
from core directly and not messing about with a custom type at all. This is what I came up with:Does anyone see a problem with implementing it this way?
Beta Was this translation helpful? Give feedback.
All reactions