Skip to content
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

Use WHATWG URL for url and email validation #203

Open
stevenvachon opened this issue Jul 5, 2017 · 10 comments
Open

Use WHATWG URL for url and email validation #203

stevenvachon opened this issue Jul 5, 2017 · 10 comments

Comments

@stevenvachon
Copy link

stevenvachon commented Jul 5, 2017

Via universal-url as it covers far more edge cases such as IDNAs and IPv6 than a simple regex will.

const isEmail = email => {
  try {
    const url = new URL(`mailto:${email}`);
    return url.search === '';
  } catch (error) {
    return false;
  }
};

const isURL = url => {
  try {
    url = new URL(url);
    return url.protocol === 'http' || url.protocol === 'https' || url.protocol === 'ftp';
  } catch (error) {
    return false;
  }
};
@ljharb
Copy link
Collaborator

ljharb commented Jul 5, 2017

In order to do that, we'd have to ship the entire URL polyfill to browsers - or require that users do it. The polyfill still seems prohibitively large.

(also, in general, email validation that's anything more than "it has an @, and you can receive an email sent to it" is useless, so I'm not interested in making email validation "better", when it needs to be reduced, not increased)

@stevenvachon
Copy link
Author

stevenvachon commented Jul 5, 2017

URL is correct and complete while this library's regex is neither. File size concerns are addressed via universal-url-lite.

Checking an email address for a correct IDNA is no less useful than checking a URL for the same. Email servers adhere to the same IP and DNS rules as a web server.

@ljharb
Copy link
Collaborator

ljharb commented Jul 5, 2017

They're slightly different, a@b is a valid email address. The only way you can correctly validate an email address is to send it an email with a secret, and verify receipt of the secret.

To clarify: does universal-url include 100% of the unicode compliance it needs, and then universal-url-lite only fails on IDNA URLs?

@stevenvachon
Copy link
Author

stevenvachon commented Jul 5, 2017

mailto:a@b is a valid URL as well, but I would think that a@:*&^: is not a valid email address. However: jsdom/whatwg-url#98

universal-url is, to my knowledge, 100% TR46 compliant. universal-url-lite has two shims, one with incomplete TR46 and one that relies on the native browser implementation, which all are currently incomplete.

@ljharb
Copy link
Collaborator

ljharb commented Jul 5, 2017

It would help if you could provide sets of non-IDNA test cases that pass with your shim, but fail with the current implementation - that would help me understand the risk/reward.

@stevenvachon
Copy link
Author

require("forms/lib/validators").url(false)(null, {
  data: "http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"
}, result => console.log(result))
//-> Please enter a valid URL.

@ljharb
Copy link
Collaborator

ljharb commented Jul 6, 2017

Thanks - so, these are the categories of URLs that using universal-url would allow to be valid:

  • IPv6 URLs
  • IDNA URLs
  • alternative protocol URLs

These are the categories of URLs that currently already work:

  • ascii URLs
  • IPv4 URLs
  • http/https/ftp URLs

Any others?

@stevenvachon
Copy link
Author

This library currently only supports http, https and ftp.

@ljharb
Copy link
Collaborator

ljharb commented Jul 6, 2017

OK, I've edited my list above. Any other categories?

@stevenvachon
Copy link
Author

That's all I can think of at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants