Loading topic
zod installed (npm install zod). The examples work on Zod 3 and Zod 4. Where the two versions differ, the text says so.5 min readz.string() and z.number(). Here you will describe whole objects, including nested ones, and learn how Zod treats keys that y4 min readz.infer removes the second description. You write the schema once and let the compiler derive the type from it.5 min readtry/catch. You should also have a schema like the User object from earlier chapters. This chapter covers the two methods that run a schema against a value: parse and safeParse. It also covers how to c5 min readsafeParse from the previous chapter and can read a TypeScript type. You should also know the difference between an exception and a returned result. After this chapter you will be able to read a Zod error, turn it into messages a user or another program can use, and5 min readz.object schema, call parse and safeParse, and derive a type with z.infer. It also assumes you know that TypeScript treats undefined and null as different types. Real data often leaves a field out, sends null, or sends nothing useful. Zod gives you5 min readz.object, call safeParse, and read error.issues. You also need to know how TypeScript represents arrays, tuples, Record, and string literal unions. Here you will learn the four Zod schemas for collections and fixed sets of values, how e4 min readstring | number), narrowing with if or switch, and the Zod basics from earlier chapters: z.object, z.infer, and safeParse. Real data often has more than one valid shape. A message may carry a string or a number, and an event may be a 5 min readz.object, derive types with z.infer, and call parse and safeParse. It also assumes you know TypeScript's own utility types such as Pick and Partial. Zod offers runtime counterparts of those utilities. They build new schemas from an 5 min readmin, max, email, and regex cover the common cases. Some rules do not fit them: two fields must match, a date range must be ordered, or a value must pass a lookup. refine lets you attach your own rule to any schema.5 min readparse and safeParse, and read z.infer types. It also assumes you know the difference between input and output shapes, though the next chapter treats that difference formally.5 min readparse and safeParse, and read error.issues. It also assumes you know how JavaScript converts values with Number() and new Date().4 min readtypeof. It also assumes you know how parse, safeParse, transform, default, and coerce work from earlier chapters. Examples use Zod 3.x and 4.x unless noted.5 min readparse and safeParse, refinements, transforms, and coercion. This chapter puts them together at the three places where untrusted data enters most applications. It assumes you can write a Zod object schema, call safeParse, and read error.issues.4 min read