ngx-custom-validators icon indicating copy to clipboard operation
ngx-custom-validators copied to clipboard

localhost does not pass a URL validation

Open smnbbrv opened this issue 6 years ago • 6 comments

Hi @rsaenen

the following URL does not pass the validation by URL

http://localhost:7576/login/callback

Is there a way to fix this?

smnbbrv avatar Aug 05 '19 19:08 smnbbrv

Hi, This URL is invalid, because it hasn't a domain.

Why would you validate this type of URL ?

rsaenen avatar Aug 07 '19 06:08 rsaenen

Well this is still a possible URL. Apart from that, you can assign any name to any IP in the hosts file and the URL using this name will be as valid as the localhost one

I need this for the local development mostly

smnbbrv avatar Aug 07 '19 07:08 smnbbrv

You can use a pipe to add a domain to your URL.

I can't edit the validator regex to let 'localhost' to be valid and impact all of others users of this package. It would allow 'http://baguette' for example, this is forbidden.

rsaenen avatar Aug 07 '19 09:08 rsaenen

Just found out that the current regex also treats 10.*, 127.* and 192.* IPs as non-URLs.

I would say, this is not really a validator that really validates the URL. It bans lots of URLs that are valid but considered not useful for standard use cases. You can't set your home page to be localhost or the IP from the private / intranet range, but it does not anyhow mean that this is not a URL.

However I really understand that changing this behaviour is a breaking change and also I understand that this is the inheritance you got from the original project. Thank you for supporting this library!

More to some sort of solution: would adding another validator make sense? Or just extending the validator with a parameter "isPublicUrl" = (by default) true which means the current behaviour and if false then a standard URL validation allowing any domain name and IP is applied?

smnbbrv avatar Aug 07 '19 09:08 smnbbrv

Thanks for the feedback, I guess the best choice is to keep simple regex to validate any data.

An IPValidator should comes up with the possibility to combine multiple validators with an OR. Example:

new FormControl(value, [Validators.required, CustomValidators.or(CustomValidators.IP, CustomValidators.url)]

rsaenen avatar Aug 07 '19 11:08 rsaenen

I would suggest adding the support for the localhost & IPs as part of the domain, and before each of them is should be the protocol part (aka "HTTP://"). See my PR with the added support + tests: https://github.com/yuyang041060120/ng2-validation/pull/140

ilannn avatar May 23 '21 19:05 ilannn