⋅ 7 min read

Schema validation in TypeScript with Zod

Abhinav Anshul Doing interesting things on the Web.

Recent posts:

8 Tips To Reduce Unused Javascript

8 tips to reduce unused JavaScript

Knowing how to reduce unused JavaScript can help you save time, optimize performance, and improve efficiency.

Abhinav Anshul
Nov 27, 2023 ⋅ 5 min read
Guide to mobile emulator testing with MiniSim

Guide to mobile emulator testing with MiniSim

MiniSim makes virtual emulator testing easy — learn how to use it to test both Android and iOS apps on macOS in this detailed post.

Emmanuel John
Nov 22, 2023 ⋅ 5 min read
Implementing safe, dynamic localization in TypeScript apps

Implementing safe, dynamic localization in TypeScript apps

After internationalization, you can further localize your apps to make user experiences more friendly. Learn how to do this in TypeScript.

Yan Sun
Nov 21, 2023 ⋅ 6 min read
Using Dev Containers In Vs Code For An Easier Dev Setup Process

Using dev containers with VS Code for an easier dev setup

You can leverage containers to streamline the process of setting up a dev environment. Let’s see how using VS Code and Docker.

Yashodhan Joshi
Nov 21, 2023 ⋅ 11 min read
View all posts

3 Replies to "Schema validation in TypeScript with Zod"

  1. Correct me if I am wrong, but you don’t use the parse function with TS typically… you infer the type, effectively from the ZOD ‘model’ (I call it) and then you assign that type to the variable, or function return or whatever as you would usually do with TS. Then parse is done inherently as part of type checking, but standardized with ZOD models.

    Got a huge app, sharing front and backend code, express/vite. Express has routes hierarchical frontend doesn’t, plus has it’s own router. Trying to share route strings mostly and response types. Also trying to figure out naming conventions… ATM I am using SomethingModel for the ZOD stuff and SomethingModelType for the type inferers but its a bit verbose.

  2. I’m searching for good examples of how to name the zod schema objects.
    Unfortunately your examples disappointed me.

    When you ask a programmer what ‘dataInputFromUser’ means they would think it’s a variable that holds the actual data entered by the user at runtime.
    To express the intended meaning ‘dataInputFromUserSchema’ would be more to the point.

    Identifier with upper camel case names (like ‘UserData’) usually denote either a class, interface, enum, global constant or a type. So it’s clear that these variables don’t hold the values itself, but rather describe the allowed/expected value. Hence such a name is not that missleading.
    But typically you also want to have a TS type with the same name (‘UserData’). Unfortunately this w/could lead to a name clash. So ‘UserDataSchema’ seems to be the solution here.

  3. Great Article! One possible error in the article to correct. Under the section Adding a custom validation with Zod:
    .refine((data) => data.mobileNumber === data.confirmMobileNumber
    This should probably be !== in order to trigger the message?

Leave a Reply