Syntax Video
You Should Try zod v4
Intro
Zod is a schema validation library that I use in pretty much every single one of my JavaScript projects and they just released stable version 4 and it's all good things. They've sped up parsing by up to seven times in some cases. They've reduced their bundle size in half and as a part of this release they've closed nine of the most upvoted issues on GitHub. Now the migration path to V4 is very seamless. If you're currently on V3 and if you have version 3.25 25 of ZOD installed, you can already start using some of these new features because V4 is actually just included as a subpath in version 3.25. Now, in this video, I'm going to show you how I'm using Zod V4 in my projects and show you how it can actually clean up and reduce the amount of code that you're writing in a lot of cases. If that sounds good to you, let's dive in. My name is CJ. Welcome to Syntax. Now, the first big feature that I am really excited about is error pretty builtin. I cannot count the number of times where I've written a
Error pretty-printing
custom function to reconfigure the ZOD errors that are being output or installed the ZOD validation error package which has some of those things built in. But now built into ZOD is Z.pritify error and it gives you a nice readable error that you can use on your front end or as output in the console if you're doing like validation of config files and you don't have to do all of that manually or install a separate package. Now, now one example where you might use this is if you want to send a readable ZOD error back to your client.
Now, this app that I'm demonstrating here has client side validation and server side validation, but I disabled the client side validation so that the error can be returned from the backend. And so, if I try to submit this form without filling anything in, you can see here that this is the formatted error that was returned from the backend. Now, to do this, I wrote some custom code, right? I'm iterating over all of the Zod issues, grabbing their path and the error message and then putting that all back together into a single string. But now with V4, you can do this in a single method call. So if you instead use Z.printitify error and then pass that error in, that will automatically make a nice human readable error out of that zod error object. So now if I submit this to my backend, you can see this is nice and human readable. Now, another thing my backend is doing here is returning an error object that can automatically be passed into the front end to show those error messages on the forms themselves.
And to do this, I have some very manual code. So, I am reducing over the issues for each one. I'm grabbing its path and then creating a record that has the path name colon the error message. Now, there isn't a new built-in in V4 that does this exactly, but there is one that gets us a lot closer. So if you check out formatting errors in the new docs, there is one called flatten error and it actually gives us these field errors where all of the error messages are arrays. So let's use that directly and see if we can get a similar behavior. So now instead of all this code, I can just flatten the error and then grab the field errors. And you can see that data comes through in exactly the same way.
Now my front end is actually able to handle either a string error message or an array of error messages. So this works just fine without any other updates. But depending on if you were writing some custom code for that or using another library, you might need to make some updates. But with these two new built-in methods, now my code is a whole lot cleaner and I don't have any custom error reformatting code when working with these ZOD errors. Now another spot where I have some custom formatting of the ZOD error is whenever I start up my app. So in this app, I'm using ZOD to have a type- safe env. So essentially when the app starts up we parse process.env to make sure that all of these environment variables exist.
Otherwise the app crashes with a nice readable error message that says hey you're missing these environment variables. Now to do this I have some more custom ugly code for reformatting that error. I iterate over all the issues. Grab the path and then say hey this this is a missing value there. Now with v4 I can just use that built-in flatten error method. Grab all the keys because those are the missing values from my env log those out instead. And so now if I start up my app with some missing values in the env, I can see what those properties are and I didn't have to write a custom reduce to grab all of those out. Now another new feature that was long awaited was the introduction of this string bool type.
Stringbool
So a lot of times maybe when you're building command line tools or when you're reading values in from an env. And this supports all the possible ways that you would want to do that. So you can go from the string true to the value of true or any other truthy like value like one yes on things you commonly might use like in a CLI tool or something like that. And similarly they support all of those same values for false. And where this was useful for me is in a project called bite dash. So this is where I designed a complex schema with drizzle to show how a complex schema might actually look. But one of the things I was doing in this codebase was I had two environment variables called migrating or seating.
And these would be set to true or false in my env. And you can see before I had to come up with a custom coersse and transform to do this. But now I can just use this string bool and it does all the parsing for me. So in myv if I have the word true db migrating will show up as a boolean with the true value whenever I'm reading it in in my application. So this is a tiny change but very useful for cases like this where you're doing that parsing manually. Now one more feature I am very excited about is builtin JSON schema conversion. So now there's this built-in method called Z.2JSON schema.
JSON Schema conversion
You can pass in any ZOD schema and automatically get back a JSON schema. In the past, there are a lot of thirdparty libraries that would do this. But now with this builtin, it's going to be much easier to integrate ZOD with things like open API or other things that are dependent on JSON schema. And that actually could be combined with this other new feature in ZOD called metadata. So they've introduced this new registry where you can register metadata about all of your schemas and then use it somewhere in your app. And where I see this being used is something like open API where you have your schemas defined with zod. You then can convert them with JSON schema and you can use the metadata registry to add all this information that you want to appear in your open API definition. And all of this is fully built into ZOD. Now, if you're currently on V3, you can check out their migration guide to see some of the things that might be deprecated or that you might have to change. Now, I'm not going to go through all of these one by one, but I will say that for the most part, I haven't really run into any of these because a lot of these deprecations are things that people were
Migration Guide
using that were maybe non-standard or there were maybe better ways to do them. So, if you run into any type issues or anything like that, if you start importing from Zod V4, then just take a look at this migration guide and you'll see some alternatives for some of the ways you might be using ZOD right now. Now, there's a ton of other new features in here that I'm not going to go over. I highly recommend that you go try these things out. So, install ZOD version 3.25, head to the docs, and look at all of these new features. And like I mentioned earlier, also check out the migration guide to see where some of these things might have changed. Now, one of the last things is to talk about how they've actually architected this new version of Zod 4. And what they've done is defined a core library, extracted out all of that core functionality, and now that core functionality is shared between V4 and V4 Mini. But what's exciting about this is for library authors. So anyone that's doing some tight integration with Zod, maybe like the Drizzle Zod library, which I use very often when I need to convert my database schemas into ZOD validators, that library is doing some
zod core
very under the hood things in order to generate these ZOD types. So they might actually benefit from pulling in methods directly from core when they need to have more customizations and do things that maybe the library author didn't intend. And they even talk about this here, like if you're building out a schema library, check out how V4 and V4 mini were built to see how all of these things in the core library are actually being used to build out a more fullfeatured library. All right, that's all I have for you in this video, but I highly recommend install Zodv4. Give it a try. Let me know down in the comments if you've come across any migration issues. Let me know if you like some of these newer features. And thank you so much for watching. All right, see you in the next one.
thanks!
Quote it with credit and a link. Quoting Syntax