]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/custom-validators/pods.ts
Update webpack config
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / pods.ts
index e4c827feb11b6405d964afce9499bb108e4e6a13..f2ca520c089891c536f95acffdeb6200b311a440 100644 (file)
@@ -1,17 +1,26 @@
-import expressValidator = require('express-validator')
-// TODO: use .validator when express-validator typing will have validator field
-const validator = expressValidator['validator']
+import * as validator from 'validator'
 
-import { isArray } from './misc'
+import { isArray, exists } from './misc'
+import { isTestInstance } from '../core-utils'
 
-function isHostValid (host) {
-  return validator.isURL(host) && host.split('://').length === 1
+function isHostValid (host: string) {
+  const isURLOptions = {
+    require_host: true,
+    require_tld: true
+  }
+
+  // We validate 'localhost', so we don't have the top level domain
+  if (isTestInstance()) {
+    isURLOptions.require_tld = false
+  }
+
+  return exists(host) && validator.isURL(host, isURLOptions) && host.split('://').length === 1
 }
 
-function isEachUniqueHostValid (hosts) {
+function isEachUniqueHostValid (hosts: string[]) {
   return isArray(hosts) &&
     hosts.length !== 0 &&
-    hosts.every(function (host) {
+    hosts.every(host => {
       return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
     })
 }
@@ -22,3 +31,10 @@ export {
   isEachUniqueHostValid,
   isHostValid
 }
+
+declare module 'express-validator' {
+  export interface Validator {
+    isEachUniqueHostValid
+    isHostValid
+  }
+}