]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/servers.ts
Don't expose constants directly in initializers/
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / servers.ts
1 import * as validator from 'validator'
2 import 'express-validator'
3
4 import { isArray, exists } from './misc'
5 import { isTestInstance } from '../core-utils'
6 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
7
8 function isHostValid (host: string) {
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
20 }
21
22 function isEachUniqueHostValid (hosts: string[]) {
23 return isArray(hosts) &&
24 hosts.length !== 0 &&
25 hosts.every(host => {
26 return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
27 })
28 }
29
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
38 // ---------------------------------------------------------------------------
39
40 export {
41 isValidContactBody,
42 isValidContactFromName,
43 isEachUniqueHostValid,
44 isHostValid
45 }