blob: 423d1337f68226b8d8d357e80822a82f40717322 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
import { AbstractControl, FormControl, ValidatorFn, Validators } from '@angular/forms'
import { BuildFormValidator } from './form-validator.model'
import { validateHost } from './host'
export function getNotEmptyHosts (hosts: string) {
return hosts
.split('\n')
.filter((host: string) => host && host.length !== 0) // Eject empty hosts
}
const validDomains: ValidatorFn = (control: FormControl) => {
if (!control.value) return null
const newHostsErrors = []
const hosts = getNotEmptyHosts(control.value)
for (const host of hosts) {
if (validateHost(host) === false) {
newHostsErrors.push($localize`${host} is not valid`)
}
}
/* Is not valid. */
if (newHostsErrors.length !== 0) {
return {
'validDomains': {
reason: 'invalid',
value: newHostsErrors.join('. ') + '.'
}
}
}
/* Is valid. */
return null
}
const isHostsUnique: ValidatorFn = (control: AbstractControl) => {
if (!control.value) return null
const hosts = getNotEmptyHosts(control.value)
if (hosts.every((host: string) => hosts.indexOf(host) === hosts.lastIndexOf(host))) {
return null
} else {
return {
'uniqueDomains': {
reason: 'invalid'
}
}
}
}
export const DOMAINS_VALIDATOR: BuildFormValidator = {
VALIDATORS: [Validators.required, validDomains, isHostsUnique],
MESSAGES: {
'required': $localize`Domain is required.`,
'validDomains': $localize`Domains entered are invalid.`,
'uniqueDomains': $localize`Domains entered contain duplicates.`
}
}
|