]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/servers.ts
Fix trending days display on first load
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / servers.ts
CommitLineData
4d4e5cd4 1import * as validator from 'validator'
fdbda9e3 2import 'express-validator'
d57d6f26 3
69818c93 4import { isArray, exists } from './misc'
556ddc31 5import { isTestInstance } from '../core-utils'
a4101923 6import { CONSTRAINTS_FIELDS } from '../../initializers'
67bf9b96 7
69818c93 8function isHostValid (host: string) {
556ddc31
C
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
d57d6f26
C
20}
21
69818c93 22function isEachUniqueHostValid (hosts: string[]) {
65fcc311 23 return isArray(hosts) &&
49abbbbe 24 hosts.length !== 0 &&
075f16ca 25 hosts.every(host => {
67bf9b96 26 return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
d57d6f26
C
27 })
28}
29
a4101923
C
30function isValidContactBody (value: any) {
31 return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.BODY)
32}
33
34function isValidContactFromName (value: any) {
35 return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.FROM_NAME)
36}
37
d57d6f26
C
38// ---------------------------------------------------------------------------
39
65fcc311 40export {
a4101923
C
41 isValidContactBody,
42 isValidContactFromName,
65fcc311
C
43 isEachUniqueHostValid,
44 isHostValid
45}