2020-01-07 14:56:07 +01:00
|
|
|
import validator from 'validator'
|
2019-07-25 16:23:44 +02:00
|
|
|
import { exists, isArray } from './misc'
|
2017-08-25 18:36:49 +02:00
|
|
|
import { isTestInstance } from '../core-utils'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
|
2016-12-28 15:49:23 +01:00
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
function isHostValid (host: string) {
|
2017-08-25 18:36:49 +02: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
|
2016-08-21 10:08:40 +02:00
|
|
|
}
|
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
function isEachUniqueHostValid (hosts: string[]) {
|
2017-05-15 22:22:03 +02:00
|
|
|
return isArray(hosts) &&
|
2016-11-14 20:03:04 +01:00
|
|
|
hosts.length !== 0 &&
|
2017-07-11 17:04:57 +02:00
|
|
|
hosts.every(host => {
|
2016-12-28 15:49:23 +01:00
|
|
|
return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host)
|
2016-08-21 10:08:40 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-09 15:14:29 +01: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 10:08:40 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
export {
|
2019-01-09 15:14:29 +01:00
|
|
|
isValidContactBody,
|
|
|
|
isValidContactFromName,
|
2017-05-15 22:22:03 +02:00
|
|
|
isEachUniqueHostValid,
|
|
|
|
isHostValid
|
|
|
|
}
|