PeerTube/server/core/helpers/custom-validators/servers.ts

43 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-01-07 07:56:07 -06:00
import validator from 'validator'
import { CONFIG } from '@server/initializers/config.js'
import { CONSTRAINTS_FIELDS } from '../../initializers/constants.js'
import { exists, isArray } from './misc.js'
2016-12-28 08:49:23 -06:00
2017-06-10 15:15:25 -05:00
function isHostValid (host: string) {
2017-08-25 11:36:49 -05:00
const isURLOptions = {
require_host: true,
require_tld: true
}
// We validate 'localhost', so we don't have the top level domain
if (CONFIG.WEBSERVER.HOSTNAME === 'localhost' || CONFIG.WEBSERVER.HOSTNAME === '127.0.0.1') {
2017-08-25 11:36:49 -05:00
isURLOptions.require_tld = false
}
return exists(host) && validator.default.isURL(host, isURLOptions) && host.split('://').length === 1
}
2017-06-10 15:15:25 -05:00
function isEachUniqueHostValid (hosts: string[]) {
2017-05-15 15:22:03 -05:00
return isArray(hosts) &&
2017-07-11 10:04:57 -05:00
hosts.every(host => {
2016-12-28 08:49:23 -06:00
return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
})
}
2019-01-09 08:14:29 -06:00
function isValidContactBody (value: any) {
return exists(value) && validator.default.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.BODY)
2019-01-09 08:14:29 -06:00
}
function isValidContactFromName (value: any) {
return exists(value) && validator.default.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.FROM_NAME)
2019-01-09 08:14:29 -06:00
}
// ---------------------------------------------------------------------------
2017-05-15 15:22:03 -05:00
export {
2019-01-09 08:14:29 -06:00
isValidContactBody,
isValidContactFromName,
2017-05-15 15:22:03 -05:00
isEachUniqueHostValid,
isHostValid
}