validator.js
validator.js copied to clipboard
isURL returns false when testing urls with localhost domain
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]
I have analysed the code. Can you assign this to me ? Can we provide a special condition for "localhost" in the hostname validation ?
http://localhost:3000/
In the tests I see this is a a invalid URL . Can I know why ?
Please assign us (@MatiasC248 and me) this one as we already have a solution for it, and will make a pull request shortly
You can use host_whitelist
option with localhost
:
validator.isURL(url, { host_whitelist: ["localhost"] })
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