aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/servers.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/custom-validators/servers.ts')
-rw-r--r--server/helpers/custom-validators/servers.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/servers.ts b/server/helpers/custom-validators/servers.ts
new file mode 100644
index 000000000..d5021bf38
--- /dev/null
+++ b/server/helpers/custom-validators/servers.ts
@@ -0,0 +1,34 @@
1import * as validator from 'validator'
2import 'express-validator'
3
4import { isArray, exists } from './misc'
5import { isTestInstance } from '../core-utils'
6
7function isHostValid (host: string) {
8 const isURLOptions = {
9 require_host: true,
10 require_tld: true
11 }
12
13 // We validate 'localhost', so we don't have the top level domain
14 if (isTestInstance()) {
15 isURLOptions.require_tld = false
16 }
17
18 return exists(host) && validator.isURL(host, isURLOptions) && host.split('://').length === 1
19}
20
21function isEachUniqueHostValid (hosts: string[]) {
22 return isArray(hosts) &&
23 hosts.length !== 0 &&
24 hosts.every(host => {
25 return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
26 })
27}
28
29// ---------------------------------------------------------------------------
30
31export {
32 isEachUniqueHostValid,
33 isHostValid
34}