Skip to content
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

Request to support 'hostname' string validation #3589

Open
manpreet-compro opened this issue Jun 20, 2024 · 5 comments
Open

Request to support 'hostname' string validation #3589

manpreet-compro opened this issue Jun 20, 2024 · 5 comments

Comments

@manpreet-compro
Copy link

Issue:
I am working on a use case where I need to validate hostname strings before passing them to a database. While Zod supports URL validation, I could not find any built-in support for validating hostnames specifically.

For example, I need to validate strings such as:

www.example.com
mail.google.com

Current Workaround:
Currently, I am using a regular expression to validate hostnames. However, having built-in support in Zod for hostname validation would be more efficient and reliable.

Request:
I would like to request the addition of hostname validation support in Zod, this would be beneficial. If this request makes sense, I am willing to contribute to adding this feature to the library.

@m10rten
Copy link
Contributor

m10rten commented Jun 21, 2024

Hi, first of all: I think this idea has potential in being implemented and used widely.

However this can be widely interpreted, hi.how.are.you.google.com is a valid domain, but should it be a valid hostname? Or should only google.com or example.com be valid.

Lets look at mozilla's definition on hostname:

https://hello.world.example.com/ -> url.hostname = hello.world.example.com

But should www also be part of it?

@manpreet-compro
Copy link
Author

manpreet-compro commented Jun 21, 2024

@m10rten Thanks for following up.

I believe the value returned from URL.hostname should be considered a valid hostname. It may or may not include www, depending on the domain, Ref: https://developer.mozilla.org/en-US/docs/Web/API/URL/hostname

const url1 = new URL(
  "https://developer.mozilla.org/en-US/docs/Web/API/URL/hostname"
);
console.log(url1.hostname);  // developer.mozilla.org

const url2 = new URL("https://hello.world.example.com/");
console.log(url2.hostname);  // hello.world.example.com

const url3 = new URL("https://www.google.com/");
console.log(url3.hostname);  // www.google.com

@manpreet-compro
Copy link
Author

I further spent some time checking the RFC specification (https://datatracker.ietf.org/doc/html/rfc3986#autoid-21), As per the specifications, a valid hostname could be

@m10rten
Copy link
Contributor

m10rten commented Jun 21, 2024

Thanks for clearing that up!

Maybe we can work with that, how do you see using it? And what would the result look like?

Or exact like a new URL().hostname?

@manpreet-compro
Copy link
Author

I think it should be used similarly to existing zod string validations, such as email or uuid.
So this will be z.string().hostname().

As for the implementation options:

  • Adding a regex is one option.
  • Another option is to check if a URL can be successfully constructed from the hostname string without throwing any errors. (I haven't tried this yet, so I'm not sure of the correctness of this option but can explore.)

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

2 participants