python-validators / validators

Python Data Validation for Humans™.
MIT License
958 stars 152 forks source link

url validator rejects some local URLs as invalid even though they should be valid #392

Closed MidnightStallion closed 1 month ago

MidnightStallion commented 1 month ago

To the best of my knowledge, a URL such as 'http://localhost' should be valid. but validators says it is not.

http://127.0.0.1 works, but http://locahost does not.

If my understanding is correct, a local host running web server does not need an FQDN for it to work. So I believe 'http://localhost' or 'http://localhost:8000' should be a valid URL.

Steps to reproduce:

  1. validators.url('http://127.0.0.1')
  2. Returns True
  3. validators.url('http://localhost')
  4. Returns a ValidationError
yozachar commented 1 month ago

Hi @MidnightStallion, there is a simple_host parameter for the url function.

$ python -c "from validators import url; print(url('https://localhost', simple_host=True))"
True

I guess an example in the documentation might help.


ref https://github.com/python-validators/validators/issues/285#issuecomment-1676336214

MidnightStallion commented 1 month ago

Hi yozachar. Thanks for your fast response and sorry for the trouble. It works the way I intend when I use simple_host. Thanks for all your work on this. It has saved me a lot of time when it comes to validation.