-
Notifications
You must be signed in to change notification settings - Fork 141
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
URL.canParse() #713
Comments
"Valid" here will need to be defined as the spec defines a clear notion of a "validation error". Specifically, a URL can parse successfully but still be considered "invalid" per the spec. Does "isValid(...) === true" mean that the URL parses successfully or does it mean it parsed without any validation errors? |
As far as bikeshedding the API is concerned, I would likely prefer something more like: URL.test(input[, base]); With the semantics specifically only limited to: Will this parse successfully. |
Please see https://whatwg.org/faq#adding-new-features, especially step 1 and 2. |
How would that be different from what |
The key difference would be no allocations, in particular no Error object created if it does not parse, and therefore no expensive stack capture. |
@domenic Should we provide more use-cases here? All that we are trying to solve here is to have a convenient way to identify if a given URL is valid or not. @jasnell Either way if we agree this is a problem space, happy to discuss more. |
@hemanth well it would help if you would first clarify what semantics you are after as validity and "can parse" are very different things. (But yeah, depending on the answer, use cases and demonstrating there is a need would help.) |
The primary use case I've encountered for this is really functions like the isValid() in the OP at the top. Specifically, code that wishes to verify that a given input can be handled as a URL (can parse) without incurring the additional cost of creating either a URL object or an Error object with an expensive to generate stack. I've encountered this pattern a number of times. Most often, I'd say, the check is performed using a regex, which is problematic in that it doesn't match the parsing semantics and can be difficult to make correct. There's an old module on npm called is-url that has over 2.5 million downloads per week to address this specific problem using an incomplete regex approach. For me, I would 100% support an API that checks if an input can be parsed as a URL. I have no use case for further validity checks. Alternatively, I would be good with a |
@annevk My thoughts resonated completely with @jasnell. There is also This is a very common pattern across multiple code base and would be beneficial to multiple developers to have a connivence method with in the standards. If there is a template repo that I can fork and add more structure to the challenge and propose potential solutions, I am happy to collaborate. |
See also #372 although that would not return a boolean presumably. Given that there's popular libraries that do something like this this is worth doing I think, but this is not validation. We should either call this something like |
It's not clear to me solving this at the level of a new API is the best idea. Wouldn't it be better to make |
That wouldn’t likely help you if an error is caught by a calling function, or an async function, or any of the Promise methods. |
Right, you would put the catch there immediately surrounding the new URL call, just like you would put the URL.isParseable call there immediately surrounding the string input. |
I'd argue that it's definitely better to make capturing that stack more efficient just in general but I don't think that eliminates to usefulness of the proposed new API. For instance, suppose I am filtering a list of strings to acquire only the ones that are parseable URLs e.g. |
@domenic that would certainly be an interesting unobservable optimization, for a lot of patterns - but i don't think the real problem here is performance, it's ergonomics. try/catching something is a wildly icky way to get a boolean. |
For the ergonomics of try/catch, perhaps that's something that should be solved at the language level? For instance, some languages have a |
There's not that many predicates that need to be implemented in the form of a try/catch, so i'm not sure if that'd be worth additional syntax. |
@domenic do your comments above mean you disagree with #372 (comment) now? I find @jasnell's comment above about being able to use a non-throwing method in a number of scenarios compelling. Also, https://www.npmjs.com/package/valid-url, https://www.npmjs.com/package/is-url-superb, and https://www.npmjs.com/package/is-url suggest an authoritative method would be useful for the ecosystem. @valenting @ricea @achristensen07 thoughts? |
I'm not convinced that In the absence of benchmarks I'm also not convinced by the performance argument. Edit: Actually, even if there were benchmarks, why do we think this is performance critical? Who needs to verify whether a million URLs parse anyway? |
Tests: web-platform-tests/wpt#39069. Fixes #713.
If you have the following code, the url actually needs to be parsed twice. const url = 'http://foo.com'
let parsed_url = null
if (URL.canParse(url)) {
parsed_url = new URL(url)
} Looking back, now that we've added |
I'm not a big fan of folks resurrecting closed issues. Please consider filing a new issue in the future if there's something you wish to discuss. As for your question, I don't think that's the only case where |
It would be good to have a
URL.isValid
method?Today we have to do something like:
The proposal is to have something like:
The text was updated successfully, but these errors were encountered: