Zod RequestZod Request
API Reference

headersSchema

Creates a Zod schema that validates the headers of a request.

Signature

function headersSchema<T extends z.ZodRawShape>(
  schema: z.ZodObject<T>
): z.ZodType<z.infer<z.ZodObject<T>>>;

Parameters

  • schema (required): A Zod object schema defining the expected headers

Returns

A preprocessed Zod schema that validates headers from a request.

Basic Usage

import { headersSchema, requestSchema } from "@nicnocquee/zod-request";
import { z } from "zod";

const headers = headersSchema(
  z.object({
    authorization: z.string(),
    "content-type": z.string().optional(),
  })
);

export async function GET(request: Request) {
  const validated = await requestSchema({
    headers,
  }).parseAsync(request);

  const auth = validated.headersObj?.authorization; // string
  const contentType = validated.headersObj?.["content-type"]; // string | undefined
}

Optional Headers

Use .optional() to make headers optional:

const headers = headersSchema(
  z.object({
    authorization: z.string(),
    "x-custom-header": z.string().optional(),
  })
);

Header Names

Header names are case-insensitive in HTTP, but the schema uses the exact keys you provide. Access headers using the same keys:

const headers = headersSchema(
  z.object({
    "Content-Type": z.string(),
  })
);

// Access with the same key
const contentType = validated.headersObj?.["Content-Type"];

Behavior

  • Converts Headers or Headers-like objects to a plain object
  • Returns undefined for missing optional headers
  • Only extracts headers defined in the schema

See Also