]>
Commit | Line | Data |
---|---|---|
7cde3b9c | 1 | import validator from 'validator' |
c8861d5d | 2 | import { exists, isArray } from './misc' |
556ddc31 | 3 | import { isTestInstance } from '../core-utils' |
74dc3bca | 4 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' |
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 | ||
a4101923 C |
28 | function isValidContactBody (value: any) { |
29 | return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.BODY) | |
30 | } | |
31 | ||
32 | function isValidContactFromName (value: any) { | |
33 | return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.FROM_NAME) | |
34 | } | |
35 | ||
d57d6f26 C |
36 | // --------------------------------------------------------------------------- |
37 | ||
65fcc311 | 38 | export { |
a4101923 C |
39 | isValidContactBody, |
40 | isValidContactFromName, | |
65fcc311 C |
41 | isEachUniqueHostValid, |
42 | isHostValid | |
43 | } |