diff options
author | Chocobozzz <me@florianbigard.com> | 2020-08-17 11:47:04 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-08-17 15:12:55 +0200 |
commit | 7ed1edbbe4ffbef28093e4f5630751cb652814e4 (patch) | |
tree | 831862165dbfce593447a517c2294a7a4c28d840 /client/src/app/shared/form-validators | |
parent | 1a95f0b9627f8016767a5a386620cbc3335d5f93 (diff) | |
download | PeerTube-7ed1edbbe4ffbef28093e4f5630751cb652814e4.tar.gz PeerTube-7ed1edbbe4ffbef28093e4f5630751cb652814e4.tar.zst PeerTube-7ed1edbbe4ffbef28093e4f5630751cb652814e4.zip |
We don't need services anymore for validators
Diffstat (limited to 'client/src/app/shared/form-validators')
17 files changed, 701 insertions, 0 deletions
diff --git a/client/src/app/shared/form-validators/abuse-validators.ts b/client/src/app/shared/form-validators/abuse-validators.ts new file mode 100644 index 000000000..75bfacf01 --- /dev/null +++ b/client/src/app/shared/form-validators/abuse-validators.ts | |||
@@ -0,0 +1,29 @@ | |||
1 | import { Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | |||
4 | export const ABUSE_REASON_VALIDATOR: BuildFormValidator = { | ||
5 | VALIDATORS: [Validators.required, Validators.minLength(2), Validators.maxLength(3000)], | ||
6 | MESSAGES: { | ||
7 | 'required': $localize`Report reason is required.`, | ||
8 | 'minlength': $localize`Report reason must be at least 2 characters long.`, | ||
9 | 'maxlength': $localize`Report reason cannot be more than 3000 characters long.` | ||
10 | } | ||
11 | } | ||
12 | |||
13 | export const ABUSE_MODERATION_COMMENT_VALIDATOR: BuildFormValidator = { | ||
14 | VALIDATORS: [Validators.required, Validators.minLength(2), Validators.maxLength(3000)], | ||
15 | MESSAGES: { | ||
16 | 'required': $localize`Moderation comment is required.`, | ||
17 | 'minlength': $localize`Moderation comment must be at least 2 characters long.`, | ||
18 | 'maxlength': $localize`Moderation comment cannot be more than 3000 characters long.` | ||
19 | } | ||
20 | } | ||
21 | |||
22 | export const ABUSE_MESSAGE_VALIDATOR: BuildFormValidator = { | ||
23 | VALIDATORS: [Validators.required, Validators.minLength(2), Validators.maxLength(3000)], | ||
24 | MESSAGES: { | ||
25 | 'required': $localize`Abuse message is required.`, | ||
26 | 'minlength': $localize`Abuse message must be at least 2 characters long.`, | ||
27 | 'maxlength': $localize`Abuse message cannot be more than 3000 characters long.` | ||
28 | } | ||
29 | } | ||
diff --git a/client/src/app/shared/form-validators/batch-domains-validators.ts b/client/src/app/shared/form-validators/batch-domains-validators.ts new file mode 100644 index 000000000..423d1337f --- /dev/null +++ b/client/src/app/shared/form-validators/batch-domains-validators.ts | |||
@@ -0,0 +1,60 @@ | |||
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/custom-config-validators.ts b/client/src/app/shared/form-validators/custom-config-validators.ts new file mode 100644 index 000000000..41b3cbba9 --- /dev/null +++ b/client/src/app/shared/form-validators/custom-config-validators.ts | |||
@@ -0,0 +1,80 @@ | |||
1 | import { Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | |||
4 | export const INSTANCE_NAME_VALIDATOR: BuildFormValidator = { | ||
5 | VALIDATORS: [Validators.required], | ||
6 | MESSAGES: { | ||
7 | 'required': $localize`Instance name is required.` | ||
8 | } | ||
9 | } | ||
10 | |||
11 | export const INSTANCE_SHORT_DESCRIPTION_VALIDATOR: BuildFormValidator = { | ||
12 | VALIDATORS: [Validators.max(250)], | ||
13 | MESSAGES: { | ||
14 | 'max': $localize`Short description should not be longer than 250 characters.` | ||
15 | } | ||
16 | } | ||
17 | |||
18 | export const SERVICES_TWITTER_USERNAME_VALIDATOR: BuildFormValidator = { | ||
19 | VALIDATORS: [Validators.required], | ||
20 | MESSAGES: { | ||
21 | 'required': $localize`Twitter username is required.` | ||
22 | } | ||
23 | } | ||
24 | |||
25 | export const CACHE_PREVIEWS_SIZE_VALIDATOR: BuildFormValidator = { | ||
26 | VALIDATORS: [Validators.required, Validators.min(1), Validators.pattern('[0-9]+')], | ||
27 | MESSAGES: { | ||
28 | 'required': $localize`Previews cache size is required.`, | ||
29 | 'min': $localize`Previews cache size must be greater than 1.`, | ||
30 | 'pattern': $localize`Previews cache size must be a number.` | ||
31 | } | ||
32 | } | ||
33 | |||
34 | export const CACHE_CAPTIONS_SIZE_VALIDATOR: BuildFormValidator = { | ||
35 | VALIDATORS: [Validators.required, Validators.min(1), Validators.pattern('[0-9]+')], | ||
36 | MESSAGES: { | ||
37 | 'required': $localize`Captions cache size is required.`, | ||
38 | 'min': $localize`Captions cache size must be greater than 1.`, | ||
39 | 'pattern': $localize`Captions cache size must be a number.` | ||
40 | } | ||
41 | } | ||
42 | |||
43 | export const SIGNUP_LIMIT_VALIDATOR: BuildFormValidator = { | ||
44 | VALIDATORS: [Validators.required, Validators.min(-1), Validators.pattern('-?[0-9]+')], | ||
45 | MESSAGES: { | ||
46 | 'required': $localize`Signup limit is required.`, | ||
47 | 'min': $localize`Signup limit must be greater than 1.`, | ||
48 | 'pattern': $localize`Signup limit must be a number.` | ||
49 | } | ||
50 | } | ||
51 | |||
52 | export const ADMIN_EMAIL_VALIDATOR: BuildFormValidator = { | ||
53 | VALIDATORS: [Validators.required, Validators.email], | ||
54 | MESSAGES: { | ||
55 | 'required': $localize`Admin email is required.`, | ||
56 | 'email': $localize`Admin email must be valid.` | ||
57 | } | ||
58 | } | ||
59 | |||
60 | export const TRANSCODING_THREADS_VALIDATOR: BuildFormValidator = { | ||
61 | VALIDATORS: [Validators.required, Validators.min(0)], | ||
62 | MESSAGES: { | ||
63 | 'required': $localize`Transcoding threads is required.`, | ||
64 | 'min': $localize`Transcoding threads must be greater or equal to 0.` | ||
65 | } | ||
66 | } | ||
67 | |||
68 | export const INDEX_URL_VALIDATOR: BuildFormValidator = { | ||
69 | VALIDATORS: [Validators.pattern(/^https:\/\//)], | ||
70 | MESSAGES: { | ||
71 | 'pattern': $localize`Index URL should be a URL` | ||
72 | } | ||
73 | } | ||
74 | |||
75 | export const SEARCH_INDEX_URL_VALIDATOR: BuildFormValidator = { | ||
76 | VALIDATORS: [Validators.pattern(/^https?:\/\//)], | ||
77 | MESSAGES: { | ||
78 | 'pattern': $localize`Search index URL should be a URL` | ||
79 | } | ||
80 | } | ||
diff --git a/client/src/app/shared/form-validators/form-validator.model.ts b/client/src/app/shared/form-validators/form-validator.model.ts new file mode 100644 index 000000000..248a3b1d3 --- /dev/null +++ b/client/src/app/shared/form-validators/form-validator.model.ts | |||
@@ -0,0 +1,14 @@ | |||
1 | import { ValidatorFn } from '@angular/forms' | ||
2 | |||
3 | export type BuildFormValidator = { | ||
4 | VALIDATORS: ValidatorFn[], | ||
5 | MESSAGES: { [ name: string ]: string } | ||
6 | } | ||
7 | |||
8 | export type BuildFormArgument = { | ||
9 | [ id: string ]: BuildFormValidator | BuildFormArgument | ||
10 | } | ||
11 | |||
12 | export type BuildFormDefaultValues = { | ||
13 | [ name: string ]: string | string[] | BuildFormDefaultValues | ||
14 | } | ||
diff --git a/client/src/app/shared/form-validators/host.ts b/client/src/app/shared/form-validators/host.ts new file mode 100644 index 000000000..c18a35f9b --- /dev/null +++ b/client/src/app/shared/form-validators/host.ts | |||
@@ -0,0 +1,8 @@ | |||
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 new file mode 100644 index 000000000..f621f03a4 --- /dev/null +++ b/client/src/app/shared/form-validators/index.ts | |||
@@ -0,0 +1,17 @@ | |||
1 | export * from './form-validator.model' | ||
2 | export * from './host' | ||
3 | |||
4 | // Don't re export const variables because webpack 4 cannot do tree shaking with them | ||
5 | // export * from './abuse-validators' | ||
6 | // export * from './batch-domains-validators' | ||
7 | // export * from './custom-config-validators' | ||
8 | // export * from './instance-validators' | ||
9 | // export * from './login-validators' | ||
10 | // export * from './reset-password-validators' | ||
11 | // export * from './user-validators' | ||
12 | // export * from './video-block-validators' | ||
13 | // export * from './video-captions-validators' | ||
14 | // export * from './video-channel-validators' | ||
15 | // export * from './video-comment-validators' | ||
16 | // export * from './video-playlist-validators' | ||
17 | // export * from './video-validators' | ||
diff --git a/client/src/app/shared/form-validators/instance-validators.ts b/client/src/app/shared/form-validators/instance-validators.ts new file mode 100644 index 000000000..a72e28ba0 --- /dev/null +++ b/client/src/app/shared/form-validators/instance-validators.ts | |||
@@ -0,0 +1,49 @@ | |||
1 | import { Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | |||
4 | export const FROM_EMAIL_VALIDATOR: BuildFormValidator = { | ||
5 | VALIDATORS: [Validators.required, Validators.email], | ||
6 | MESSAGES: { | ||
7 | 'required': $localize`Email is required.`, | ||
8 | 'email': $localize`Email must be valid.` | ||
9 | } | ||
10 | } | ||
11 | |||
12 | export const FROM_NAME_VALIDATOR: BuildFormValidator = { | ||
13 | VALIDATORS: [ | ||
14 | Validators.required, | ||
15 | Validators.minLength(1), | ||
16 | Validators.maxLength(120) | ||
17 | ], | ||
18 | MESSAGES: { | ||
19 | 'required': $localize`Your name is required.`, | ||
20 | 'minlength': $localize`Your name must be at least 1 character long.`, | ||
21 | 'maxlength': $localize`Your name cannot be more than 120 characters long.` | ||
22 | } | ||
23 | } | ||
24 | |||
25 | export const SUBJECT_VALIDATOR: BuildFormValidator = { | ||
26 | VALIDATORS: [ | ||
27 | Validators.required, | ||
28 | Validators.minLength(1), | ||
29 | Validators.maxLength(120) | ||
30 | ], | ||
31 | MESSAGES: { | ||
32 | 'required': $localize`A subject is required.`, | ||
33 | 'minlength': $localize`The subject must be at least 1 character long.`, | ||
34 | 'maxlength': $localize`The subject cannot be more than 120 characters long.` | ||
35 | } | ||
36 | } | ||
37 | |||
38 | export const BODY_VALIDATOR: BuildFormValidator = { | ||
39 | VALIDATORS: [ | ||
40 | Validators.required, | ||
41 | Validators.minLength(3), | ||
42 | Validators.maxLength(5000) | ||
43 | ], | ||
44 | MESSAGES: { | ||
45 | 'required': $localize`A message is required.`, | ||
46 | 'minlength': $localize`The message must be at least 3 characters long.`, | ||
47 | 'maxlength': $localize`The message cannot be more than 5000 characters long.` | ||
48 | } | ||
49 | } | ||
diff --git a/client/src/app/shared/form-validators/login-validators.ts b/client/src/app/shared/form-validators/login-validators.ts new file mode 100644 index 000000000..1ceae1be3 --- /dev/null +++ b/client/src/app/shared/form-validators/login-validators.ts | |||
@@ -0,0 +1,20 @@ | |||
1 | import { Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | |||
4 | export const LOGIN_USERNAME_VALIDATOR: BuildFormValidator = { | ||
5 | VALIDATORS: [ | ||
6 | Validators.required | ||
7 | ], | ||
8 | MESSAGES: { | ||
9 | 'required': $localize`Username is required.` | ||
10 | } | ||
11 | } | ||
12 | |||
13 | export const LOGIN_PASSWORD_VALIDATOR: BuildFormValidator = { | ||
14 | VALIDATORS: [ | ||
15 | Validators.required | ||
16 | ], | ||
17 | MESSAGES: { | ||
18 | 'required': $localize`Password is required.` | ||
19 | } | ||
20 | } | ||
diff --git a/client/src/app/shared/form-validators/reset-password-validators.ts b/client/src/app/shared/form-validators/reset-password-validators.ts new file mode 100644 index 000000000..b87f2eab9 --- /dev/null +++ b/client/src/app/shared/form-validators/reset-password-validators.ts | |||
@@ -0,0 +1,11 @@ | |||
1 | import { Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | |||
4 | export const RESET_PASSWORD_CONFIRM_VALIDATOR: BuildFormValidator = { | ||
5 | VALIDATORS: [ | ||
6 | Validators.required | ||
7 | ], | ||
8 | MESSAGES: { | ||
9 | 'required': $localize`Confirmation of the password is required.` | ||
10 | } | ||
11 | } | ||
diff --git a/client/src/app/shared/form-validators/user-validators.ts b/client/src/app/shared/form-validators/user-validators.ts new file mode 100644 index 000000000..18199505c --- /dev/null +++ b/client/src/app/shared/form-validators/user-validators.ts | |||
@@ -0,0 +1,144 @@ | |||
1 | import { Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | |||
4 | export const USER_USERNAME_VALIDATOR: BuildFormValidator = { | ||
5 | VALIDATORS: [ | ||
6 | Validators.required, | ||
7 | Validators.minLength(1), | ||
8 | Validators.maxLength(50), | ||
9 | Validators.pattern(/^[a-z0-9][a-z0-9._]*$/) | ||
10 | ], | ||
11 | MESSAGES: { | ||
12 | 'required': $localize`Username is required.`, | ||
13 | 'minlength': $localize`Username must be at least 1 character long.`, | ||
14 | 'maxlength': $localize`Username cannot be more than 50 characters long.`, | ||
15 | 'pattern': $localize`Username should be lowercase alphanumeric; dots and underscores are allowed.` | ||
16 | } | ||
17 | } | ||
18 | |||
19 | export const USER_CHANNEL_NAME_VALIDATOR: BuildFormValidator = { | ||
20 | VALIDATORS: [ | ||
21 | Validators.required, | ||
22 | Validators.minLength(1), | ||
23 | Validators.maxLength(50), | ||
24 | Validators.pattern(/^[a-z0-9][a-z0-9._]*$/) | ||
25 | ], | ||
26 | MESSAGES: { | ||
27 | 'required': $localize`Channel name is required.`, | ||
28 | 'minlength': $localize`Channel name must be at least 1 character long.`, | ||
29 | 'maxlength': $localize`Channel name cannot be more than 50 characters long.`, | ||
30 | 'pattern': $localize`Channel name should be lowercase alphanumeric; dots and underscores are allowed.` | ||
31 | } | ||
32 | } | ||
33 | |||
34 | export const USER_EMAIL_VALIDATOR: BuildFormValidator = { | ||
35 | VALIDATORS: [ Validators.required, Validators.email ], | ||
36 | MESSAGES: { | ||
37 | 'required': $localize`Email is required.`, | ||
38 | 'email': $localize`Email must be valid.` | ||
39 | } | ||
40 | } | ||
41 | |||
42 | export const USER_PASSWORD_VALIDATOR: BuildFormValidator = { | ||
43 | VALIDATORS: [ | ||
44 | Validators.required, | ||
45 | Validators.minLength(6), | ||
46 | Validators.maxLength(255) | ||
47 | ], | ||
48 | MESSAGES: { | ||
49 | 'required': $localize`Password is required.`, | ||
50 | 'minlength': $localize`Password must be at least 6 characters long.`, | ||
51 | 'maxlength': $localize`Password cannot be more than 255 characters long.` | ||
52 | } | ||
53 | } | ||
54 | |||
55 | export const USER_PASSWORD_OPTIONAL_VALIDATOR: BuildFormValidator = { | ||
56 | VALIDATORS: [ | ||
57 | Validators.minLength(6), | ||
58 | Validators.maxLength(255) | ||
59 | ], | ||
60 | MESSAGES: { | ||
61 | 'minlength': $localize`Password must be at least 6 characters long.`, | ||
62 | 'maxlength': $localize`Password cannot be more than 255 characters long.` | ||
63 | } | ||
64 | } | ||
65 | |||
66 | export const USER_CONFIRM_PASSWORD_VALIDATOR: BuildFormValidator = { | ||
67 | VALIDATORS: [], | ||
68 | MESSAGES: { | ||
69 | 'matchPassword': $localize`The new password and the confirmed password do not correspond.` | ||
70 | } | ||
71 | } | ||
72 | |||
73 | export const USER_VIDEO_QUOTA_VALIDATOR: BuildFormValidator = { | ||
74 | VALIDATORS: [ Validators.required, Validators.min(-1) ], | ||
75 | MESSAGES: { | ||
76 | 'required': $localize`Video quota is required.`, | ||
77 | 'min': $localize`Quota must be greater than -1.` | ||
78 | } | ||
79 | } | ||
80 | export const USER_VIDEO_QUOTA_DAILY_VALIDATOR: BuildFormValidator = { | ||
81 | VALIDATORS: [ Validators.required, Validators.min(-1) ], | ||
82 | MESSAGES: { | ||
83 | 'required': $localize`Daily upload limit is required.`, | ||
84 | 'min': $localize`Daily upload limit must be greater than -1.` | ||
85 | } | ||
86 | } | ||
87 | |||
88 | export const USER_ROLE_VALIDATOR: BuildFormValidator = { | ||
89 | VALIDATORS: [ Validators.required ], | ||
90 | MESSAGES: { | ||
91 | 'required': $localize`User role is required.` | ||
92 | } | ||
93 | } | ||
94 | |||
95 | export const USER_DISPLAY_NAME_REQUIRED_VALIDATOR = buildDisplayNameValidator(true) | ||
96 | |||
97 | export const USER_DESCRIPTION_VALIDATOR: BuildFormValidator = { | ||
98 | VALIDATORS: [ | ||
99 | Validators.minLength(3), | ||
100 | Validators.maxLength(1000) | ||
101 | ], | ||
102 | MESSAGES: { | ||
103 | 'minlength': $localize`Description must be at least 3 characters long.`, | ||
104 | 'maxlength': $localize`Description cannot be more than 1000 characters long.` | ||
105 | } | ||
106 | } | ||
107 | |||
108 | export const USER_TERMS_VALIDATOR: BuildFormValidator = { | ||
109 | VALIDATORS: [ | ||
110 | Validators.requiredTrue | ||
111 | ], | ||
112 | MESSAGES: { | ||
113 | 'required': $localize`You must agree with the instance terms in order to register on it.` | ||
114 | } | ||
115 | } | ||
116 | |||
117 | export const USER_BAN_REASON_VALIDATOR: BuildFormValidator = { | ||
118 | VALIDATORS: [ | ||
119 | Validators.minLength(3), | ||
120 | Validators.maxLength(250) | ||
121 | ], | ||
122 | MESSAGES: { | ||
123 | 'minlength': $localize`Ban reason must be at least 3 characters long.`, | ||
124 | 'maxlength': $localize`Ban reason cannot be more than 250 characters long.` | ||
125 | } | ||
126 | } | ||
127 | |||
128 | function buildDisplayNameValidator (required: boolean) { | ||
129 | const control = { | ||
130 | VALIDATORS: [ | ||
131 | Validators.minLength(1), | ||
132 | Validators.maxLength(120) | ||
133 | ], | ||
134 | MESSAGES: { | ||
135 | 'required': $localize`Display name is required.`, | ||
136 | 'minlength': $localize`Display name must be at least 1 character long.`, | ||
137 | 'maxlength': $localize`Display name cannot be more than 50 characters long.` | ||
138 | } | ||
139 | } | ||
140 | |||
141 | if (required) control.VALIDATORS.push(Validators.required) | ||
142 | |||
143 | return control | ||
144 | } | ||
diff --git a/client/src/app/shared/form-validators/video-block-validators.ts b/client/src/app/shared/form-validators/video-block-validators.ts new file mode 100644 index 000000000..d3974aefe --- /dev/null +++ b/client/src/app/shared/form-validators/video-block-validators.ts | |||
@@ -0,0 +1,10 @@ | |||
1 | import { Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | |||
4 | export const VIDEO_BLOCK_REASON_VALIDATOR: BuildFormValidator = { | ||
5 | VALIDATORS: [ Validators.minLength(2), Validators.maxLength(300) ], | ||
6 | MESSAGES: { | ||
7 | 'minlength': $localize`Block reason must be at least 2 characters long.`, | ||
8 | 'maxlength': $localize`Block reason cannot be more than 300 characters long.` | ||
9 | } | ||
10 | } | ||
diff --git a/client/src/app/shared/form-validators/video-captions-validators.ts b/client/src/app/shared/form-validators/video-captions-validators.ts new file mode 100644 index 000000000..9742d2925 --- /dev/null +++ b/client/src/app/shared/form-validators/video-captions-validators.ts | |||
@@ -0,0 +1,16 @@ | |||
1 | import { Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | |||
4 | export const VIDEO_CAPTION_LANGUAGE_VALIDATOR: BuildFormValidator = { | ||
5 | VALIDATORS: [ Validators.required ], | ||
6 | MESSAGES: { | ||
7 | 'required': $localize`Video caption language is required.` | ||
8 | } | ||
9 | } | ||
10 | |||
11 | export const VIDEO_CAPTION_FILE_VALIDATOR: BuildFormValidator = { | ||
12 | VALIDATORS: [ Validators.required ], | ||
13 | MESSAGES: { | ||
14 | 'required': $localize`Video caption file is required.` | ||
15 | } | ||
16 | } | ||
diff --git a/client/src/app/shared/form-validators/video-channel-validators.ts b/client/src/app/shared/form-validators/video-channel-validators.ts new file mode 100644 index 000000000..0daab22ce --- /dev/null +++ b/client/src/app/shared/form-validators/video-channel-validators.ts | |||
@@ -0,0 +1,52 @@ | |||
1 | import { Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | |||
4 | export const VIDEO_CHANNEL_NAME_VALIDATOR: BuildFormValidator = { | ||
5 | VALIDATORS: [ | ||
6 | Validators.required, | ||
7 | Validators.minLength(1), | ||
8 | Validators.maxLength(50), | ||
9 | Validators.pattern(/^[a-z0-9][a-z0-9._]*$/) | ||
10 | ], | ||
11 | MESSAGES: { | ||
12 | 'required': $localize`Name is required.`, | ||
13 | 'minlength': $localize`Name must be at least 1 character long.`, | ||
14 | 'maxlength': $localize`Name cannot be more than 50 characters long.`, | ||
15 | 'pattern': $localize`Name should be lowercase alphanumeric; dots and underscores are allowed.` | ||
16 | } | ||
17 | } | ||
18 | |||
19 | export const VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR: BuildFormValidator = { | ||
20 | VALIDATORS: [ | ||
21 | Validators.required, | ||
22 | Validators.minLength(1), | ||
23 | Validators.maxLength(50) | ||
24 | ], | ||
25 | MESSAGES: { | ||
26 | 'required': $localize`Display name is required.`, | ||
27 | 'minlength': $localize`Display name must be at least 1 character long.`, | ||
28 | 'maxlength': $localize`Display name cannot be more than 50 characters long.` | ||
29 | } | ||
30 | } | ||
31 | |||
32 | export const VIDEO_CHANNEL_DESCRIPTION_VALIDATOR: BuildFormValidator = { | ||
33 | VALIDATORS: [ | ||
34 | Validators.minLength(3), | ||
35 | Validators.maxLength(1000) | ||
36 | ], | ||
37 | MESSAGES: { | ||
38 | 'minlength': $localize`Description must be at least 3 characters long.`, | ||
39 | 'maxlength': $localize`Description cannot be more than 1000 characters long.` | ||
40 | } | ||
41 | } | ||
42 | |||
43 | export const VIDEO_CHANNEL_SUPPORT_VALIDATOR: BuildFormValidator = { | ||
44 | VALIDATORS: [ | ||
45 | Validators.minLength(3), | ||
46 | Validators.maxLength(1000) | ||
47 | ], | ||
48 | MESSAGES: { | ||
49 | 'minlength': $localize`Support text must be at least 3 characters long.`, | ||
50 | 'maxlength': $localize`Support text cannot be more than 1000 characters long` | ||
51 | } | ||
52 | } | ||
diff --git a/client/src/app/shared/form-validators/video-comment-validators.ts b/client/src/app/shared/form-validators/video-comment-validators.ts new file mode 100644 index 000000000..c56564d34 --- /dev/null +++ b/client/src/app/shared/form-validators/video-comment-validators.ts | |||
@@ -0,0 +1,11 @@ | |||
1 | import { Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | |||
4 | export const VIDEO_COMMENT_TEXT_VALIDATOR: BuildFormValidator = { | ||
5 | VALIDATORS: [ Validators.required, Validators.minLength(1), Validators.maxLength(3000) ], | ||
6 | MESSAGES: { | ||
7 | 'required': $localize`Comment is required.`, | ||
8 | 'minlength': $localize`Comment must be at least 2 characters long.`, | ||
9 | 'maxlength': $localize`Comment cannot be more than 3000 characters long.` | ||
10 | } | ||
11 | } | ||
diff --git a/client/src/app/shared/form-validators/video-ownership-change-validators.ts b/client/src/app/shared/form-validators/video-ownership-change-validators.ts new file mode 100644 index 000000000..e1a2df8a6 --- /dev/null +++ b/client/src/app/shared/form-validators/video-ownership-change-validators.ts | |||
@@ -0,0 +1,25 @@ | |||
1 | import { AbstractControl, ValidationErrors, Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | |||
4 | export const OWNERSHIP_CHANGE_CHANNEL_VALIDATOR: BuildFormValidator = { | ||
5 | VALIDATORS: [ Validators.required ], | ||
6 | MESSAGES: { | ||
7 | 'required': $localize`The channel is required.` | ||
8 | } | ||
9 | } | ||
10 | |||
11 | export const OWNERSHIP_CHANGE_USERNAME_VALIDATOR: BuildFormValidator = { | ||
12 | VALIDATORS: [ Validators.required, localAccountValidator ], | ||
13 | MESSAGES: { | ||
14 | 'required': $localize`The username is required.`, | ||
15 | 'localAccountOnly': $localize`You can only transfer ownership to a local account` | ||
16 | } | ||
17 | } | ||
18 | |||
19 | function localAccountValidator (control: AbstractControl): ValidationErrors { | ||
20 | if (control.value.includes('@')) { | ||
21 | return { 'localAccountOnly': true } | ||
22 | } | ||
23 | |||
24 | return null | ||
25 | } | ||
diff --git a/client/src/app/shared/form-validators/video-playlist-validators.ts b/client/src/app/shared/form-validators/video-playlist-validators.ts new file mode 100644 index 000000000..7e3d29458 --- /dev/null +++ b/client/src/app/shared/form-validators/video-playlist-validators.ts | |||
@@ -0,0 +1,54 @@ | |||
1 | import { Validators, AbstractControl } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | import { VideoPlaylistPrivacy } from '@shared/models' | ||
4 | |||
5 | export const VIDEO_PLAYLIST_DISPLAY_NAME_VALIDATOR: BuildFormValidator = { | ||
6 | VALIDATORS: [ | ||
7 | Validators.required, | ||
8 | Validators.minLength(1), | ||
9 | Validators.maxLength(120) | ||
10 | ], | ||
11 | MESSAGES: { | ||
12 | 'required': $localize`Display name is required.`, | ||
13 | 'minlength': $localize`Display name must be at least 1 character long.`, | ||
14 | 'maxlength': $localize`Display name cannot be more than 120 characters long.` | ||
15 | } | ||
16 | } | ||
17 | |||
18 | export const VIDEO_PLAYLIST_PRIVACY_VALIDATOR: BuildFormValidator = { | ||
19 | VALIDATORS: [ | ||
20 | Validators.required | ||
21 | ], | ||
22 | MESSAGES: { | ||
23 | 'required': $localize`Privacy is required.` | ||
24 | } | ||
25 | } | ||
26 | |||
27 | export const VIDEO_PLAYLIST_DESCRIPTION_VALIDATOR: BuildFormValidator = { | ||
28 | VALIDATORS: [ | ||
29 | Validators.minLength(3), | ||
30 | Validators.maxLength(1000) | ||
31 | ], | ||
32 | MESSAGES: { | ||
33 | 'minlength': $localize`Description must be at least 3 characters long.`, | ||
34 | 'maxlength': $localize`Description cannot be more than 1000 characters long.` | ||
35 | } | ||
36 | } | ||
37 | |||
38 | export const VIDEO_PLAYLIST_CHANNEL_ID_VALIDATOR: BuildFormValidator = { | ||
39 | VALIDATORS: [], | ||
40 | MESSAGES: { | ||
41 | 'required': $localize`The channel is required when the playlist is public.` | ||
42 | } | ||
43 | } | ||
44 | |||
45 | export function setPlaylistChannelValidator (channelControl: AbstractControl, privacy: VideoPlaylistPrivacy) { | ||
46 | if (privacy.toString() === VideoPlaylistPrivacy.PUBLIC.toString()) { | ||
47 | channelControl.setValidators([Validators.required]) | ||
48 | } else { | ||
49 | channelControl.setValidators(null) | ||
50 | } | ||
51 | |||
52 | channelControl.markAsDirty() | ||
53 | channelControl.updateValueAndValidity() | ||
54 | } | ||
diff --git a/client/src/app/shared/form-validators/video-validators.ts b/client/src/app/shared/form-validators/video-validators.ts new file mode 100644 index 000000000..23f2391b2 --- /dev/null +++ b/client/src/app/shared/form-validators/video-validators.ts | |||
@@ -0,0 +1,101 @@ | |||
1 | import { AbstractControl, ValidationErrors, ValidatorFn, Validators } from '@angular/forms' | ||
2 | import { BuildFormValidator } from './form-validator.model' | ||
3 | |||
4 | export const VIDEO_NAME_VALIDATOR: BuildFormValidator = { | ||
5 | VALIDATORS: [ Validators.required, Validators.minLength(3), Validators.maxLength(120) ], | ||
6 | MESSAGES: { | ||
7 | 'required': $localize`Video name is required.`, | ||
8 | 'minlength': $localize`Video name must be at least 3 characters long.`, | ||
9 | 'maxlength': $localize`Video name cannot be more than 120 characters long.` | ||
10 | } | ||
11 | } | ||
12 | |||
13 | export const VIDEO_PRIVACY_VALIDATOR: BuildFormValidator = { | ||
14 | VALIDATORS: [ Validators.required ], | ||
15 | MESSAGES: { | ||
16 | 'required': $localize`Video privacy is required.` | ||
17 | } | ||
18 | } | ||
19 | |||
20 | export const VIDEO_CATEGORY_VALIDATOR: BuildFormValidator = { | ||
21 | VALIDATORS: [ ], | ||
22 | MESSAGES: {} | ||
23 | } | ||
24 | |||
25 | export const VIDEO_LICENCE_VALIDATOR: BuildFormValidator = { | ||
26 | VALIDATORS: [ ], | ||
27 | MESSAGES: {} | ||
28 | } | ||
29 | |||
30 | export const VIDEO_LANGUAGE_VALIDATOR: BuildFormValidator = { | ||
31 | VALIDATORS: [ ], | ||
32 | MESSAGES: {} | ||
33 | } | ||
34 | |||
35 | export const VIDEO_IMAGE_VALIDATOR: BuildFormValidator = { | ||
36 | VALIDATORS: [ ], | ||
37 | MESSAGES: {} | ||
38 | } | ||
39 | |||
40 | export const VIDEO_CHANNEL_VALIDATOR: BuildFormValidator = { | ||
41 | VALIDATORS: [ Validators.required ], | ||
42 | MESSAGES: { | ||
43 | 'required': $localize`Video channel is required.` | ||
44 | } | ||
45 | } | ||
46 | |||
47 | export const VIDEO_DESCRIPTION_VALIDATOR: BuildFormValidator = { | ||
48 | VALIDATORS: [ Validators.minLength(3), Validators.maxLength(10000) ], | ||
49 | MESSAGES: { | ||
50 | 'minlength': $localize`Video description must be at least 3 characters long.`, | ||
51 | 'maxlength': $localize`Video description cannot be more than 10000 characters long.` | ||
52 | } | ||
53 | } | ||
54 | |||
55 | export const VIDEO_TAG_VALIDATOR: BuildFormValidator = { | ||
56 | VALIDATORS: [ Validators.minLength(2), Validators.maxLength(30) ], | ||
57 | MESSAGES: { | ||
58 | 'minlength': $localize`A tag should be more than 2 characters long.`, | ||
59 | 'maxlength': $localize`A tag should be less than 30 characters long.` | ||
60 | } | ||
61 | } | ||
62 | |||
63 | export const VIDEO_TAGS_ARRAY_VALIDATOR: BuildFormValidator = { | ||
64 | VALIDATORS: [ Validators.maxLength(5), arrayTagLengthValidator() ], | ||
65 | MESSAGES: { | ||
66 | 'maxlength': $localize`A maximum of 5 tags can be used on a video.`, | ||
67 | 'arrayTagLength': $localize`A tag should be more than 2, and less than 30 characters long.` | ||
68 | } | ||
69 | } | ||
70 | |||
71 | export const VIDEO_SUPPORT_VALIDATOR: BuildFormValidator = { | ||
72 | VALIDATORS: [ Validators.minLength(3), Validators.maxLength(1000) ], | ||
73 | MESSAGES: { | ||
74 | 'minlength': $localize`Video support must be at least 3 characters long.`, | ||
75 | 'maxlength': $localize`Video support cannot be more than 1000 characters long.` | ||
76 | } | ||
77 | } | ||
78 | |||
79 | export const VIDEO_SCHEDULE_PUBLICATION_AT_VALIDATOR: BuildFormValidator = { | ||
80 | VALIDATORS: [ ], | ||
81 | MESSAGES: { | ||
82 | 'required': $localize`A date is required to schedule video update.` | ||
83 | } | ||
84 | } | ||
85 | |||
86 | export const VIDEO_ORIGINALLY_PUBLISHED_AT_VALIDATOR: BuildFormValidator = { | ||
87 | VALIDATORS: [ ], | ||
88 | MESSAGES: {} | ||
89 | } | ||
90 | |||
91 | function arrayTagLengthValidator (min = 2, max = 30): ValidatorFn { | ||
92 | return (control: AbstractControl): ValidationErrors => { | ||
93 | const array = control.value as Array<string> | ||
94 | |||
95 | if (array.every(e => e.length > min && e.length < max)) { | ||
96 | return null | ||
97 | } | ||
98 | |||
99 | return { 'arrayTagLength': true } | ||
100 | } | ||
101 | } | ||