webmozarts / assert

Assertions to validate method input/output with nice error messages.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature Request] assert url

pana1990 opened this issue · comments

commented

It would be awesome if assert can validate urls as beberlei/assert.

Rereference: https://github.com/beberlei/assert/blob/master/lib/Assert/Assertion.php#L1555

That is quite a monster regex in order to validate that, and it would be the most complex assertion this package has by far. If there was a more easy method of validating urls i'd be all for it, but it may be out of the scope for this package.

URL is a too complicated and ambiguous type to validate it in a single correct way.

commented

@BackEndTea @zerkms maybe we can use native filter from php :

filter_var($url, FILTER_VALIDATE_URL)

This one is better for you?

@pana1990 because it still leaves room to interpretation: "google.com" <- should this url validate?

Ahh I didn't see that discussion earlier and I already created PR: #199. I started from filter_var($url, FILTER_VALIDATE_URL) but then URLs without http/https protocol are passing. Any suggestions on it?

Edit
As longer I think about it then I also came to conclusion that regex is too complex for just handling assertions. I changed PR to use filter_var. Any type of protocol will be valid (not only http, https) but it's actually not so bad as I initially thought.

That's my very point - it won't pass google.com while I think it's a valid URL.

I'd woudn't mind having this, but my main concern in what @zerkms says. google.com will be a valid URL to most people. But it wouldn't be valid for this, the same goes for //google.com.

I hate to revive this issue from nearly 2 years ago; but a "URL" isn't nearly as ambiguous as you make it seem. It's pretty well-defined by RFC 1738, as can be seen on the Wikipedia page for URL. I'm still in favor of adding Assert::url() which just does a "dumb" \filter_var($value, FILTER_VALIDATE_URL) the same way Assert::email() works.

If this turns out to be an issue, might I suggest a more specific name like Assert::absoluteUrl() or Assert::httpUrl()?