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

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2017-06-05 14:53:49 -05:00
import * as validator from 'validator'
2017-09-07 08:27:35 -05:00
import 'express-validator'
2017-06-10 15:15:25 -05:00
import { isArray, exists } from './misc'
2017-08-25 11:36:49 -05:00
import { isTestInstance } from '../core-utils'
2019-01-09 08:14:29 -06:00
import { CONSTRAINTS_FIELDS } from '../../initializers'
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 (isTestInstance()) {
isURLOptions.require_tld = false
}
return exists(host) && validator.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) &&
hosts.length !== 0 &&
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.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.BODY)
}
function isValidContactFromName (value: any) {
return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.FROM_NAME)
}
// ---------------------------------------------------------------------------
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
}