diff options
Diffstat (limited to 'client/src/app/shared/form-validators/host-validators.ts')
-rw-r--r-- | client/src/app/shared/form-validators/host-validators.ts | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/client/src/app/shared/form-validators/host-validators.ts b/client/src/app/shared/form-validators/host-validators.ts new file mode 100644 index 000000000..d750113ef --- /dev/null +++ b/client/src/app/shared/form-validators/host-validators.ts | |||
@@ -0,0 +1,105 @@ | |||
1 | import { AbstractControl, ValidatorFn, Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | |||
4 | function validateHost (value: string) { | ||
5 | // Thanks to http://stackoverflow.com/a/106223 | ||
6 | const HOST_REGEXP = new RegExp( | ||
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])$' | ||
8 | ) | ||
9 | |||
10 | return HOST_REGEXP.test(value) | ||
11 | } | ||
12 | |||
13 | function validateHandle (value: string) { | ||
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 { | ||
35 | 'validHosts': { | ||
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 { | ||
58 | 'validHostsOrHandles': { | ||
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 { | ||
83 | 'unique': { | ||
84 | reason: 'invalid' | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | |||
89 | export const UNIQUE_HOSTS_VALIDATOR: BuildFormValidator = { | ||
90 | VALIDATORS: [ Validators.required, validHosts, unique ], | ||
91 | MESSAGES: { | ||
92 | 'required': $localize`Domain is required.`, | ||
93 | 'validHosts': $localize`Hosts entered are invalid.`, | ||
94 | 'unique': $localize`Hosts entered contain duplicates.` | ||
95 | } | ||
96 | } | ||
97 | |||
98 | export const UNIQUE_HOSTS_OR_HANDLE_VALIDATOR: BuildFormValidator = { | ||
99 | VALIDATORS: [ Validators.required, validHostsOrHandles, unique ], | ||
100 | MESSAGES: { | ||
101 | 'required': $localize`Domain is required.`, | ||
102 | 'validHostsOrHandles': $localize`Hosts or handles are invalid.`, | ||
103 | 'unique': $localize`Hosts or handles contain duplicates.` | ||
104 | } | ||
105 | } | ||