2020-01-07 08:56:07 -05:00
|
|
|
import validator from 'validator'
|
2022-10-11 07:42:58 -04:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
2019-04-11 08:26:41 -04:00
|
|
|
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
|
2022-07-06 09:44:14 -04:00
|
|
|
import { exists, isArray } from './misc'
|
2016-12-28 09:49:23 -05:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function isHostValid (host: string) {
|
2017-08-25 12:36:49 -04:00
|
|
|
const isURLOptions = {
|
|
|
|
require_host: true,
|
|
|
|
require_tld: true
|
|
|
|
}
|
|
|
|
|
|
|
|
// We validate 'localhost', so we don't have the top level domain
|
2023-02-17 04:28:33 -05:00
|
|
|
if (CONFIG.WEBSERVER.HOSTNAME === 'localhost' || CONFIG.WEBSERVER.HOSTNAME === '127.0.0.1') {
|
2017-08-25 12:36:49 -04:00
|
|
|
isURLOptions.require_tld = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return exists(host) && validator.isURL(host, isURLOptions) && host.split('://').length === 1
|
2016-08-21 04:08:40 -04:00
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function isEachUniqueHostValid (hosts: string[]) {
|
2017-05-15 16:22:03 -04:00
|
|
|
return isArray(hosts) &&
|
2017-07-11 11:04:57 -04:00
|
|
|
hosts.every(host => {
|
2016-12-28 09:49:23 -05:00
|
|
|
return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
|
2016-08-21 04:08:40 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-09 09:14:29 -05: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)
|
|
|
|
}
|
|
|
|
|
2016-08-21 04:08:40 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
2019-01-09 09:14:29 -05:00
|
|
|
isValidContactBody,
|
|
|
|
isValidContactFromName,
|
2017-05-15 16:22:03 -04:00
|
|
|
isEachUniqueHostValid,
|
|
|
|
isHostValid
|
|
|
|
}
|