zod
zod copied to clipboard
bug: `z.string().url()` is incosistent across browser
Just learned that z.string().url()
actually fails on firefox for https://*.example.com
while it passes for every other browser.
Not too sure if this inconsistency is intended or not.
Happy to make a PR to help resolve the inconsistency with the new URL
being used under the hood for FF.
On version zod: 3.20.2
I have confirmed this. However, this is an issue with the implementation of URL
across browsers and not necessarily a problem with zod
.
Yeap, totally aligned.
saw the
try{
new URL()
} catch(e) {}
block for validating URL
I think we should either
- Document this within the docs (Am leaning towards this)
- Do some wrapper around URLs coming from Firefox to make sure it passes
- Do custom regex
- Something else
I think the main thing is to make either the behavior consistent or at least have it documented.
Happy to do any of the above
This is also an issue in React Native, where new URL
does not throw correctly in its constructor:
This appears to have impacted at least one user: https://github.com/colinhacks/zod/issues/2236 as well as someone downstream as a user of my library (which uses Zod).
While this could likely be solved by poly-filling URL
in React Native with a non-standard (for RN) URL polyfill, this method is:
- More challenging to debug for those less familiar with the inner working of React Native/polyfills
- Potentially breaking for users with React Native if the working of the current React Native usage is required
Ideally, I actually think that a regex should potentially replace the URL
functionality in order to keep consistency and avoid headaches for end-users.
Also happy to work on this if needed.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Weird because I saw someone using url() normally... I don't know if you used something else, I don't remember.
I was looking to open another issue but I guess this can be used as well.
The following value foo-bar: true
will be considered as a valid URL for the z.string().url()
schema.
I had to use a custom validator using refine
to define a regex for HTTP URLs
z.string().refine((value) => /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/.test(value), 'Field should be a valid URL')