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