8000 v4: URL parser does not accept `localhost` as a valid hostname · Issue #4103 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

v4: URL parser does not accept localhost as a valid hostname #4103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Punie opened this issue Apr 11, 2025 · 5 comments
Closed

v4: URL parser does not accept localhost as a valid hostname #4103

Punie opened this issue Apr 11, 2025 · 5 comments

Comments

@Punie
Copy link
Punie commented Apr 11, 2025

I'm parsing a postgres connection URL with the new v4 z.url() schema and it seems to fail locally.

Going a bit deeper in my investigation, I notice that the URL parser here validates the hostname against this regex which doesn't match on localhost.

@Punie
Copy link
Author
Punie commented Apr 12, 2025

BTW, I'm not sure if this is truly considered a bug or if this behavior is expected (I expect it's a bug, given that it breaks with the expectations of zod v3) but if it's the case, I'd be willing to offer a PR if that helps 🙂

@rafaell-lycan
Copy link

Sam issue! I was trying to migrate a tiny app that relies on the old z.string().url() and now I'm getting the error.

@erwin-offshorly
Copy link

not just localhost. in general any hostname that doesn't end with a top-level domain (.tld) is not accepted.

crude workaround:

z.string().refine((val) => {
    try {
      new URL(val)
      return true
    } catch {
      return false
    }
  })

@0xBison
Copy link
0xBison commented May 14, 2025

Also encountering this issue. I made a repository here: https://github.com/0xBison/zod-url-issue with various tests

It also seems to allow urls like: https://example.com/path with spaces which are not valid

@colinhacks
Copy link
Owner
colinhacks commented May 15, 2025

Just merged #4367 which fixes the breakage from Zod 3 and adds some other goodies: optional protocol and hostname regex params. Merged in the latest betas.

Note that you'll need to rewrite some imports after upgrading per #4364

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
0