]>
Commit | Line | Data |
---|---|---|
e309822b | 1 | import { I18n } from '@ngx-translate/i18n-polyfill' |
02c01341 | 2 | import { Validators, ValidatorFn, ValidationErrors, AbstractControl } from '@angular/forms' |
e309822b | 3 | import { Injectable } from '@angular/core' |
67ed6552 | 4 | import { BuildFormValidator } from './form-validator.service' |
e309822b C |
5 | |
6 | @Injectable() | |
7 | export class VideoValidatorsService { | |
8 | readonly VIDEO_NAME: BuildFormValidator | |
9 | readonly VIDEO_PRIVACY: BuildFormValidator | |
10 | readonly VIDEO_CATEGORY: BuildFormValidator | |
11 | readonly VIDEO_LICENCE: BuildFormValidator | |
12 | readonly VIDEO_LANGUAGE: BuildFormValidator | |
13 | readonly VIDEO_IMAGE: BuildFormValidator | |
14 | readonly VIDEO_CHANNEL: BuildFormValidator | |
15 | readonly VIDEO_DESCRIPTION: BuildFormValidator | |
02c01341 RK |
16 | readonly VIDEO_TAGS_ARRAY: BuildFormValidator |
17 | readonly VIDEO_TAG: BuildFormValidator | |
e309822b | 18 | readonly VIDEO_SUPPORT: BuildFormValidator |
bbe0f064 | 19 | readonly VIDEO_SCHEDULE_PUBLICATION_AT: BuildFormValidator |
1e74f19a | 20 | readonly VIDEO_ORIGINALLY_PUBLISHED_AT: BuildFormValidator |
e309822b C |
21 | |
22 | constructor (private i18n: I18n) { | |
23 | ||
24 | this.VIDEO_NAME = { | |
25 | VALIDATORS: [ Validators.required, Validators.minLength(3), Validators.maxLength(120) ], | |
26 | MESSAGES: { | |
27 | 'required': this.i18n('Video name is required.'), | |
28 | 'minlength': this.i18n('Video name must be at least 3 characters long.'), | |
29 | 'maxlength': this.i18n('Video name cannot be more than 120 characters long.') | |
30 | } | |
31 | } | |
32 | ||
33 | this.VIDEO_PRIVACY = { | |
34 | VALIDATORS: [ Validators.required ], | |
35 | MESSAGES: { | |
36 | 'required': this.i18n('Video privacy is required.') | |
37 | } | |
38 | } | |
39 | ||
40 | this.VIDEO_CATEGORY = { | |
41 | VALIDATORS: [ ], | |
42 | MESSAGES: {} | |
43 | } | |
44 | ||
45 | this.VIDEO_LICENCE = { | |
46 | VALIDATORS: [ ], | |
47 | MESSAGES: {} | |
48 | } | |
49 | ||
50 | this.VIDEO_LANGUAGE = { | |
51 | VALIDATORS: [ ], | |
52 | MESSAGES: {} | |
53 | } | |
54 | ||
55 | this.VIDEO_IMAGE = { | |
56 | VALIDATORS: [ ], | |
57 | MESSAGES: {} | |
58 | } | |
59 | ||
60 | this.VIDEO_CHANNEL = { | |
61 | VALIDATORS: [ Validators.required ], | |
62 | MESSAGES: { | |
63 | 'required': this.i18n('Video channel is required.') | |
64 | } | |
65 | } | |
66 | ||
67 | this.VIDEO_DESCRIPTION = { | |
68 | VALIDATORS: [ Validators.minLength(3), Validators.maxLength(10000) ], | |
69 | MESSAGES: { | |
70 | 'minlength': this.i18n('Video description must be at least 3 characters long.'), | |
71 | 'maxlength': this.i18n('Video description cannot be more than 10000 characters long.') | |
72 | } | |
73 | } | |
74 | ||
02c01341 | 75 | this.VIDEO_TAG = { |
e309822b C |
76 | VALIDATORS: [ Validators.minLength(2), Validators.maxLength(30) ], |
77 | MESSAGES: { | |
78 | 'minlength': this.i18n('A tag should be more than 2 characters long.'), | |
79 | 'maxlength': this.i18n('A tag should be less than 30 characters long.') | |
80 | } | |
81 | } | |
82 | ||
02c01341 RK |
83 | this.VIDEO_TAGS_ARRAY = { |
84 | VALIDATORS: [ Validators.maxLength(5), this.arrayTagLengthValidator() ], | |
85 | MESSAGES: { | |
86 | 'maxlength': this.i18n('A maximum of 5 tags can be used on a video.'), | |
87 | 'arrayTagLength': this.i18n('A tag should be more than 2, and less than 30 characters long.') | |
88 | } | |
89 | } | |
90 | ||
e309822b | 91 | this.VIDEO_SUPPORT = { |
d23e6a1c | 92 | VALIDATORS: [ Validators.minLength(3), Validators.maxLength(1000) ], |
e309822b C |
93 | MESSAGES: { |
94 | 'minlength': this.i18n('Video support must be at least 3 characters long.'), | |
d23e6a1c | 95 | 'maxlength': this.i18n('Video support cannot be more than 1000 characters long.') |
e309822b C |
96 | } |
97 | } | |
bbe0f064 C |
98 | |
99 | this.VIDEO_SCHEDULE_PUBLICATION_AT = { | |
100 | VALIDATORS: [ ], | |
101 | MESSAGES: { | |
102 | 'required': this.i18n('A date is required to schedule video update.') | |
103 | } | |
104 | } | |
1e74f19a | 105 | |
106 | this.VIDEO_ORIGINALLY_PUBLISHED_AT = { | |
107 | VALIDATORS: [ ], | |
108 | MESSAGES: {} | |
109 | } | |
e309822b | 110 | } |
02c01341 RK |
111 | |
112 | arrayTagLengthValidator (min = 2, max = 30): ValidatorFn { | |
113 | return (control: AbstractControl): ValidationErrors => { | |
114 | const array = control.value as Array<string> | |
115 | ||
116 | if (array.every(e => e.length > min && e.length < max)) { | |
117 | return null | |
118 | } | |
119 | ||
120 | return { 'arrayTagLength': true } | |
121 | } | |
122 | } | |
e309822b | 123 | } |