]>
Commit | Line | Data |
---|---|---|
1 | import validator from 'validator' | |
2 | import { exists, isArray } from './misc' | |
3 | import { isTestInstance } from '../core-utils' | |
4 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' | |
5 | ||
6 | function isHostValid (host: string) { | |
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 | |
18 | } | |
19 | ||
20 | function isEachUniqueHostValid (hosts: string[]) { | |
21 | return isArray(hosts) && | |
22 | hosts.length !== 0 && | |
23 | hosts.every(host => { | |
24 | return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host) | |
25 | }) | |
26 | } | |
27 | ||
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 | ||
36 | // --------------------------------------------------------------------------- | |
37 | ||
38 | export { | |
39 | isValidContactBody, | |
40 | isValidContactFromName, | |
41 | isEachUniqueHostValid, | |
42 | isHostValid | |
43 | } |