]>
Commit | Line | Data |
---|---|---|
e309822b C |
1 | import { I18n } from '@ngx-translate/i18n-polyfill' |
2 | import { Validators } from '@angular/forms' | |
67ed6552 | 3 | import { BuildFormValidator } from './form-validator.service' |
e309822b C |
4 | import { Injectable } from '@angular/core' |
5 | ||
6 | @Injectable() | |
7 | export class UserValidatorsService { | |
8 | readonly USER_USERNAME: BuildFormValidator | |
4abe9c59 | 9 | readonly USER_CHANNEL_NAME: BuildFormValidator |
e309822b C |
10 | readonly USER_EMAIL: BuildFormValidator |
11 | readonly USER_PASSWORD: BuildFormValidator | |
45f1bd72 | 12 | readonly USER_PASSWORD_OPTIONAL: BuildFormValidator |
b0ee41df | 13 | readonly USER_CONFIRM_PASSWORD: BuildFormValidator |
e309822b | 14 | readonly USER_VIDEO_QUOTA: BuildFormValidator |
bee0abff | 15 | readonly USER_VIDEO_QUOTA_DAILY: BuildFormValidator |
e309822b | 16 | readonly USER_ROLE: BuildFormValidator |
1f20622f | 17 | readonly USER_DISPLAY_NAME_REQUIRED: BuildFormValidator |
e309822b | 18 | readonly USER_DESCRIPTION: BuildFormValidator |
b4a929ac | 19 | readonly USER_TERMS: BuildFormValidator |
e309822b | 20 | |
141b177d C |
21 | readonly USER_BAN_REASON: BuildFormValidator |
22 | ||
e309822b C |
23 | constructor (private i18n: I18n) { |
24 | ||
25 | this.USER_USERNAME = { | |
26 | VALIDATORS: [ | |
27 | Validators.required, | |
d0ce42c1 BY |
28 | Validators.minLength(1), |
29 | Validators.maxLength(50), | |
6ebfaf67 | 30 | Validators.pattern(/^[a-z0-9][a-z0-9._]*$/) |
e309822b C |
31 | ], |
32 | MESSAGES: { | |
33 | 'required': this.i18n('Username is required.'), | |
d0ce42c1 BY |
34 | 'minlength': this.i18n('Username must be at least 1 character long.'), |
35 | 'maxlength': this.i18n('Username cannot be more than 50 characters long.'), | |
4523bf11 | 36 | 'pattern': this.i18n('Username should be lowercase alphanumeric; dots and underscores are allowed.') |
e309822b C |
37 | } |
38 | } | |
39 | ||
4abe9c59 | 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 | ||
e309822b C |
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 | ||
45f1bd72 JL |
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 | ||
b0ee41df C |
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 | ||
e309822b C |
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 | } | |
bee0abff FA |
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 | } | |
e309822b C |
108 | |
109 | this.USER_ROLE = { | |
110 | VALIDATORS: [ Validators.required ], | |
111 | MESSAGES: { | |
112 | 'required': this.i18n('User role is required.') | |
113 | } | |
114 | } | |
115 | ||
1f20622f | 116 | this.USER_DISPLAY_NAME_REQUIRED = this.getDisplayName(true) |
e309822b C |
117 | |
118 | this.USER_DESCRIPTION = { | |
119 | VALIDATORS: [ | |
120 | Validators.minLength(3), | |
d23e6a1c | 121 | Validators.maxLength(1000) |
e309822b C |
122 | ], |
123 | MESSAGES: { | |
124 | 'minlength': this.i18n('Description must be at least 3 characters long.'), | |
d23e6a1c | 125 | 'maxlength': this.i18n('Description cannot be more than 1000 characters long.') |
e309822b C |
126 | } |
127 | } | |
b4a929ac C |
128 | |
129 | this.USER_TERMS = { | |
130 | VALIDATORS: [ | |
131 | Validators.requiredTrue | |
132 | ], | |
133 | MESSAGES: { | |
32d7f2b7 | 134 | 'required': this.i18n('You must agree with the instance terms in order to register on it.') |
b4a929ac C |
135 | } |
136 | } | |
141b177d C |
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 | } | |
e309822b | 148 | } |
1f20622f C |
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 | } | |
e309822b | 167 | } |