]>
Commit | Line | Data |
---|---|---|
4d029ef8 C |
1 | import { AbstractControl, ValidatorFn, Validators } from '@angular/forms' |
2 | import { BuildFormValidator } from './form-validator.model' | |
3 | ||
af7fd04a | 4 | export function validateHost (value: string) { |
4d029ef8 C |
5 | // Thanks to http://stackoverflow.com/a/106223 |
6 | const HOST_REGEXP = new RegExp( | |
9df52d66 | 7 | '^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]).)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9])$' |
4d029ef8 C |
8 | ) |
9 | ||
10 | return HOST_REGEXP.test(value) | |
11 | } | |
12 | ||
af7fd04a | 13 | export function validateHandle (value: string) { |
4d029ef8 C |
14 | if (!value) return false |
15 | ||
16 | return value.includes('@') | |
17 | } | |
18 | ||
19 | const validHosts: ValidatorFn = (control: AbstractControl) => { | |
20 | if (!control.value) return null | |
21 | ||
22 | const errors = [] | |
23 | const hosts = splitAndGetNotEmpty(control.value) | |
24 | ||
25 | for (const host of hosts) { | |
26 | if (validateHost(host) === false) { | |
27 | errors.push($localize`${host} is not valid`) | |
28 | } | |
29 | } | |
30 | ||
31 | // valid | |
32 | if (errors.length === 0) return null | |
33 | ||
34 | return { | |
9df52d66 | 35 | validHosts: { |
4d029ef8 C |
36 | reason: 'invalid', |
37 | value: errors.join('. ') + '.' | |
38 | } | |
39 | } | |
40 | } | |
41 | ||
42 | const validHostsOrHandles: ValidatorFn = (control: AbstractControl) => { | |
43 | if (!control.value) return null | |
44 | ||
45 | const errors = [] | |
46 | const lines = splitAndGetNotEmpty(control.value) | |
47 | ||
48 | for (const line of lines) { | |
49 | if (validateHost(line) === false && validateHandle(line) === false) { | |
50 | errors.push($localize`${line} is not valid`) | |
51 | } | |
52 | } | |
53 | ||
54 | // valid | |
55 | if (errors.length === 0) return null | |
56 | ||
57 | return { | |
9df52d66 | 58 | validHostsOrHandles: { |
4d029ef8 C |
59 | reason: 'invalid', |
60 | value: errors.join('. ') + '.' | |
61 | } | |
62 | } | |
63 | } | |
64 | ||
65 | // --------------------------------------------------------------------------- | |
66 | ||
67 | export function splitAndGetNotEmpty (value: string) { | |
68 | return value | |
69 | .split('\n') | |
70 | .filter(line => line && line.length !== 0) // Eject empty hosts | |
71 | } | |
72 | ||
73 | export const unique: ValidatorFn = (control: AbstractControl) => { | |
74 | if (!control.value) return null | |
75 | ||
76 | const hosts = splitAndGetNotEmpty(control.value) | |
77 | ||
78 | if (hosts.every((host: string) => hosts.indexOf(host) === hosts.lastIndexOf(host))) { | |
79 | return null | |
80 | } | |
81 | ||
82 | return { | |
9df52d66 | 83 | unique: { |
4d029ef8 C |
84 | reason: 'invalid' |
85 | } | |
86 | } | |
87 | } | |
88 | ||
89 | export const UNIQUE_HOSTS_VALIDATOR: BuildFormValidator = { | |
90 | VALIDATORS: [ Validators.required, validHosts, unique ], | |
91 | MESSAGES: { | |
9df52d66 C |
92 | required: $localize`Domain is required.`, |
93 | validHosts: $localize`Hosts entered are invalid.`, | |
94 | unique: $localize`Hosts entered contain duplicates.` | |
4d029ef8 C |
95 | } |
96 | } | |
97 | ||
98 | export const UNIQUE_HOSTS_OR_HANDLE_VALIDATOR: BuildFormValidator = { | |
99 | VALIDATORS: [ Validators.required, validHostsOrHandles, unique ], | |
100 | MESSAGES: { | |
9df52d66 C |
101 | required: $localize`Domain is required.`, |
102 | validHostsOrHandles: $localize`Hosts or handles are invalid.`, | |
103 | unique: $localize`Hosts or handles contain duplicates.` | |
4d029ef8 C |
104 | } |
105 | } |