Skip to content

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

@markcarroll

Description

@markcarroll

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions