]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/servers.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / servers.ts
CommitLineData
7cde3b9c 1import validator from 'validator'
e5fc6b59 2import { CONFIG } from '@server/initializers/config'
74dc3bca 3import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
9452d4fd 4import { exists, isArray } from './misc'
67bf9b96 5
69818c93 6function 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
efaf3797 13 if (CONFIG.WEBSERVER.HOSTNAME === 'localhost' || CONFIG.WEBSERVER.HOSTNAME === '127.0.0.1') {
556ddc31
C
14 isURLOptions.require_tld = false
15 }
16
17 return exists(host) && validator.isURL(host, isURLOptions) && host.split('://').length === 1
d57d6f26
C
18}
19
69818c93 20function isEachUniqueHostValid (hosts: string[]) {
65fcc311 21 return isArray(hosts) &&
075f16ca 22 hosts.every(host => {
67bf9b96 23 return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
d57d6f26
C
24 })
25}
26
a4101923
C
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
d57d6f26
C
35// ---------------------------------------------------------------------------
36
65fcc311 37export {
a4101923
C
38 isValidContactBody,
39 isValidContactFromName,
65fcc311
C
40 isEachUniqueHostValid,
41 isHostValid
42}