Back to Home
    ZodZod v3.22Intermediate

    Zod

    Schema declaration, validation, type inference, transformations, and error handling.

    6 min read
    zodvalidationtypescriptschemas

    Basic Schemas

    1 topic

    Primitive Schemas

    Define and validate basic types

    typescript
    import { z } from "zod";
    
    const stringSchema = z.string();
    const numberSchema = z.number();
    const boolSchema = z.boolean();
    const dateSchema = z.date();
    
    // With validations
    const email = z.string().email();
    const age = z.number().min(0).max(120);
    const name = z.string().min(1).max(100);
    
    // Parse (throws on error)
    const result = email.parse("[email protected]");
    
    // Safe parse (returns result object)
    const safe = email.safeParse("invalid");
    if (!safe.success) console.log(safe.error);

    💡 Use safeParse for graceful error handling

    📌 parse throws ZodError on failure

    Object Schemas

    1 topic

    Object Validation

    Validate complex object structures

    typescript
    const UserSchema = z.object({
      name: z.string(),
      email: z.string().email(),
      age: z.number().optional(),
      role: z.enum(["admin", "user"]),
    });
    
    // Infer TypeScript type
    type User = z.infer<typeof UserSchema>;
    
    // Extend and modify
    const CreateUser = UserSchema.omit({ role: true });
    const UpdateUser = UserSchema.partial();
    const AdminUser = UserSchema.extend({
      permissions: z.array(z.string()),
    });

    💡 z.infer extracts TypeScript types from schemas

    ⚡ Use .partial() for update/patch operations

    Related Articles

    Background reading and deeper explanations for this sheet.

    Evaluation of LLM Systems: 12 Overlooked Metrics That Actually Predict Production Success

    Most LLM teams track accuracy and latency, then wonder why users still churn. This practical guide covers underused evaluation metrics that reveal real-world quality, safety, cost, and reliability issues before they hit production. You’ll get concrete formulas, examples, and implementation patterns you can apply immediately.

    keyword overlap

    FastAPI Interview Questions: Practical Cheat Sheet for Developers

    Preparing for FastAPI interviews can feel overwhelming if you only memorize definitions. This practical cheat sheet helps you answer common FastAPI interview questions with confidence using real-world examples, production-ready patterns, and beginner-friendly explanations with depth.

    keyword overlap

    Intro to FastAPI: Build a Production-Ready Python API with Real Examples

    FastAPI is one of the fastest ways to build modern APIs in Python without sacrificing code quality or developer experience. In this practical, beginner-friendly guide, you’ll learn how to create endpoints, validate data, handle errors, connect a database, secure routes, and prepare your FastAPI app for real-world deployment.

    keyword overlap

    Python Memory Management Cheat Sheet: Refcount, GC, and PyMalloc

    Python memory management combines reference counting, cyclic garbage collection, and a custom allocator that makes object handling fast and predictable. This guide explains how CPython allocates, tracks, reuses, and frees memory so you can diagnose leaks, reduce overhead, and write production-safe code.

    keyword overlap