Overview
Zod provides two functions for URL validation:z.url()- Validates any URL with a protocolz.httpUrl()- Validates HTTP/HTTPS URLs specifically
~/workspace/source/packages/zod/src/v4/classic/schemas.ts:513-523
z.url()
Validates URLs with any protocol scheme.Basic Usage
Valid URL Examples
Invalid URL Examples
Custom Error Message
Parameters
string: Custom error messageURLParams: Object with:message?: string: Custom error messageprotocol?: RegExp: Custom protocol validation patternhostname?: RegExp: Custom hostname validation pattern
z.httpUrl()
Validates URLs with HTTP or HTTPS protocol specifically.Basic Usage
Valid Examples
Custom Error Message
The URL validator preserves the original input exactly as provided. It does not normalize, encode, or modify the URL string in any way.
Examples
API Endpoint Validation
Website URL with Additional Constraints
Form Validation
URL with Query Parameters
Return Type
Returns aZodURL schema that validates and returns strings.
Related Methods
- z.string() - Base string validation
- z.email() - Email validation