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

Validate isURL if it contains a TLD *or* a protocol #635

Closed
tsx opened this issue Mar 6, 2017 · 7 comments
Closed

Validate isURL if it contains a TLD *or* a protocol #635

tsx opened this issue Mar 6, 2017 · 7 comments

Comments

@tsx
Copy link

tsx commented Mar 6, 2017

tl;dr: There should be a way to validate isURL if it has a TLD or a protocol part, or both. For example:

Use case: I use isURL validation to determine if a user-provided string is an URL or not. Based on that, I render that string either as a hyperlink or plain text. Being user-friendly, I allow URLs to be typed without protocol, assuming implicit http:// in that case. However, sometimes users want to link to their servers on a local network, referencing them by just a hostname, like "http://localhost". To avoid rendering any single word as a link, I'd like a valid URL to have at least one of URL features: either a TLD (like .com) or a protocol (like "http://").

I know that there are require_tld and require_protocol options, but if I set both to false, it will see any single word like "domain" as a valid URL, which is not the case.

@chriso
Copy link
Collaborator

chriso commented Mar 6, 2017

Why not just:

if (validator.isURL(url, {require_tld: true}) ||
    validator.isURL(url, {require_protocol: true}) {
    // render as URL
}

@tsx
Copy link
Author

tsx commented Mar 6, 2017

Well this is definitely an option but requires parsing same input twice. Doing twice more work is not always a good idea performance-wise when trying to decide how to display a few thousands pieces of text on a page.

@mncrff
Copy link

mncrff commented Nov 27, 2017

I am having a similar issue. For me it seems that require_tld: true isn't working at all. The following code block will return true:

validator.isURL('www.something', {
    require_tld: true
});

I am using version 9.1.2, and have tested it both locally (node 8.7.0) and on https://npm.runkit.com/validator

@dsacramone
Copy link

@mncrff I am having the same issue... Why is "http://www.ddd" valid when I pass in {require_tld: true} ???

validator.isURL('http://www.ddd', {
require_tld: true
});

that should fail, but doesn't. Any fix or reason why it's returning true?

@dsacramone
Copy link

bueller bueller?

@chriso
Copy link
Collaborator

chriso commented Jan 31, 2018

@dsacramone read #642 (comment)

@profnandaa
Copy link
Member

Well this is definitely an option but requires parsing same input twice. Doing twice more work is not always a good idea performance-wise when trying to decide how to display a few thousands pieces of text on a page.

@tsx - remember that that work still has to be done by the end of the day, either by the library or library's end-user. I don't see any perf gains here.

@profnandaa profnandaa added the closed-as-stale For stale issues and PRs label Feb 14, 2020
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

5 participants