]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/servers.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / servers.ts
CommitLineData
7cde3b9c 1import validator from 'validator'
c8861d5d 2import { exists, isArray } from './misc'
556ddc31 3import { isTestInstance } from '../core-utils'
74dc3bca 4import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
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
13 if (isTestInstance()) {
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) &&
49abbbbe 22 hosts.length !== 0 &&
075f16ca 23 hosts.every(host => {
67bf9b96 24 return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
d57d6f26
C
25 })
26}
27
a4101923
C
28function isValidContactBody (value: any) {
29 return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.BODY)
30}
31
32function isValidContactFromName (value: any) {
33 return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.FROM_NAME)
34}
35
d57d6f26
C
36// ---------------------------------------------------------------------------
37
65fcc311 38export {
a4101923
C
39 isValidContactBody,
40 isValidContactFromName,
65fcc311
C
41 isEachUniqueHostValid,
42 isHostValid
43}