TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Zod 4

770 pointsby bpierre2 days ago

37 comments

colinmcd2 days ago
Author here, AMA!<p>Regarding the versioning: I wrote a fairly detailed writeup here[0] for those who are interested in the reasons for this approach.<p>Ultimately npm is not designed to handle the situation Zod finds itself in. Zod is subject to a bunch of constraints that virtually no other libraries are subject to. Namely, the are dozens or hundreds of libraries that directly import interfaces&#x2F;classes from Zod and use them in their own public-facing API.<p>Since these libraries are directly coupled to Zod, they would need to publish a new major version whenever Zod does. That&#x27;s ultimately reasonable in isolation, but in Zod&#x27;s case it would trigger a &quot;version avalanche&quot; would just be painful for everyone involved. Selfishly, I suspect it would result in a huge swath of the ecosystem pinning on v3 forever.<p>The approach I ended up using is analogous to what Golang does. In essence a given package never publishes new breaking versions: they just add a new subpath when a new breaking release is made. In the TypeScript ecosystem, this means libraries can configure a single peer dependency on zod@^3.25.0 and support both versions simultaneously by importing what they need from &quot;zod&#x2F;v3&quot; and &quot;zod&#x2F;v4&quot;. It provides a nice opt-in incremental upgrade path for end-users of Zod too.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;colinhacks&#x2F;zod&#x2F;issues&#x2F;4371">https:&#x2F;&#x2F;github.com&#x2F;colinhacks&#x2F;zod&#x2F;issues&#x2F;4371</a>
评论 #44034577 未加载
评论 #44039422 未加载
评论 #44034605 未加载
评论 #44033445 未加载
评论 #44036663 未加载
评论 #44033821 未加载
评论 #44033242 未加载
评论 #44039356 未加载
评论 #44040209 未加载
评论 #44037613 未加载
评论 #44037589 未加载
评论 #44033655 未加载
评论 #44034882 未加载
评论 #44033604 未加载
评论 #44035047 未加载
评论 #44040108 未加载
评论 #44037793 未加载
rafram2 days ago
&gt; To simplify the migration process both for users and Zod&#x27;s ecosystem of associated libraries, Zod 4 is being published alongside Zod 3 as part of the zod@3.25 release. [...] import Zod 4 from the &quot;&#x2F;v4&quot; subpath<p>npm is an absolute disaster of a dependency management system. Peer dependencies are so broken that they had to make v4 pretend it&#x27;s v3.
评论 #44032999 未加载
评论 #44031376 未加载
评论 #44032448 未加载
评论 #44032209 未加载
评论 #44039968 未加载
评论 #44037564 未加载
评论 #44031366 未加载
评论 #44032324 未加载
评论 #44031320 未加载
johnfn2 days ago
I&#x27;m curious if anyone here can answer a question I&#x27;ve wondered about for a long time. I&#x27;ve heard Zod might be in the right ballpark, but from reading the documentation, I&#x27;m not sure how I would go about it.<p>Say I have a type returned by the server that might have more sophisticated types than the server API can represent. For instance, api&#x2F;:postid&#x2F;author returns a User, but it could either be a normal User or an anonymous User, in which case fields like `username`, `location`, etc come back null. So in this case I might want to use a discriminated union to represent my User object. And other objects coming back from other endpoints might also need some type alterations done to them as well. For instance, a User might sometimes have Post[] on them, and if the Post is from a moderator, it might have special attributes, etc - another discriminated union.<p>In the past, I&#x27;ve written functions like normalizeUser() and normalizePost() to solve this, but this quickly becomes really messy. Since different endpoints return different subsets of the User&#x2F;Post model, I would end up writing like 5 different versions of normalizePost for each endpoint, which seems like a mess.<p>How do people solve this problem?
评论 #44031272 未加载
评论 #44031235 未加载
评论 #44031184 未加载
评论 #44031461 未加载
评论 #44033586 未加载
评论 #44031145 未加载
causal2 days ago
Zod is a lot better than some of the alternative solutions I&#x27;ve seen. That said, the need for this sort of explicit validation always felt like a failure of where modern web development has taken us. It&#x27;s so frustrating how full-stack development requires so many ways of describing the same shapes: JS input validation, Swagger for API definition, server-side input validation, ORM for schema conformance, and TypeScript often requiring separate definitions on server and client side. It&#x27;s so tedious.
评论 #44031137 未加载
评论 #44031026 未加载
评论 #44038062 未加载
评论 #44031175 未加载
评论 #44032905 未加载
评论 #44031509 未加载
评论 #44031101 未加载
评论 #44031027 未加载
satvikpendem2 days ago
Zod 4 looks good but even with their latest improvements, ArkType is still an order of magnitude faster. Sometimes for the sake of backward and syntax compatibility, it is difficult to make something much faster than a fully greenfield newer library. We recently did an analysis of all these types of tools for our project and decided to go with ArkType for partially this reason, the other was TypeScript ergonomics felt nicer.
评论 #44031449 未加载
评论 #44032135 未加载
评论 #44031526 未加载
评论 #44033777 未加载
myflash131 day ago
As a full stack dev that runs their own SaaS with thousands of users in production, sometimes I get a very pleasant reminder of how many problems I am blissfully unaware of just because I decided not to build an SPA or use JS frontend frameworks. It never occurred to me that I need such a thing as Zod&#x2F;ArkType - first time hearing of it, and I have no use for it.<p>It boggles my mind how much effort and complexity and tooling goes into building an SPA. Entire classes of problems simply don&#x27;t exist if you choose not to build an SPA. Meanwhile, I use the browser as designed: with full page reloads, backend development only, and occasional reactivity using a backend-only framework like Laravel Livewire. Everything is so simple: from access control to validation to state management. And yes, my app is fast, reactive, modern, SEO friendly, and serves thousands of users in production.
评论 #44040810 未加载
评论 #44040101 未加载
评论 #44040131 未加载
评论 #44040080 未加载
评论 #44040228 未加载
评论 #44040052 未加载
8s2ngy2 days ago
Congratulations to the Zod team on the new release. At the risk of sounding overtly negative, I can&#x27;t help but shudder when I think about the number of breaking changes outlined in the migration guide. For projects that rely heavily on Zod, it feels like a daunting task ahead—one that will demand a lot of developer attention and time to navigate. Having maintained a few frontend projects that are 4-5 years old at work, I really empathize with them.<p>In my experience, large React projects often depend on a multitude of libraries, and when each one rolls out substantial changes—sometimes with barely any documentation—it can quickly become overwhelming. This is honestly one of my least favorite aspects of working with JavaScript. It just feels like a constant uphill battle to keep everything in sync and functioning smoothly.
评论 #44031721 未加载
评论 #44031379 未加载
评论 #44031238 未加载
varbhat2 days ago
I am not an expert here but I had a thought that JSON-Schema might be a good choice because since it&#x27;s schema based, i can implement the validators in Non-Typescript languages too.<p><a href="https:&#x2F;&#x2F;ajv.js.org" rel="nofollow">https:&#x2F;&#x2F;ajv.js.org</a> is one such JSON Schema library. How does zod compare to this?
评论 #44031274 未加载
评论 #44031328 未加载
评论 #44032191 未加载
评论 #44033265 未加载
punkpeye2 days ago
Shocked by the negativity around this.<p>I tested early versions of zod v4 and liked the new API, but was very concerned about what will be the migration path. I was even going to suggest to publish under a new package name.<p>But the author&#x27;s approach is ingenious. It allows for someone like me to start adopting v4 immediately without waiting for every dependency to update.<p>Well done!
评论 #44034547 未加载
labadal2 days ago
I just got started working Zod into a new project. This could not have happened at a better time. I would have needed to change so much ot migrate to v4 based on what I&#x27;m seeing.
pipallweek1 day ago
The versioning approach here is a really interesting compromise — especially in the npm ecosystem where breaking changes ripple out so painfully through transitive dependencies. From a developer ergonomics standpoint though, the shift to zod&#x2F;v4 imports will definitely create some friction. For teams with IDEs auto-importing from &#x27;zod&#x27; and linters enforcing import styles, it might introduce subtle DX issues until workflows are adjusted.<p>That said, the strategy does seem to prioritize ecosystem stability over short-term convenience, which is fair. Would love to see better tooling (maybe even IDE plugins or codemods) to help projects transition cleanly. Really appreciate the thoughtful design behind all of this.
neilpa2 days ago
We&#x27;re currently evaluating both Zod and ArkType as a replacement for earlier JSON schema validations. The thing I can&#x27;t get over with Zod is the syntax is _so_ different from defining TS types.<p>Are there reasons to go with Zod over ArkType?
评论 #44031049 未加载
评论 #44031099 未加载
评论 #44031212 未加载
评论 #44031154 未加载
mac92 days ago
The little animation that highlights what section you are reading currently is beautiful
评论 #44053722 未加载
评论 #44045408 未加载
sroussey2 days ago
Hopefully someone can compare Zod4 to TypeBox.<p>Last I looked, the nice thing about TypeBox was that is _was_ JsonSchema just typed which was nice for interoperability.
评论 #44038913 未加载
90s_dev2 days ago
&gt; TypeScript-first schema validation with static type inference<p>Is there a comparison guide? Never heard of this before, but I used io-ts and ajv.
CGamesPlay2 days ago
One decision that seems dubious to me is the zod&#x2F;v4-mini import. I suspect that this will actually <i>increase</i> bundle sizes across the ecosystem. The docs specifically say &quot;zod&#x2F;v4 is still recommended for the majority of use cases&quot;, so application developers will use it. Library authors will think to themselves, &quot;but I want to enable my library to be used by those with uncommonly strict bundle size requirements&quot;, and so will use that. The net result is that both zod&#x2F;v4 and zod&#x2F;v4-mini will be included in the application bundle.<p>I guess this could be mostly avoided if zod&#x2F;v4 actually a wrapper around zod&#x2F;v4-mini. Is that the case?
评论 #44039116 未加载
aduffy2 days ago
Seeing v4-mini reminds me of an LLM. I can only assume the next major release will be zod-3.9-medium-high
评论 #44032345 未加载
indigovole2 days ago
With yet another exciting new release of something reaching the top of HN, I would just like to urge devs to put a description of the project they&#x27;re actually releasing and a link to the page describing the project in the release announcement.<p>These announcements could be a valuable touchpoint for you to reach a whole new audience, but I can&#x27;t remember a single one that starts with something like &quot;exciting new release of NAME, the X that does Y for users of Z. Check out the project home page at <a href="https:&#x2F;&#x2F;" rel="nofollow">https:&#x2F;&#x2F;</a> for more.&quot;<p>Quite often, the release announcement is a dead end that can&#x27;t even take me to the project! In this case, the only link is a tiny octocat in the lower left-hand corner, AFAICS.
评论 #44032821 未加载
jasonthorsness2 days ago
But how is the below possible - doesn&#x27;t it need to include most of the TypeScript compiler? Does it compile the type definitions to some kind of validation structure (like how a compiled regex works) and then use just that?<p><pre><code> - Zero external dependencies - Works in Node.js and all modern browsers - Tiny: 2kb core bundle (gzipped)</code></pre>
评论 #44031512 未加载
评论 #44031630 未加载
__natty__2 days ago
Great job! I love support of json conversion you built-in. However, I find top level string bad choice as it was more verbose before, to have them under string() namespace.
MobiusHorizons2 days ago
The website fails to load with a javascript error in Safari.<p>`SyntaxError: Invalid regular expression: invalid group specifier name`
评论 #44040691 未加载
msukkarieh2 days ago
We&#x27;ve been using zod for our open source tool and are loving it! Will definitely try to migrate over to v4 soon, thanks for the project!
nikolay2 days ago
Zod and the way to define schemas with it has been one of the reasons I didn&#x27;t want to touch TypeScript as it&#x27;s becoming more prevalent!
评论 #44031933 未加载
mrwww2 days ago
Defining schemas in zod and using z.infer&lt;&gt; to get my types is just lovely, thank you!
LMMojo2 days ago
All that text. So, so much text and not one single (maybe there was, but I fell asleep), not one single &quot;Let me tell you what Zod is&quot; paragraph. Lots of &quot;Zod 4 is better than Zod 3&quot; and &quot;Here&#x27;s what we pulled back, out, in, from Zod 2&quot;.
评论 #44037429 未加载
评论 #44037448 未加载
评论 #44037546 未加载
bobbyraduloff2 days ago
great job on zod. it’s an incredible library. also really excited about the locale errors. finally I can push the zod errors directly to the frontend. would love to take responsibility for the BG locale.
h1fra2 days ago
Impressive results, we need some deep dive on the performance improvements!
s-mon2 days ago
Who else here is going to the Zod meetup tonight?
ar-nelson2 days ago
Obligatory shameless plug whenever Zod is posted: if you want similar, but much more minimal schema validation at runtime, with a JSON representation, try Spartan Schema: <a href="https:&#x2F;&#x2F;github.com&#x2F;ar-nelson&#x2F;spartan-schema">https:&#x2F;&#x2F;github.com&#x2F;ar-nelson&#x2F;spartan-schema</a>
评论 #44031122 未加载
assimpleaspossi1 day ago
Wow! This is great!!!<p>What is it?
评论 #44047341 未加载
dankobgd2 days ago
i would use it if there was just 1 normal zod lib not like how it is now: zod and zod mini and who knows what. I rather use arktype or valibot if i am in browser only.
slackfan1 day ago
hmmm. Zod on Zed?
_tqr32 days ago
Still no hapi support?
revskill2 days ago
Mind blowing.
baalimago2 days ago
I read this, and I find myself not wanting to migrate since I know that LLMs can&#x27;t generate Zod 4 syntax yet.
devin2 days ago
Seeing an announcement for a new version of some typescript library I&#x27;ve never heard of shoot to the top of the front page makes me feel extremely out of touch with mainstream dev.
评论 #44031073 未加载
评论 #44031185 未加载
评论 #44033560 未加载
cluckindan2 days ago
Between GraphQL and Zod, there really is no comparison. The latter feels clunky and superfluous by design when the former ecosystem can smoothly provide static TS typings without extra work or schema duplication.
评论 #44031001 未加载
评论 #44031418 未加载
评论 #44034706 未加载