]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/custom-validators/servers.ts
Implement contact form on server side
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / servers.ts
... / ...
CommitLineData
1import * as validator from 'validator'
2import 'express-validator'
3
4import { isArray, exists } from './misc'
5import { isTestInstance } from '../core-utils'
6import { CONSTRAINTS_FIELDS } from '../../initializers'
7
8function 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
22function 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
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
38// ---------------------------------------------------------------------------
39
40export {
41 isValidContactBody,
42 isValidContactFromName,
43 isEachUniqueHostValid,
44 isHostValid
45}