]>
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 | |
9 | readonly USER_EMAIL: BuildFormValidator | |
10 | readonly USER_PASSWORD: BuildFormValidator | |
45f1bd72 | 11 | readonly USER_PASSWORD_OPTIONAL: BuildFormValidator |
b0ee41df | 12 | readonly USER_CONFIRM_PASSWORD: BuildFormValidator |
e309822b | 13 | readonly USER_VIDEO_QUOTA: BuildFormValidator |
bee0abff | 14 | readonly USER_VIDEO_QUOTA_DAILY: BuildFormValidator |
e309822b | 15 | readonly USER_ROLE: BuildFormValidator |
1f20622f | 16 | readonly USER_DISPLAY_NAME_REQUIRED: BuildFormValidator |
e309822b | 17 | readonly USER_DESCRIPTION: BuildFormValidator |
b4a929ac | 18 | readonly USER_TERMS: BuildFormValidator |
e309822b | 19 | |
141b177d C |
20 | readonly USER_BAN_REASON: BuildFormValidator |
21 | ||
e309822b C |
22 | constructor (private i18n: I18n) { |
23 | ||
24 | this.USER_USERNAME = { | |
25 | VALIDATORS: [ | |
26 | Validators.required, | |
d0ce42c1 BY |
27 | Validators.minLength(1), |
28 | Validators.maxLength(50), | |
6ebfaf67 | 29 | Validators.pattern(/^[a-z0-9][a-z0-9._]*$/) |
e309822b C |
30 | ], |
31 | MESSAGES: { | |
32 | 'required': this.i18n('Username is required.'), | |
d0ce42c1 BY |
33 | 'minlength': this.i18n('Username must be at least 1 character long.'), |
34 | 'maxlength': this.i18n('Username cannot be more than 50 characters long.'), | |
4523bf11 | 35 | 'pattern': this.i18n('Username should be lowercase alphanumeric; dots and underscores are allowed.') |
e309822b C |
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 | ||
45f1bd72 JL |
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 | ||
b0ee41df C |
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 | ||
e309822b C |
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 | } | |
bee0abff FA |
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 | } | |
e309822b C |
92 | |
93 | this.USER_ROLE = { | |
94 | VALIDATORS: [ Validators.required ], | |
95 | MESSAGES: { | |
96 | 'required': this.i18n('User role is required.') | |
97 | } | |
98 | } | |
99 | ||
1f20622f | 100 | this.USER_DISPLAY_NAME_REQUIRED = this.getDisplayName(true) |
e309822b C |
101 | |
102 | this.USER_DESCRIPTION = { | |
103 | VALIDATORS: [ | |
104 | Validators.minLength(3), | |
d23e6a1c | 105 | Validators.maxLength(1000) |
e309822b C |
106 | ], |
107 | MESSAGES: { | |
108 | 'minlength': this.i18n('Description must be at least 3 characters long.'), | |
d23e6a1c | 109 | 'maxlength': this.i18n('Description cannot be more than 1000 characters long.') |
e309822b C |
110 | } |
111 | } | |
b4a929ac C |
112 | |
113 | this.USER_TERMS = { | |
114 | VALIDATORS: [ | |
115 | Validators.requiredTrue | |
116 | ], | |
117 | MESSAGES: { | |
32d7f2b7 | 118 | 'required': this.i18n('You must agree with the instance terms in order to register on it.') |
b4a929ac C |
119 | } |
120 | } | |
141b177d C |
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 | } | |
e309822b | 132 | } |
1f20622f C |
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 | } | |
e309822b | 151 | } |