API Reference
protocolSchema
Creates a type-safe Zod literal schema for a specific protocol value.
Signature
function protocolSchema<T extends Protocol>(protocol: T): z.ZodLiteral<T>;Parameters
protocol(required): A valid protocol value ("http"or"https")
Returns
A z.ZodLiteral schema that validates to the exact protocol value provided.
Example
import { protocolSchema, requestSchema } from "@nicnocquee/zod-request";
const schema = requestSchema({
protocol: protocolSchema("https"),
});
export async function GET(request: Request) {
const validated = await schema.parseAsync(request);
// validated.protocol is "https"
}Type Safety
TypeScript will catch invalid protocol values at compile time:
// ✅ Valid - TypeScript accepts this
protocolSchema("https");
// ❌ Invalid - TypeScript error at compile time
protocolSchema("ftp"); // Error: Argument of type '"ftp"' is not assignableRuntime Validation
The function also validates at runtime and throws an error if an invalid protocol is provided:
protocolSchema("invalid"); // Throws: Invalid protocol: "invalid". Must be one of: http, httpsprotocolEnumSchema
A Zod enum schema that validates against all valid protocol values ("http" and "https").
Signature
const protocolEnumSchema: z.ZodEnum<["http", "https"]>;Usage
Use this when you want to accept any valid protocol value:
import { protocolEnumSchema, requestSchema } from "@nicnocquee/zod-request";
const schema = requestSchema({
protocol: protocolEnumSchema,
});
export async function GET(request: Request) {
const validated = await schema.parseAsync(request);
// validated.protocol is "http" | "https"
}When to Use
- Use
protocolEnumSchemawhen you want to accept any valid protocol - Use
protocolSchemawhen you need to validate a specific protocol value