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