How does ZOD work? Build it yourself!

  Рет қаралды 11,502

Andrew Burgess

Andrew Burgess

Жыл бұрын

Zod may be the best TypeScript schema validation library, and it offers both runtime and compile time type checks! Let's try to build it ourselves to get an idea for how it might work.
My Blog: shaky.sh
My Coding Setup: shaky.sh/tools/

Пікірлер: 32
@vendiddy
@vendiddy Ай бұрын
This was very illuminating! In a similar vein I'd love to understand how something like ts-pattern works under the hood. Like how to implement a mini pattern-matching library.
@dinckelman
@dinckelman Жыл бұрын
I decided to mess around with Zod in a test project, and it seemed very convenient, at least at first glance. If this ends up working out for what I need it to do, I might convert my entire recent project to it. It might fix a couple inconvenient bugs there
@fagnersales532
@fagnersales532 Жыл бұрын
Before knowing abohut Zod, I was used to validate everything by myself, then I tried Zod in a test project and I can tell, it is very easy to grow with zod, the difficulty doesn't increase together, it feels you're just creating Types/Interfaces in a more security way, I really recommend you to do that.
@dinckelman
@dinckelman Жыл бұрын
@@fagnersales532 strict types is basically the whole point of my project so if I can close a few gaps for what I couldn't do natively, it'll be awesome
@janithrandeniya4504
@janithrandeniya4504 Жыл бұрын
This was great - thank you!
@sankalpmukim1052
@sankalpmukim1052 Жыл бұрын
How does Nest's Dependency Injection and some other cool features work together? They use extremely good separation of concerns by using modules for everything. Any kind of under the hood analysis will be really cool!
@seatube327
@seatube327 Жыл бұрын
Great video! I'd suggest returning the parsed value of arg from the validate function so that it strips out properties that were not defined in the schema like zod does.
@-ion
@-ion Жыл бұрын
Zod's philosophy of "parse, don't validate" naturally leads to that being the output.
@avimehenwal
@avimehenwal Жыл бұрын
Great video, I am interesting to know how JSON object config parsing libraries work with big complex objects which multiple optional and default properties
@AngelHdzMultimedia
@AngelHdzMultimedia Жыл бұрын
Nice channel! Isn't it `arg as keyof S` in line 42 instead of any? Thank you!
@-ion
@-ion Жыл бұрын
Zod uses "parse, don't validate" as a tagline. Just validating and then asserting a type leaves some room for logic errors in the validator. Actually having to construct a value corresponding to the parser's return type makes the code more robust against such errors.
@awekeningbro1207
@awekeningbro1207 Жыл бұрын
when I create the assertion function like you did, I get an error - 'val' is not defined. on the line 'asserts val is a string' . Why?
@andrew-burgess
@andrew-burgess Жыл бұрын
Make sure that you use the same name as the argument of the assertion function. So function assertsString(val: unknown): asserts val is string {...}
@nielsgregersen1351
@nielsgregersen1351 Жыл бұрын
It's kind of strange that typescript isn't able to make the connection between : 'asserts value is string' and 'if (typeof value !== "string") throw' since it knows what both statements do individually
@wlockuz4467
@wlockuz4467 Жыл бұрын
I have ran into this a bunch. I had to put a return in the same block after throwing an error so after that block TypeScript would give me the correct type.
@-ion
@-ion Жыл бұрын
You can return an actual string rather than a disconnected type assertion, turning it from a validator into a parser. This is what Zod does.
@brielov
@brielov Жыл бұрын
I use my own implementation (:
@vimhub
@vimhub Жыл бұрын
you can use the "satisfy" correct ?
@andrew-burgess
@andrew-burgess Жыл бұрын
Hmm, probably! What do you have in mind?
@munzamt
@munzamt Жыл бұрын
How recoil reaches fine grained update? How jest is working (except especially)?
@wlockuz4467
@wlockuz4467 Жыл бұрын
+1
@wertrager
@wertrager Жыл бұрын
Universal quantification
@fedordostoevskiy4209
@fedordostoevskiy4209 Жыл бұрын
👀👍👍
@kevinandeleven
@kevinandeleven Жыл бұрын
Too late, already built mine in January 😂.. Name on npm is valleyed
@kevinandeleven
@kevinandeleven Жыл бұрын
Actually, it was a validation library I wrote a few years back to use in all my projects, back and frontend, and it had its unique api, so when I ran into zod in January and liked it's api design, I decided to add a similar api into my library
@ChristophVonBagel
@ChristophVonBagel Жыл бұрын
You will bow to Zod lol
@vukkulvar9769
@vukkulvar9769 11 ай бұрын
I dislike how Zod does things backwards. Zod should enforce validation to follow a type, rather than making up a type from validation.
@kardashevr
@kardashevr Жыл бұрын
how are you so condescending and not condescending at all at the same time?
@andrew-burgess
@andrew-burgess Жыл бұрын
Lol I hope that’s a good thing? I just try to explain it how I’d want to hear it if I was learning it for the first time.
@helsontaveras7961
@helsontaveras7961 Жыл бұрын
he's not condescending at all... he's a very patient and thorough teacher.
@vanjazed7021
@vanjazed7021 Жыл бұрын
How is he condencending? Oh, you mean the school teacher like tone of voice?
هو يعني اي ضباط متخصصين شرطة او حربية ؟
2:35
Nour Malk - نور مالك
Рет қаралды 27 М.
Купили айфон для собачки #shorts #iribaby
00:31
В ДЕТСТВЕ СТРОИШЬ ДОМ ПОД СТОЛОМ
00:17
SIDELNIKOVVV
Рет қаралды 3,9 МЛН
1 класс vs 11 класс  (игрушка)
00:30
БЕРТ
Рет қаралды 3,6 МЛН
5 Tips for Classier TypeScript
9:01
Andrew Burgess
Рет қаралды 4,7 М.
how to get better at typescript (feat. kysely)
16:57
Andrew Burgess
Рет қаралды 7 М.
will i never understand this? unknown.
12:05
Andrew Burgess
Рет қаралды 3,1 М.
Infer is easier than you think
13:38
Matt Pocock
Рет қаралды 85 М.
Svelte Broke Up With TypeScript??!
7:11
Theo - t3․gg
Рет қаралды 69 М.
ARRAYLIST VS LINKEDLIST
21:20
Core Dumped
Рет қаралды 50 М.
Zod's Object Parsing: A Game Changer for TypeScript
10:19
Josh tried coding
Рет қаралды 8 М.
Generics: The most intimidating TypeScript feature
18:19
Matt Pocock
Рет қаралды 163 М.
Купил этот ваш VR.
37:21
Ремонтяш
Рет қаралды 231 М.
TOP-18 ФИШЕК iOS 18
17:09
Wylsacom
Рет қаралды 721 М.
ТОП-5 культовых телефонов‼️
1:00
Pedant.ru
Рет қаралды 15 М.
i like you subscriber ♥️♥️ #trending #iphone #apple #iphonefold
0:14
Дени против умной колонки😁
0:40
Deni & Mani
Рет қаралды 12 МЛН