Why I use Zod with Server Actions

  Рет қаралды 12,241

Ryan Toronto

Ryan Toronto

Күн бұрын

In this video we're going to take a look at how Zod makes FormData easy to work with!
Check out my Build UI course on React Server Components: buildui.com/courses/react-ser...
- 0:00 - Intro
- 0:23 - Introduction to Server Actions
- 2:58 - Problems with FormData and TypeScript
- 6:14 - Introduction to Zod
- 10:17 - Dealing with Checkboxes
- 12:35 - Code cleanup
- 13:42 - Summary
- 14:38 - Outro

Пікірлер: 34
@zeynalabdinqdirov2386
@zeynalabdinqdirov2386 Ай бұрын
You can put a + in front of the value to convert the form.id to a string. Exp: +form.id. It will be shorter.🙂
@n3uron_bloom938
@n3uron_bloom938 11 ай бұрын
Great video, clear and comprehensive. Thanks!
@jitx2797
@jitx2797 11 ай бұрын
Awesome content Ryan. Please bring more of these. Simply amazing content.
@RyanToronto
@RyanToronto 11 ай бұрын
Thanks so much!
@futuremoe
@futuremoe 11 ай бұрын
Awesome video! Working with server actions has been refreshing. I think it's important to note that Zod will throw Errors which will shut down the Node process if they make it to the main event loop. You can prevent this by wrapping dot parse in try/catch or calling the server action with a then/catch chain. This can get pretty verbose. I prefer to use dot safeParse. You can destructure { success, data, error } and handle success and failure accordingly. This makes it convenient to validate forms with Zod and display error messages you've defined in the schema.
@RyanToronto
@RyanToronto 11 ай бұрын
Awesome, great advice!
@codefixlabs
@codefixlabs 11 ай бұрын
Awesome video!
@prashlovessamosa
@prashlovessamosa 10 ай бұрын
Great explanation thanks for making this.
@RyanToronto
@RyanToronto 10 ай бұрын
Thank you!
@wassup102
@wassup102 7 ай бұрын
awesome, code is so cleannnn
@luke_bennett_
@luke_bennett_ Жыл бұрын
If you use react-hook-form with the zod resolver you can share the schema on the frontend and backend.
@RyanToronto
@RyanToronto 11 ай бұрын
Awesome! That's great to know
@roguesherlock
@roguesherlock 11 ай бұрын
I'm not sure if we can do it in prisma but in drizzle orm you can create zod schema from your models. Add in react hook form and you've basically got end-to-end type safety with your db model as the single source of truth!
@jpkiser5051
@jpkiser5051 11 ай бұрын
@@roguesherlock I recommend that you don’t do this. It’s really important when building scalable applications to create an abstraction layer (your business logic) between your client and your database. You should be able to make changes to your database without touching your client.
@roguesherlock
@roguesherlock 11 ай бұрын
@@jpkiser5051 yeah probably. It's mostly for small stuff where you have a tight control over your inputs and outputs.
@RyanToronto
@RyanToronto 11 ай бұрын
That's awesome. I really need to give drizzle a try, how are you liking it? One thing about defining the schema in the action (vs using the database generated zod schema), is that you might have a different form for admins vs non-admins, so you'd want a more restrictive schema for the non-admins. Though having an auto generated schema that you can quickly get access to sounds pretty awesome. I think my next project will use drizzle :)
@gosnooky
@gosnooky 5 ай бұрын
One small optimization I'd recommend is to place the definition of the Zod schema outside of that function so memory doesn't need to be reallocated each time the function is called, since the schema will never change over time.
@jacerchetoui709
@jacerchetoui709 4 ай бұрын
Thank you so much
@hassanallen1823
@hassanallen1823 11 ай бұрын
either use try catch or use safeparse incase if empty input avoid page error. nice video 3>
@KoenVerheyen
@KoenVerheyen 10 ай бұрын
Are you using a specific UI library? The loading button is cool!
@RyanToronto
@RyanToronto 10 ай бұрын
Hi @KoenVerheyen! No specific UI library, but I do use Tailwind to style everything. Here's a twitter thread on how I built the submit button: twitter.com/ryantotweets/status/1676625909351153671
@Shawn-Mosher
@Shawn-Mosher 11 ай бұрын
How do you show errors to the UI so the users can fix?
@RyanToronto
@RyanToronto 11 ай бұрын
That's a topic deserves a whole other video :) Basic idea you pass the server action into a client component that invokes the action and keeps track of any errors it returns. If it errors then the client component can set state displaying those errors.
@Shawn-Mosher
@Shawn-Mosher 11 ай бұрын
@@RyanToronto that sounds like a great follow up video as I’m trying to wrap my head around it! Thanks!!
@akosbalint3485
@akosbalint3485 11 ай бұрын
I think you can parse the id as number directly with zod.
@filipjnc
@filipjnc 11 ай бұрын
z.coerce.number()
@ExpertGamingOnline
@ExpertGamingOnline 9 ай бұрын
What i am missing, and was hoping to see in this video, is error handling with zod and displaying the error messages client side.
@RyanToronto
@RyanToronto 9 ай бұрын
That's coming up in a future video!
@nathanguedes7726
@nathanguedes7726 11 ай бұрын
Mano tu parece o Jim do the office tlg?
@Notoriousjunior374
@Notoriousjunior374 11 ай бұрын
Doing db operations without any proxy is hella raw and insecure. Why not use a rest endpoint to handle all that stuff?
@tiagoagm
@tiagoagm 8 ай бұрын
How about validations with zod?
Zod Goes Where TypeScript Can't
8:11
Theo - t3․gg
Рет қаралды 54 М.
MEU IRMÃO FICOU FAMOSO
00:52
Matheus Kriwat
Рет қаралды 29 МЛН
🌊Насколько Глубокий Океан ? #shorts
00:42
Stupid Barry Find Mellstroy in Escape From Prison Challenge
00:29
Garri Creative
Рет қаралды 20 МЛН
Next 14 Reveals React's Taint, Solves RSC Safety Issues
15:22
Theo - t3․gg
Рет қаралды 56 М.
Learn Zod In 30 Minutes
31:03
Web Dev Simplified
Рет қаралды 134 М.
React's new cache function
10:14
Ryan Toronto
Рет қаралды 8 М.
Using Forms in Next.js (Server Actions, Revalidating Data)
10:27
Enums considered harmful
9:23
Matt Pocock
Рет қаралды 195 М.
The Truth About React Server Components
26:33
Theo - t3․gg
Рет қаралды 44 М.
File uploads in Next JS 14 just got SO MUCH EASIER!!
12:24
WebDevEducation
Рет қаралды 15 М.
My Problem with Next.js Server Actions
10:13
James Q Quick
Рет қаралды 15 М.
MEU IRMÃO FICOU FAMOSO
00:52
Matheus Kriwat
Рет қаралды 29 МЛН