]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/custom-validators/servers.ts
/!\ Use a dedicated config file for development
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / servers.ts
... / ...
CommitLineData
1import validator from 'validator'
2import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
3import { isTestOrDevInstance } from '../core-utils'
4import { exists, isArray } from './misc'
5
6function 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 (isTestOrDevInstance()) {
14 isURLOptions.require_tld = false
15 }
16
17 return exists(host) && validator.isURL(host, isURLOptions) && host.split('://').length === 1
18}
19
20function isEachUniqueHostValid (hosts: string[]) {
21 return isArray(hosts) &&
22 hosts.every(host => {
23 return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
24 })
25}
26
27function isValidContactBody (value: any) {
28 return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.BODY)
29}
30
31function isValidContactFromName (value: any) {
32 return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.FROM_NAME)
33}
34
35// ---------------------------------------------------------------------------
36
37export {
38 isValidContactBody,
39 isValidContactFromName,
40 isEachUniqueHostValid,
41 isHostValid
42}