API Reference
requestSchema
Creates a comprehensive Zod schema that validates a Request object, including URL, search params, body, headers, method, mode, protocol, hostname, and pathname.
Signature
function requestSchema<
TSearchParams extends z.ZodTypeAny | undefined = undefined,
TBody extends z.ZodTypeAny | undefined = undefined,
THeaders extends z.ZodTypeAny | undefined = undefined,
TMethod extends HttpMethodSchema | undefined = undefined,
TMode extends RequestModeSchema | undefined = undefined,
TProtocol extends ProtocolSchema | undefined = undefined,
THostname extends z.ZodTypeAny | undefined = undefined,
TPathname extends z.ZodTypeAny | undefined = undefined
>({
searchParams,
body,
headers,
method,
mode,
protocol,
hostname,
pathname,
}: {
searchParams?: TSearchParams;
body?: TBody;
headers?: THeaders;
method?: TMethod;
mode?: TMode;
protocol?: TProtocol;
hostname?: THostname;
pathname?: TPathname;
}): z.ZodType<ValidatedRequest>;Parameters
All parameters are optional:
searchParams: Schema for validating URL search parameters (created withsearchParamsSchema)body: Schema for validating request body (created withbodySchema)headers: Schema for validating request headers (created withheadersSchema)method: Schema for validating HTTP method (created withhttpMethodSchemaorhttpMethodEnumSchema)mode: Schema for validating Request mode (created withrequestModeSchemaorrequestModeEnumSchema)protocol: Schema for validating URL protocol (created withprotocolSchemaorprotocolEnumSchema)hostname: Schema for validating URL hostname (any string schema)pathname: Schema for validating URL pathname (any string schema)
Returns
A preprocessed Zod schema that validates the entire Request object. The validated result includes:
url: The parsed URL objectsearchParamsObject: Validated search parameters (ifsearchParamsprovided)body: The original request body stream (ifbodyprovided)bodyObject: The validated body object for direct property access (ifbodyprovided)headers: The original Headers object (ifheadersprovided)headersObj: Validated headers (ifheadersprovided)method: Validated HTTP method (ifmethodprovided)mode: Validated Request mode (ifmodeprovided)protocol: Validated protocol (ifprotocolprovided)hostname: Validated hostname (ifhostnameprovided)pathname: Validated pathname (ifpathnameprovided)
Basic Usage
import {
requestSchema,
searchParamsSchema,
bodySchema,
} from "@nicnocquee/zod-request";
import { z } from "zod";
const schema = requestSchema({
searchParams: searchParamsSchema(
z.object({
page: z.string().optional(),
})
),
body: bodySchema({
json: z.object({
name: z.string(),
}),
}),
});
export async function POST(request: Request) {
const validated = await schema.parseAsync(request);
const page = validated.searchParamsObject?.page;
const name = validated.bodyObject?.name;
const url = validated.url; // URL object
}Complete Example
import {
requestSchema,
searchParamsSchema,
bodySchema,
headersSchema,
httpMethodSchema,
protocolSchema,
} from "@nicnocquee/zod-request";
import { z } from "zod";
const schema = requestSchema({
searchParams: searchParamsSchema(
z.object({
id: z.string(),
})
),
body: bodySchema({
json: z.object({
data: z.string(),
}),
}),
headers: headersSchema(
z.object({
authorization: z.string(),
})
),
method: httpMethodSchema("POST"),
protocol: protocolSchema("https"),
hostname: z.string().includes("example.com"),
pathname: z.string().startsWith("/api"),
});
export async function POST(request: Request) {
const validated = await schema.parseAsync(request);
// All validated properties are type-safe
const id = validated.searchParamsObject.id; // string
const data = validated.bodyObject.data; // string
const auth = validated.headersObj.authorization; // string
const method = validated.method; // "POST"
const protocol = validated.protocol; // "https"
const hostname = validated.hostname; // string (validated to include "example.com")
const pathname = validated.pathname; // string (validated to start with "/api")
}Type Safety
The return type is automatically inferred based on which schemas you provide. Properties are only available if their corresponding schema is provided:
const schema = requestSchema({
searchParams: searchParamsSchema(z.object({ id: z.string() })),
// body not provided
});
const validated = await schema.parseAsync(request);
validated.searchParamsObject; // ✅ Available
validated.bodyObject; // ❌ TypeScript error: Property 'bodyObject' does not existError Handling
The schema throws a ZodError if validation fails:
try {
const validated = await schema.parseAsync(request);
} catch (error) {
if (error instanceof z.ZodError) {
// Handle validation errors
console.error(error.errors);
}
}See Also
- Usage Guides - Detailed guides for each validation type
searchParamsSchema- Validate search parametersbodySchema- Validate request bodyheadersSchema- Validate headershttpMethodSchema- Validate HTTP methodrequestModeSchema- Validate Request modeprotocolSchema- Validate protocolhostname- Validate hostnamepathname- Validate pathname