Revision

Data Validation

We learned how we can define a Zod schema for our data, from primitive types to complex object, and array types. We then used these schemas to validate the integrity of our data.

Type Inference

We learned how we can automatically infer the type of the data from the Schema we defined. Schema.parse already returns a typed(and transformed) object. Moreover we can use Zod.infer type helper to manually extract the type from the schema.

Transformation

We learned how we can transform our data from one datatype to another by defining transformations within the schema. We also observed how the type inferrence accounted for this transformed type.

Custom Validation Logic

We learned how we can implement our custom validators using the schema.refine method.

Schema Manipulation Methods

We looked at various methods which can be used to manipulate Zod schemas.

  • .pick : extract specified schemas from an object schema.
  • .omit : exclude specified schemas from an object schema.
  • .merge : merge two object schemas.
  • .optional : make a schema optional.
  • .default : provide a default value.

Error Handling

We looked at various ways to handle errors that may occur if validation fails.