Skip to main content

Overview

Zod provides two functions for URL validation:
  • z.url() - Validates any URL with a protocol
  • z.httpUrl() - Validates HTTP/HTTPS URLs specifically
Location in source: ~/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 message
  • URLParams: Object with:
    • message?: string: Custom error message
    • protocol?: RegExp: Custom protocol validation pattern
    • hostname?: 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 a ZodURL schema that validates and returns strings.
The URL validator checks if the string is a valid URL format but does not verify that the URL is accessible or that the domain exists.