validator.js icon indicating copy to clipboard operation
validator.js copied to clipboard

isURL returns false when testing urls with localhost domain

Open nikelborm opened this issue 1 year ago • 4 comments

Describe the bug isURL() returns false when testing urls with localhost domain

Examples Reproduction repo (click to open)

import validator from 'validator';

console.log('isURL: ', validator.isURL('http://localhost/api/hello'));

console.log(new URL('http://localhost/api/hello'));
npm start

> [email protected] start
> node index.js

isURL:  false
URL {
  href: 'http://localhost/api/hello',
  origin: 'http://localhost',
  protocol: 'http:',
  username: '',
  password: '',
  host: 'localhost',
  hostname: 'localhost',
  port: '',
  pathname: '/api/hello',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}

Additional context Validator.js version: 13.9.0 Node.js version: 19.3.0 OS platform: [linux]

nikelborm avatar Apr 02 '23 19:04 nikelborm

I have analysed the code. Can you assign this to me ? Can we provide a special condition for "localhost" in the hostname validation ?

Chaitanyaprasad60 avatar Apr 04 '23 17:04 Chaitanyaprasad60

http://localhost:3000/

In the tests I see this is a a invalid URL . Can I know why ?

Chaitanyaprasad60 avatar Apr 04 '23 17:04 Chaitanyaprasad60

Please assign us (@MatiasC248 and me) this one as we already have a solution for it, and will make a pull request shortly

marcos-a-7 avatar Jun 05 '23 23:06 marcos-a-7

You can use host_whitelist option with localhost :

validator.isURL(url, { host_whitelist: ["localhost"] })

glarivie avatar Dec 21 '23 13:12 glarivie

I'm closing this as it's the intended behaviour. See the comment above for a workaround if you want localhost to be a valid domain

WikiRik avatar May 07 '24 20:05 WikiRik