zod icon indicating copy to clipboard operation
zod copied to clipboard

The new v4 string helpers seem incorrectly validate strings that should fail (or be corrected)

Open markcarroll opened this issue 4 months ago • 1 comments

There seems to be a flaw with the new v4 format. I like the simplification of being able to use z.url() or z.email() but it doesn't actually do what it says in the documentation.

Let's take an example:

const input = " https://a.com";

const schema = z.url();

url.parse(input):
// returns " https://a.com"

This is not a valid url but it is passing.

Now if we use the URL constructor:

new URL(input).href
// returns "https://a.com"

It strips the leading space and returns a valid URL.

So, reading the docs where you say you use the URL constructor would lead you to assume that calling z.url().parse() would return a valid trimmed string, but this is just not the case. Given that spaces are generally not acceptable in most of the helpers - email, url, etc - i think it would be preferable if the parser performs the trim by default. Or even just returns the result of the native tools that you are using.

If I am missing something please let me know, but this seems to be a real problem.

(thanks for all your work on this library by the way, overall I think it does a great job)

markcarroll avatar Jun 22 '25 00:06 markcarroll