question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

add `ZodURL` to validate parts of a url.

See original GitHub issue

Hey 👋

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:open
  • Created 9 months ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kentcdoddscommented, Dec 15, 2022

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.

1reaction
colinhackscommented, Dec 13, 2022

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?

z.url().password(z.string().min(10))

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 of URL would have its own method.


The search params stuff is a big can of worms. If the return type of z.url() is URL then how to we return the parsed/typed search params to the user?

const schema = z.url().search({
  names: z.string().transform(val => val.split(","))
});

const result = schema.parse("http://localhost?names=jim,bob");
result; // => URL
result.searchParams; // => URLSearchParams
// how to retrieve the post-parse search params as  { names: string[] }?

The result could just be URL & { params: <inferred search params type> }?

Other URLSearchParams things:

  • Do we let users specify whether a field in the URLSearchParams should be considered a multi-value (.getAll()) or single value (.get())?
  • Should there be an equivalent of .strict() and .passthrough() for the returned search params?

URLSearchParams and FormData (which are very similar in their API) may get a separate API entirely: https://github.com/colinhacks/zod/issues/1630


Sorry, brain dump. There’s a lot to think about here, and we should definitely hash out details before anyone starts on implementation.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found