1 import { AbstractControl, FormControl, ValidatorFn, Validators } from '@angular/forms'
2 import { BuildFormValidator } from './form-validator.model'
3 import { validateHost } from './host'
5 export function getNotEmptyHosts (hosts: string) {
8 .filter((host: string) => host && host.length !== 0) // Eject empty hosts
11 const validDomains: ValidatorFn = (control: FormControl) => {
12 if (!control.value) return null
14 const newHostsErrors = []
15 const hosts = getNotEmptyHosts(control.value)
17 for (const host of hosts) {
18 if (validateHost(host) === false) {
19 newHostsErrors.push($localize`${host} is not valid`)
24 if (newHostsErrors.length !== 0) {
28 value: newHostsErrors.join('. ') + '.'
37 const isHostsUnique: ValidatorFn = (control: AbstractControl) => {
38 if (!control.value) return null
40 const hosts = getNotEmptyHosts(control.value)
42 if (hosts.every((host: string) => hosts.indexOf(host) === hosts.lastIndexOf(host))) {
53 export const DOMAINS_VALIDATOR: BuildFormValidator = {
54 VALIDATORS: [Validators.required, validDomains, isHostsUnique],
56 'required': $localize`Domain is required.`,
57 'validDomains': $localize`Domains entered are invalid.`,
58 'uniqueDomains': $localize`Domains entered contain duplicates.`