The best TypeScript library just got better

preview_player
Показать описание
Zod is an extraordinary library for TypeScript validation, and with 3.20 it just got even better.

Become a TypeScript Wizard with Matt's upcoming TypeScript Course:

Follow Matt on Twitter

Рекомендации по теме
Комментарии
Автор

Thanks Matt! Incredible as always!

Florian mentioned a couple uses cases for z.symbol() below. I've also not used it much personally, but it always irked me as an inconsistency between Zod and TS. Symbols are one of three kinds of primitive values that can be used as keys in JS objects so it was particularly odd that Zod didn't represent them in any way:

type anyKey = keyof any; // string | number | symbol

Though `z.object` doesn't actually support them yet as keys...something for a future release!


const matt = Symbol("mattpocock");
const schema = z.object({
[matt]: z.string().catch("cool dude"),
});

ColinMcDonnellCorner
Автор

These are friggin awesome changes. The functional programmer deep inside of me feels satisfied. I honestly don't know which one to be the most hyped for.

jon
Автор

One of my favorites libraries is ts-pattern. Worth a look!

irlshrek
Автор

always good to hear new updates from matt channel <3

krome
Автор

I'm not a huge fan of zod doing things like transforming the data with fallbacks etc. To me it's a validation library, which gives me a clear mental model about what it does and what it returns (tells me if the data is valid or not). I feel like adding bells and whistles on for "convenience features" has a way of gradually polluting very simple concept libraries into grab-bag hodge podge frameworks. Hope they can keep it under control because zod is one of my favorite libraries

MachineYearning
Автор

The lack of composability was my biggest annoyance vs io-ts - glad to see the pipe function!

ulterior_web
Автор

I started using Zod for the first time literally this weekend, and one of the things I wanted to do was parse a stringified `DateOnly`(C#) value ("2022-12-15"), and I was surprised to found out I had to do it manually. Good to see it's now been implemented (Albeit seemingly requiring some semi-arbitrary precision option?)

modernkennnern
Автор

Thanks, Matt, for the amazing video. I didn't know about transform function.

JacksonMarshal
Автор

5:00 I’ve used Symbols in zod for some shared form prop validation in vue where some fields didn’t support specific props. Due to Vue’s lack of real TS support, it needs to be done at runtime unfortunately, so those props default to Symbol(NEVER) and the runtime validation only passes when that specific symbol is still passed (so, when nothing else is passed to the prop).

This ensures devs don’t ever pass props that aren’t supported which is misleading otherwise

FlorianWendelborn
Автор

Really liked the video, just one point of feedback. Using inline snapshots to validate the result is a bit confusing because it appears as if everything you parse is a string while it’s not.

d.sherman
Автор

2:15 I’ve previously used z.preprocess for similar things. I think the pipe example isn’t great since I see no reason to use pipe over preprocess just from the example.

FlorianWendelborn
Автор

Wow, this is really cool, i have so many problem working with query param for paging due to query param has to be string but paging needs number, so i have to write a lot of parseint and toString to solve this

anhdunghisinh
Автор

I immediatly remembered a usecase for coerce. I have a yaml file with some numeric IDs mapping release dates of something, which is either an Enum (for reuseable dates) or a number. I've had problems where zod saw the keys (my numeric IDs) as string, but I really wanted a Record<number, number> in the end.

Edit: here's my current workaround: => +val)

Mitsunee_
Автор

Zod has one of the coolest logos in open source.

GordonChil
Автор

At 3:00 when you are changing the input to parse() how are you getting those dynamic updates in the test? Video edits? If not this is a tool I need in my toolbox (that I’m missing).😢

JayFGrissom
Автор

The first code is good for setting and checking the length of the password for the use case.

kuldeepbhatt
Автор

Hmm, I just keep waiting for an excuse to switch from Joi to Zod (because a lot of people seems to go on about it), but that first example is actually super easy and intuitive in Joi 🤷..

rujor
Автор

The only thing where symbol would make sense if you would like to validate that an object is iterable (because then it has to support Symbol.iterable key). Maybe there is a use case for that.
Date/time support is great. Does this enable transforming from string to date? So if I have const schema = z.object({ value: z.datetime().transform(d=>new Date(d)) }) and I use it will it then return an object where 'value' has been converted from string to javascript Date?

Автор

Which one do you suggest to use? Joi or zod? especially for nestjs.

darkdande
Автор

Nice review :) what abou error friendly ? I use Boom but we have some alternative? Thanks

carlosricardoziegler