-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make email regex reasonable #2157
Conversation
✅ Deploy Preview for guileless-rolypoly-866f8a ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
Agree! Maybe we should:
This'll help people migrate to |
Not that you are asking for suggestions but what about:
|
Also, what do you think about using the Regex that HTML https://www.w3.org/TR/2012/WD-html-markup-20121025/input.email.html#form.data.emailaddress_xref2
|
Maybe add |
Oops, that was already supported by the regex but missed it in my breakdown. Good catch.
Definitely. I think we should consider exporting a bunch of regexes from The problem is that each additional "special" validator like It's also possible we should let people specify their own email regex which would let people customize the behavior and still get an z.string().email({ pattern: /asdf/ })
Nice! I'd missed this. I think it makes sense and seems to be functionally equivalent to mine but with support for "printable characters" in the local part. I'm not convinced emails with those characters should be considered valid, but I like the parity with browsers.
I don't like camel-cased method names, and I don't want to burn |
I would love to be able to allow only emails with a top level domain in all my apps that I am writing. If I was making zod api, I would even make it forbid these adresses without a TLD by default. If you would want it to parse it, you would have to use |
I think this regular expression is still a little too permissive when it comes to An email where the username is all
When used for mail aliasing, the And usually there is only a single plus character (or rarely they're sometimes used as separators Since
|
This comment was marked as outdated.
This comment was marked as outdated.
Hi! I just opened #2224 to enable |
i also ran into this with mail-tester.com emails, would be great if this or the other one get merged in! |
You should allow hyphens anywhere in the domain part, excluding all students of a particular country is not a great idea (I suspect that other countries also use those kinds of email addresses). No offense of course, but as this is (at least indirectly) a matter of inclusivity, I figured that raising the issue before this gets merged is important, and it illustrates @TomasHubelbauer's comment. |
I am still having the issue that In the end I had to use |
Thank you! Is there a planned release date for this fix? |
I ran into this error as well, and we didn't want to use an unstable (canary) Zod build, so I ended up just wrapping Zod and overriding import { z, type ZodString } from "zod";
// Replace this with whatever regex you need
const emailRegex =
/^([A-Z0-9_+-]+\.?)*[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
z.ZodString.prototype.email = function (
message?:
| string
| {
message?: string | undefined;
},
): ZodString {
return this._addCheck({
kind: "regex",
regex: emailRegex,
message: typeof message === "string" ? message : message?.message,
});
};
export { z }; Then, instead of Not the cleanest solution, but it saved me from having to change every |
Are you aware that this change excludes anyone with an apostrophe in their name? eg. |
A lot of people think every technically valid email address must parse validation by
z.string().email()
. I used to think this too. Over the years I've merged several PRs to make the email regex more "technically correct".Most recently, I merged a PR that adds support for IPv6 addresses as the domain part of an email. They look like this:
Turns out that PR also broke plain old subdomains like
user@sub.domain.com
. That means my mom's current email address would fail to parse, but jonny up there can get away with parsing his freakish IPv6 email.This made me re-evaluate my whole stance on what
z.string().email()
should do. Zod's users are mostly engineers who are building apps. When you're building an app, you want to make sure your users provide normal-ass email addresses. So that's whatz.string().email()
should do, imo.So I rewrote the regex from scratch to be simple and reasonable.
[a-zA-Z0-9-+._]
[a-zA-Z0-9-]
[a-zA-Z]
allowedIt is not trying to be RFC 5322 compliant. It's not going to check if the TLD is real. And it's not going to implement any of this craziness:
wtf-!#$%&'*/=?^_{|}~@mail.com
"zod is cool"@mail.com
regexiscool(kinda)@mail.com
billie@[1.2.3.4]
jonny@[ipv6:7e95:0559:10f2:21e9:9dab:7309:c116:ca3b]
👎@mail.com
For anything else, you can always use
.regex()
or.superRefine()
.