Validator GetUrl could be better

All right so i’m dealing a bit with excel and i’m seeing that we’re dealing with numbers and datetimes in a weird way.

If you’ll notice, the number and the datetime are being used as hyperlinks. It also has some formula implications because when cells contain hyperlinks, they’re not numbers. So you get some confusions from the spreadsheet software and it claims it can’t add a sum of URLs:

After a brief exploration, i believe the issue that’s causing this is the validator’s GetUrl function. When you send either a number or a datetime through RS.Stl.Validation.Validator.GetUrl, that code relies on either Uri creation to fail, or for it to fail to match a small set of formatting criteria.

I suggest that we alter the GetUrl method to do one or two things to tighten it up and reject more invalid URLs.

First, we could use Uri.IsWellFormedUriString just before Uri creation to reject strings that don’t look like URIs (to .net). I tested this and it successfully rejected the datetime i entered in. It failed to reject the numerical value.

Second, (and i’m less solid on this), i think we should reject urls that successfully pass validation as numbers (ints, bytes, decimals, floats, etc). I don’t think there are any solely numerical TLDs, are there? If there were, there’s the possibility that 123.456 could pass as a website, but if there aren’t any, then that just leaves IP schemes that could get confused with numbers, which i don’t think should be a problem.

These two bits solved my problem. What do y’all think?

2 Likes

Go ahead and create a Kiln code review (i.e. pull request) for this and I’ll merge it into canonical. I created an EnduraCode Kiln account for you several months ago. Thanks for the helpful screenshots.

The only thing that you possibly should fix first is the type of exception you’re throwing (UriFormatException). In our coding standards, we’ve written that we’ll only use our own exception classes or ApplicationException, and never any of the other built-in exception types. I’m not seeing the full context of your code, so it’s possible that there’s a good reason you’re using UriFormatException. But if there isn’t, we should probably change it.

1 Like