]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as validator from 'validator' |
fdbda9e3 | 2 | import 'express-validator' |
d57d6f26 | 3 | |
69818c93 | 4 | import { isArray, exists } from './misc' |
556ddc31 | 5 | import { isTestInstance } from '../core-utils' |
67bf9b96 | 6 | |
69818c93 | 7 | function isHostValid (host: string) { |
556ddc31 C |
8 | const isURLOptions = { |
9 | require_host: true, | |
10 | require_tld: true | |
11 | } | |
12 | ||
13 | // We validate 'localhost', so we don't have the top level domain | |
14 | if (isTestInstance()) { | |
15 | isURLOptions.require_tld = false | |
16 | } | |
17 | ||
18 | return exists(host) && validator.isURL(host, isURLOptions) && host.split('://').length === 1 | |
d57d6f26 C |
19 | } |
20 | ||
69818c93 | 21 | function isEachUniqueHostValid (hosts: string[]) { |
65fcc311 | 22 | return isArray(hosts) && |
49abbbbe | 23 | hosts.length !== 0 && |
075f16ca | 24 | hosts.every(host => { |
67bf9b96 | 25 | return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host) |
d57d6f26 C |
26 | }) |
27 | } | |
28 | ||
29 | // --------------------------------------------------------------------------- | |
30 | ||
65fcc311 C |
31 | export { |
32 | isEachUniqueHostValid, | |
33 | isHostValid | |
34 | } |