2017-06-05 19:53:49 +00:00
|
|
|
import * as validator from 'validator'
|
2017-09-07 13:27:35 +00:00
|
|
|
import 'express-validator'
|
2016-08-21 08:08:40 +00:00
|
|
|
|
2017-06-10 20:15:25 +00:00
|
|
|
import { isArray, exists } from './misc'
|
2017-08-25 16:36:49 +00:00
|
|
|
import { isTestInstance } from '../core-utils'
|
2016-12-28 14:49:23 +00:00
|
|
|
|
2017-06-10 20:15:25 +00:00
|
|
|
function isHostValid (host: string) {
|
2017-08-25 16:36:49 +00:00
|
|
|
const isURLOptions = {
|
|
|
|
require_host: true,
|
|
|
|
require_tld: true
|
|
|
|
}
|
|
|
|
|
|
|
|
// We validate 'localhost', so we don't have the top level domain
|
|
|
|
if (isTestInstance()) {
|
|
|
|
isURLOptions.require_tld = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return exists(host) && validator.isURL(host, isURLOptions) && host.split('://').length === 1
|
2016-08-21 08:08:40 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 20:15:25 +00:00
|
|
|
function isEachUniqueHostValid (hosts: string[]) {
|
2017-05-15 20:22:03 +00:00
|
|
|
return isArray(hosts) &&
|
2016-11-14 19:03:04 +00:00
|
|
|
hosts.length !== 0 &&
|
2017-07-11 15:04:57 +00:00
|
|
|
hosts.every(host => {
|
2016-12-28 14:49:23 +00:00
|
|
|
return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
|
2016-08-21 08:08:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 20:22:03 +00:00
|
|
|
export {
|
|
|
|
isEachUniqueHostValid,
|
|
|
|
isHostValid
|
|
|
|
}
|