]>
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' |
a4101923 | 6 | import { CONSTRAINTS_FIELDS } from '../../initializers' |
67bf9b96 | 7 | |
69818c93 | 8 | function isHostValid (host: string) { |
556ddc31 C |
9 | const isURLOptions = { |
10 | require_host: true, | |
11 | require_tld: true | |
12 | } | |
13 | ||
14 | // We validate 'localhost', so we don't have the top level domain | |
15 | if (isTestInstance()) { | |
16 | isURLOptions.require_tld = false | |
17 | } | |
18 | ||
19 | return exists(host) && validator.isURL(host, isURLOptions) && host.split('://').length === 1 | |
d57d6f26 C |
20 | } |
21 | ||
69818c93 | 22 | function isEachUniqueHostValid (hosts: string[]) { |
65fcc311 | 23 | return isArray(hosts) && |
49abbbbe | 24 | hosts.length !== 0 && |
075f16ca | 25 | hosts.every(host => { |
67bf9b96 | 26 | return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host) |
d57d6f26 C |
27 | }) |
28 | } | |
29 | ||
a4101923 C |
30 | function isValidContactBody (value: any) { |
31 | return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.BODY) | |
32 | } | |
33 | ||
34 | function isValidContactFromName (value: any) { | |
35 | return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.FROM_NAME) | |
36 | } | |
37 | ||
d57d6f26 C |
38 | // --------------------------------------------------------------------------- |
39 | ||
65fcc311 | 40 | export { |
a4101923 C |
41 | isValidContactBody, |
42 | isValidContactFromName, | |
65fcc311 C |
43 | isEachUniqueHostValid, |
44 | isHostValid | |
45 | } |