1 import { Injectable } from '@angular/core'
2 import { ValidatorFn, Validators } from '@angular/forms'
3 import { BuildFormValidator } from './form-validator.service'
4 import { validateHost } from './host'
7 export class BatchDomainsValidatorsService {
8 readonly DOMAINS: BuildFormValidator
12 VALIDATORS: [ Validators.required, this.validDomains, this.isHostsUnique ],
14 'required': $localize`Domain is required.`,
15 'validDomains': $localize`Domains entered are invalid.`,
16 'uniqueDomains': $localize`Domains entered contain duplicates.`
21 getNotEmptyHosts (hosts: string) {
24 .filter((host: string) => host && host.length !== 0) // Eject empty hosts
27 private validDomains: ValidatorFn = (control) => {
28 if (!control.value) return null
30 const newHostsErrors = []
31 const hosts = this.getNotEmptyHosts(control.value)
33 for (const host of hosts) {
34 if (validateHost(host) === false) {
35 newHostsErrors.push($localize`${host} is not valid`)
40 if (newHostsErrors.length !== 0) {
44 value: newHostsErrors.join('. ') + '.'
53 private isHostsUnique: ValidatorFn = (control) => {
54 if (!control.value) return null
56 const hosts = this.getNotEmptyHosts(control.value)
58 if (hosts.every((host: string) => hosts.indexOf(host) === hosts.lastIndexOf(host))) {