]>
Commit | Line | Data |
---|---|---|
b58a894b | 1 | import { AbstractControl, ValidationErrors, ValidatorFn, Validators, FormControl } from '@angular/forms' |
7ed1edbb C |
2 | import { BuildFormValidator } from './form-validator.model' |
3 | ||
b58a894b R |
4 | export const trimValidator: ValidatorFn = (control: FormControl) => { |
5 | if (control.value.startsWith(' ') || control.value.endsWith(' ')) { | |
6 | return { | |
7 | 'spaces': true | |
8 | } | |
9 | } | |
10 | return null; | |
11 | }; | |
12 | ||
7ed1edbb | 13 | export const VIDEO_NAME_VALIDATOR: BuildFormValidator = { |
b58a894b | 14 | VALIDATORS: [ Validators.required, Validators.minLength(3), Validators.maxLength(120), trimValidator ], |
7ed1edbb C |
15 | MESSAGES: { |
16 | 'required': $localize`Video name is required.`, | |
17 | 'minlength': $localize`Video name must be at least 3 characters long.`, | |
b58a894b R |
18 | 'maxlength': $localize`Video name cannot be more than 120 characters long.`, |
19 | 'spaces': $localize`Video name has leading or trailing whitespace.` | |
7ed1edbb C |
20 | } |
21 | } | |
22 | ||
23 | export const VIDEO_PRIVACY_VALIDATOR: BuildFormValidator = { | |
24 | VALIDATORS: [ Validators.required ], | |
25 | MESSAGES: { | |
26 | 'required': $localize`Video privacy is required.` | |
27 | } | |
28 | } | |
29 | ||
30 | export const VIDEO_CATEGORY_VALIDATOR: BuildFormValidator = { | |
31 | VALIDATORS: [ ], | |
32 | MESSAGES: {} | |
33 | } | |
34 | ||
35 | export const VIDEO_LICENCE_VALIDATOR: BuildFormValidator = { | |
36 | VALIDATORS: [ ], | |
37 | MESSAGES: {} | |
38 | } | |
39 | ||
40 | export const VIDEO_LANGUAGE_VALIDATOR: BuildFormValidator = { | |
41 | VALIDATORS: [ ], | |
42 | MESSAGES: {} | |
43 | } | |
44 | ||
45 | export const VIDEO_IMAGE_VALIDATOR: BuildFormValidator = { | |
46 | VALIDATORS: [ ], | |
47 | MESSAGES: {} | |
48 | } | |
49 | ||
50 | export const VIDEO_CHANNEL_VALIDATOR: BuildFormValidator = { | |
51 | VALIDATORS: [ Validators.required ], | |
52 | MESSAGES: { | |
53 | 'required': $localize`Video channel is required.` | |
54 | } | |
55 | } | |
56 | ||
57 | export const VIDEO_DESCRIPTION_VALIDATOR: BuildFormValidator = { | |
58 | VALIDATORS: [ Validators.minLength(3), Validators.maxLength(10000) ], | |
59 | MESSAGES: { | |
60 | 'minlength': $localize`Video description must be at least 3 characters long.`, | |
61 | 'maxlength': $localize`Video description cannot be more than 10000 characters long.` | |
62 | } | |
63 | } | |
64 | ||
65 | export const VIDEO_TAG_VALIDATOR: BuildFormValidator = { | |
66 | VALIDATORS: [ Validators.minLength(2), Validators.maxLength(30) ], | |
67 | MESSAGES: { | |
68 | 'minlength': $localize`A tag should be more than 2 characters long.`, | |
69 | 'maxlength': $localize`A tag should be less than 30 characters long.` | |
70 | } | |
71 | } | |
72 | ||
73 | export const VIDEO_TAGS_ARRAY_VALIDATOR: BuildFormValidator = { | |
74 | VALIDATORS: [ Validators.maxLength(5), arrayTagLengthValidator() ], | |
75 | MESSAGES: { | |
76 | 'maxlength': $localize`A maximum of 5 tags can be used on a video.`, | |
4ddcb7c3 | 77 | 'arrayTagLength': $localize`A tag should be more than 1 and less than 30 characters long.` |
7ed1edbb C |
78 | } |
79 | } | |
80 | ||
81 | export const VIDEO_SUPPORT_VALIDATOR: BuildFormValidator = { | |
82 | VALIDATORS: [ Validators.minLength(3), Validators.maxLength(1000) ], | |
83 | MESSAGES: { | |
84 | 'minlength': $localize`Video support must be at least 3 characters long.`, | |
85 | 'maxlength': $localize`Video support cannot be more than 1000 characters long.` | |
86 | } | |
87 | } | |
88 | ||
89 | export const VIDEO_SCHEDULE_PUBLICATION_AT_VALIDATOR: BuildFormValidator = { | |
90 | VALIDATORS: [ ], | |
91 | MESSAGES: { | |
92 | 'required': $localize`A date is required to schedule video update.` | |
93 | } | |
94 | } | |
95 | ||
96 | export const VIDEO_ORIGINALLY_PUBLISHED_AT_VALIDATOR: BuildFormValidator = { | |
97 | VALIDATORS: [ ], | |
98 | MESSAGES: {} | |
99 | } | |
100 | ||
101 | function arrayTagLengthValidator (min = 2, max = 30): ValidatorFn { | |
102 | return (control: AbstractControl): ValidationErrors => { | |
103 | const array = control.value as Array<string> | |
104 | ||
4ddcb7c3 | 105 | if (array.every(e => e.length >= min && e.length <= max)) { |
7ed1edbb C |
106 | return null |
107 | } | |
108 | ||
109 | return { 'arrayTagLength': true } | |
110 | } | |
111 | } |