diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-20 14:15:15 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-21 13:35:31 +0200 |
commit | 4d029ef8ec3d5274eeaa3ee6d808eb7035e7faef (patch) | |
tree | 20bcdd660ab4eb731814db3a4a40fffb48ce7482 /client/src/app/shared/form-validators | |
parent | 7f28f2ddbaeecf451d501e99ded0408c14a33600 (diff) | |
download | PeerTube-4d029ef8ec3d5274eeaa3ee6d808eb7035e7faef.tar.gz PeerTube-4d029ef8ec3d5274eeaa3ee6d808eb7035e7faef.tar.zst PeerTube-4d029ef8ec3d5274eeaa3ee6d808eb7035e7faef.zip |
Add ability for instances to follow any actor
Diffstat (limited to 'client/src/app/shared/form-validators')
4 files changed, 105 insertions, 69 deletions
diff --git a/client/src/app/shared/form-validators/batch-domains-validators.ts b/client/src/app/shared/form-validators/batch-domains-validators.ts deleted file mode 100644 index 423d1337f..000000000 --- a/client/src/app/shared/form-validators/batch-domains-validators.ts +++ /dev/null | |||
@@ -1,60 +0,0 @@ | |||
1 | import { AbstractControl, FormControl, ValidatorFn, Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | import { validateHost } from './host' | ||
4 | |||
5 | export function getNotEmptyHosts (hosts: string) { | ||
6 | return hosts | ||
7 | .split('\n') | ||
8 | .filter((host: string) => host && host.length !== 0) // Eject empty hosts | ||
9 | } | ||
10 | |||
11 | const validDomains: ValidatorFn = (control: FormControl) => { | ||
12 | if (!control.value) return null | ||
13 | |||
14 | const newHostsErrors = [] | ||
15 | const hosts = getNotEmptyHosts(control.value) | ||
16 | |||
17 | for (const host of hosts) { | ||
18 | if (validateHost(host) === false) { | ||
19 | newHostsErrors.push($localize`${host} is not valid`) | ||
20 | } | ||
21 | } | ||
22 | |||
23 | /* Is not valid. */ | ||
24 | if (newHostsErrors.length !== 0) { | ||
25 | return { | ||
26 | 'validDomains': { | ||
27 | reason: 'invalid', | ||
28 | value: newHostsErrors.join('. ') + '.' | ||
29 | } | ||
30 | } | ||
31 | } | ||
32 | |||
33 | /* Is valid. */ | ||
34 | return null | ||
35 | } | ||
36 | |||
37 | const isHostsUnique: ValidatorFn = (control: AbstractControl) => { | ||
38 | if (!control.value) return null | ||
39 | |||
40 | const hosts = getNotEmptyHosts(control.value) | ||
41 | |||
42 | if (hosts.every((host: string) => hosts.indexOf(host) === hosts.lastIndexOf(host))) { | ||
43 | return null | ||
44 | } else { | ||
45 | return { | ||
46 | 'uniqueDomains': { | ||
47 | reason: 'invalid' | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | } | ||
52 | |||
53 | export const DOMAINS_VALIDATOR: BuildFormValidator = { | ||
54 | VALIDATORS: [Validators.required, validDomains, isHostsUnique], | ||
55 | MESSAGES: { | ||
56 | 'required': $localize`Domain is required.`, | ||
57 | 'validDomains': $localize`Domains entered are invalid.`, | ||
58 | 'uniqueDomains': $localize`Domains entered contain duplicates.` | ||
59 | } | ||
60 | } | ||
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 | } | ||
diff --git a/client/src/app/shared/form-validators/host.ts b/client/src/app/shared/form-validators/host.ts deleted file mode 100644 index c18a35f9b..000000000 --- a/client/src/app/shared/form-validators/host.ts +++ /dev/null | |||
@@ -1,8 +0,0 @@ | |||
1 | export function validateHost (value: string) { | ||
2 | // Thanks to http://stackoverflow.com/a/106223 | ||
3 | const HOST_REGEXP = new RegExp( | ||
4 | '^(([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])$' | ||
5 | ) | ||
6 | |||
7 | return HOST_REGEXP.test(value) | ||
8 | } | ||
diff --git a/client/src/app/shared/form-validators/index.ts b/client/src/app/shared/form-validators/index.ts index f621f03a4..c14272a2a 100644 --- a/client/src/app/shared/form-validators/index.ts +++ b/client/src/app/shared/form-validators/index.ts | |||
@@ -1,5 +1,4 @@ | |||
1 | export * from './form-validator.model' | 1 | export * from './form-validator.model' |
2 | export * from './host' | ||
3 | 2 | ||
4 | // Don't re export const variables because webpack 4 cannot do tree shaking with them | 3 | // Don't re export const variables because webpack 4 cannot do tree shaking with them |
5 | // export * from './abuse-validators' | 4 | // export * from './abuse-validators' |