]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-forms/form-validators/user-validators.service.ts
Move form modules in the form shared module
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-forms / form-validators / user-validators.service.ts
1 import { I18n } from '@ngx-translate/i18n-polyfill'
2 import { Validators } from '@angular/forms'
3 import { BuildFormValidator } from './form-validator.service'
4 import { Injectable } from '@angular/core'
5
6 @Injectable()
7 export class UserValidatorsService {
8 readonly USER_USERNAME: BuildFormValidator
9 readonly USER_CHANNEL_NAME: BuildFormValidator
10 readonly USER_EMAIL: BuildFormValidator
11 readonly USER_PASSWORD: BuildFormValidator
12 readonly USER_PASSWORD_OPTIONAL: BuildFormValidator
13 readonly USER_CONFIRM_PASSWORD: BuildFormValidator
14 readonly USER_VIDEO_QUOTA: BuildFormValidator
15 readonly USER_VIDEO_QUOTA_DAILY: BuildFormValidator
16 readonly USER_ROLE: BuildFormValidator
17 readonly USER_DISPLAY_NAME_REQUIRED: BuildFormValidator
18 readonly USER_DESCRIPTION: BuildFormValidator
19 readonly USER_TERMS: BuildFormValidator
20
21 readonly USER_BAN_REASON: BuildFormValidator
22
23 constructor (private i18n: I18n) {
24
25 this.USER_USERNAME = {
26 VALIDATORS: [
27 Validators.required,
28 Validators.minLength(1),
29 Validators.maxLength(50),
30 Validators.pattern(/^[a-z0-9][a-z0-9._]*$/)
31 ],
32 MESSAGES: {
33 'required': this.i18n('Username is required.'),
34 'minlength': this.i18n('Username must be at least 1 character long.'),
35 'maxlength': this.i18n('Username cannot be more than 50 characters long.'),
36 'pattern': this.i18n('Username should be lowercase alphanumeric; dots and underscores are allowed.')
37 }
38 }
39
40 this.USER_CHANNEL_NAME = {
41 VALIDATORS: [
42 Validators.required,
43 Validators.minLength(1),
44 Validators.maxLength(50),
45 Validators.pattern(/^[a-z0-9][a-z0-9._]*$/)
46 ],
47 MESSAGES: {
48 'required': this.i18n('Channel name is required.'),
49 'minlength': this.i18n('Channel name must be at least 1 character long.'),
50 'maxlength': this.i18n('Channel name cannot be more than 50 characters long.'),
51 'pattern': this.i18n('Channel name should be lowercase alphanumeric; dots and underscores are allowed.')
52 }
53 }
54
55 this.USER_EMAIL = {
56 VALIDATORS: [ Validators.required, Validators.email ],
57 MESSAGES: {
58 'required': this.i18n('Email is required.'),
59 'email': this.i18n('Email must be valid.')
60 }
61 }
62
63 this.USER_PASSWORD = {
64 VALIDATORS: [
65 Validators.required,
66 Validators.minLength(6),
67 Validators.maxLength(255)
68 ],
69 MESSAGES: {
70 'required': this.i18n('Password is required.'),
71 'minlength': this.i18n('Password must be at least 6 characters long.'),
72 'maxlength': this.i18n('Password cannot be more than 255 characters long.')
73 }
74 }
75
76 this.USER_PASSWORD_OPTIONAL = {
77 VALIDATORS: [
78 Validators.minLength(6),
79 Validators.maxLength(255)
80 ],
81 MESSAGES: {
82 'minlength': this.i18n('Password must be at least 6 characters long.'),
83 'maxlength': this.i18n('Password cannot be more than 255 characters long.')
84 }
85 }
86
87 this.USER_CONFIRM_PASSWORD = {
88 VALIDATORS: [],
89 MESSAGES: {
90 'matchPassword': this.i18n('The new password and the confirmed password do not correspond.')
91 }
92 }
93
94 this.USER_VIDEO_QUOTA = {
95 VALIDATORS: [ Validators.required, Validators.min(-1) ],
96 MESSAGES: {
97 'required': this.i18n('Video quota is required.'),
98 'min': this.i18n('Quota must be greater than -1.')
99 }
100 }
101 this.USER_VIDEO_QUOTA_DAILY = {
102 VALIDATORS: [ Validators.required, Validators.min(-1) ],
103 MESSAGES: {
104 'required': this.i18n('Daily upload limit is required.'),
105 'min': this.i18n('Daily upload limit must be greater than -1.')
106 }
107 }
108
109 this.USER_ROLE = {
110 VALIDATORS: [ Validators.required ],
111 MESSAGES: {
112 'required': this.i18n('User role is required.')
113 }
114 }
115
116 this.USER_DISPLAY_NAME_REQUIRED = this.getDisplayName(true)
117
118 this.USER_DESCRIPTION = {
119 VALIDATORS: [
120 Validators.minLength(3),
121 Validators.maxLength(1000)
122 ],
123 MESSAGES: {
124 'minlength': this.i18n('Description must be at least 3 characters long.'),
125 'maxlength': this.i18n('Description cannot be more than 1000 characters long.')
126 }
127 }
128
129 this.USER_TERMS = {
130 VALIDATORS: [
131 Validators.requiredTrue
132 ],
133 MESSAGES: {
134 'required': this.i18n('You must agree with the instance terms in order to register on it.')
135 }
136 }
137
138 this.USER_BAN_REASON = {
139 VALIDATORS: [
140 Validators.minLength(3),
141 Validators.maxLength(250)
142 ],
143 MESSAGES: {
144 'minlength': this.i18n('Ban reason must be at least 3 characters long.'),
145 'maxlength': this.i18n('Ban reason cannot be more than 250 characters long.')
146 }
147 }
148 }
149
150 private getDisplayName (required: boolean) {
151 const control = {
152 VALIDATORS: [
153 Validators.minLength(1),
154 Validators.maxLength(120)
155 ],
156 MESSAGES: {
157 'required': this.i18n('Display name is required.'),
158 'minlength': this.i18n('Display name must be at least 1 character long.'),
159 'maxlength': this.i18n('Display name cannot be more than 50 characters long.')
160 }
161 }
162
163 if (required) control.VALIDATORS.push(Validators.required)
164
165 return control
166 }
167 }