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