📚

Overview of Zod Validation Library

Sep 11, 2024

Lecture on Zod Validation Library

Introduction

  • Speaker: Kyle
  • Platform: Web Dev Simplified
  • Purpose: Simplify usage of Zod, a popular TypeScript-first schema validation library.
  • Features of Zod:
    • Zero dependencies
    • Small library size
    • Immutable-first design
    • Supports functional programming style
    • Excellent TypeScript support
    • Usable in plain JavaScript scenarios

Getting Started with Zod

  • Installation:
    • Use command npm i zod to install
    • Import using import Z from 'zod'
    • Ensure TypeScript strict mode is true

Example Usage

  • Creating a Schema:
    • Use Z.object() to define an object schema
    • Define data types like strings, numbers, booleans
  • Validation:
    • Use schema.parse(data) to validate
    • Error handling if data does not match schema

Advanced TypeScript Integration

  • Inferred Types:
    • Use Z.infer() to infer TypeScript types from the schema
  • Safe Parsing:
    • Use safeParse() for boolean success/failure

Zod Primitive Types

  • Common Types:
    • String, Number, Boolean, BigInt, Date
    • Optional values with .optional()
  • Special Types:
    • Undefined, Null, Void, Any, Unknown, Never

Advanced Validations

  • String:
    • .min(), .max(), .email(), .url() validations
  • Number:
    • .gt(value), .lt(value) for ranges
  • Optional and Default Values:
    • Use .optional(), .nullable(), .nullish()
    • Define default values with .default(value)

Object and Array Types

  • Object Handling:
    • Get shapes with .shape
    • Use .partial(), .pick(), .omit(), .extend(), .merge()
    • Control unknown keys with .passthrough(), .strict()
  • Array Handling:
    • Define using Z.array(elementType)
    • Use .nonempty(), .min(length), .max(length)

Advanced Data Structures

  • Tuples:
    • Fixed-length array with specific types
    • Use .rest() for additional types
  • Union and Discriminated Union:
    • Define types using Z.union([]) or Z.discriminatedUnion()
  • Record and Map:
    • Use Z.record(keyType, valueType)
    • Maps: Z.map(keyType, valueType)

Sets and Promises

  • Set Type:
    • Unique array elements
    • Similar methods to arrays
  • Promise Type:
    • Ensure a promise structure and its return type

Custom Validation

  • Refine Method:
    • Use refine() to add custom validation logic
    • Use superRefine() for advanced control

Error Handling

  • Error Messages:
    • Customize using error map and type errors
    • Use zod-validation-error package for simplified error messages

Conclusion

  • Zod is a powerful library for TypeScript development
  • Provides comprehensive validation tools
  • Integrates deeply with TypeScript for type-safe code
  • Recommended to use additional libraries for better error message handling

Kyle emphasizes Zod's flexibility and its strong integration with TypeScript, making it a valuable tool for developers aiming for robust validation in web applications.