add `ZodURL` to validate parts of a url.
See original GitHub issueHey 👋
Currently we got z.string().url()
. It remains a string after the check.
There are more things we could validate in a url, but it’ll feel really awkward to put url-specific methods on ZodString
.
Now that we have z.coerce
& z.pipe
, might be a good idea to add the following options:
z.url(); // native `URL` instance.
z.coerce.url(); // coerce to native `URL` instance.
z.string().transform(value => new URL(value)).pipe(z.url()); // pipe to native `URL` instance for further checks.
and have checks like:
z.url().protocol('https');
z.url().port(1234);
z.url().search({
age: z.coerce.number().int().min(1),
}); // alternatively could be called `searchParams`.
z.url().username('admin');
z.url().password('123456789');
z.url().pathname(/^\/users\/\d+\/settings$/);
// etc. etc..
I’d love to open a PR if this sounds good…
Issue Analytics
- State:
- Created 9 months ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
How to Validate Links Concurrently in Python
The first step is to download a target webpage. There are many ways to download a URL in python. In this case, we...
Read more >How to validate a url in Python? (Malformed or not)
Install it from PyPI with pip ( pip install validators ). ... needs to be present to define the validity of a URL,...
Read more >How to Validate URLs in JavaScript - freeCodeCamp
With this knowledge, you can create a custom function to check the validity of a given URL string. How to Create a URL...
Read more >Secure JavaScript URL validation - Snyk
The easiest way to perform URL validation in JavaScript is by using the new URL constructor function. In addition to its simplicity, it's ......
Read more >URL validation in D7? | Drupal.org
Hey all, Im having a tough time trying to resolve this. I need to validate (check length and verify) that some user input...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve got a pretty good working version of something that can handle searchParams in #1697.
I decided that if the schema is expecting an array then we’ll treat it like an array, but if it’s not it’ll be invalid if there’s more than one value. That seems reasonable to me.
I like a lot of these ideas, but this is a big API surface with a lot of potential headaches.
Some open questions:
Will
.pathname()
,.user()
,.password()
etc. support regexes? Arrays of strings? Zod schemas?If a method exists, people will eventually ask for all of these things. But the end result is a method with a ton of overloads, and the DX starts feeling a little chaotic. There aren’t a lot of methods like that in Zod (by design).
I like
.protocol()
but we may want to steer clear of.username/.password/.port/.pathname
for that reason. Of course its a bit weird that only one property ofURL
would have its own method.The search params stuff is a big can of worms. If the return type of
z.url()
isURL
then how to we return the parsed/typed search params to the user?The result could just be
URL & { params: <inferred search params type> }
?Other
URLSearchParams
things:URLSearchParams
should be considered a multi-value (.getAll()
) or single value (.get()
)?.strict()
and.passthrough()
for the returned search params?URLSearchParams
andFormData
(which are very similar in their API) may get a separate API entirely: https://github.com/colinhacks/zod/issues/1630Sorry, brain dump. There’s a lot to think about here, and we should definitely hash out details before anyone starts on implementation.