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

localhost does not pass a URL validation #26

Open
smnbbrv opened this issue Aug 5, 2019 · 6 comments
Open

localhost does not pass a URL validation #26

smnbbrv opened this issue Aug 5, 2019 · 6 comments

Comments

@smnbbrv
Copy link

smnbbrv commented Aug 5, 2019

Hi @rsaenen

the following URL does not pass the validation by URL

http://localhost:7576/login/callback

Is there a way to fix this?

@rsaenen
Copy link
Owner

rsaenen commented Aug 7, 2019

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

Why would you validate this type of URL ?

@smnbbrv
Copy link
Author

smnbbrv commented Aug 7, 2019

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

@rsaenen
Copy link
Owner

rsaenen commented Aug 7, 2019

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.

@smnbbrv
Copy link
Author

smnbbrv commented Aug 7, 2019

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?

@rsaenen
Copy link
Owner

rsaenen commented Aug 7, 2019

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)]

@ilannn
Copy link

ilannn commented May 23, 2021

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: yuyang041060120#140

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

3 participants