servers.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import * as validator from 'validator'
  2. import 'express-validator'
  3. import { isArray, exists } from './misc'
  4. import { isTestInstance } from '../core-utils'
  5. import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
  6. function isHostValid (host: string) {
  7. const isURLOptions = {
  8. require_host: true,
  9. require_tld: true
  10. }
  11. // We validate 'localhost', so we don't have the top level domain
  12. if (isTestInstance()) {
  13. isURLOptions.require_tld = false
  14. }
  15. return exists(host) && validator.isURL(host, isURLOptions) && host.split('://').length === 1
  16. }
  17. function isEachUniqueHostValid (hosts: string[]) {
  18. return isArray(hosts) &&
  19. hosts.length !== 0 &&
  20. hosts.every(host => {
  21. return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
  22. })
  23. }
  24. function isValidContactBody (value: any) {
  25. return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.BODY)
  26. }
  27. function isValidContactFromName (value: any) {
  28. return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.FROM_NAME)
  29. }
  30. // ---------------------------------------------------------------------------
  31. export {
  32. isValidContactBody,
  33. isValidContactFromName,
  34. isEachUniqueHostValid,
  35. isHostValid
  36. }