2017-06-05 21:53:49 +02:00
|
|
|
import * as validator from 'validator'
|
2016-08-21 10:08:40 +02:00
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
import { isArray, exists } from './misc'
|
2016-12-28 15:49:23 +01:00
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
function isHostValid (host: string) {
|
|
|
|
return exists(host) && validator.isURL(host) && 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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
export {
|
|
|
|
isEachUniqueHostValid,
|
|
|
|
isHostValid
|
|
|
|
}
|
2017-06-10 22:15:25 +02:00
|
|
|
|
|
|
|
declare global {
|
|
|
|
namespace ExpressValidator {
|
|
|
|
export interface Validator {
|
|
|
|
isEachUniqueHostValid
|
|
|
|
isHostValid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|