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'
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
21 readonly USER_BAN_REASON: BuildFormValidator
23 constructor (private i18n: I18n) {
25 this.USER_USERNAME = {
28 Validators.minLength(1),
29 Validators.maxLength(50),
30 Validators.pattern(/^[a-z0-9][a-z0-9._]*$/)
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.')
40 this.USER_CHANNEL_NAME = {
43 Validators.minLength(1),
44 Validators.maxLength(50),
45 Validators.pattern(/^[a-z0-9][a-z0-9._]*$/)
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.')
56 VALIDATORS: [ Validators.required, Validators.email ],
58 'required': this.i18n('Email is required.'),
59 'email': this.i18n('Email must be valid.')
63 this.USER_PASSWORD = {
66 Validators.minLength(6),
67 Validators.maxLength(255)
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.')
76 this.USER_PASSWORD_OPTIONAL = {
78 Validators.minLength(6),
79 Validators.maxLength(255)
82 'minlength': this.i18n('Password must be at least 6 characters long.'),
83 'maxlength': this.i18n('Password cannot be more than 255 characters long.')
87 this.USER_CONFIRM_PASSWORD = {
90 'matchPassword': this.i18n('The new password and the confirmed password do not correspond.')
94 this.USER_VIDEO_QUOTA = {
95 VALIDATORS: [ Validators.required, Validators.min(-1) ],
97 'required': this.i18n('Video quota is required.'),
98 'min': this.i18n('Quota must be greater than -1.')
101 this.USER_VIDEO_QUOTA_DAILY = {
102 VALIDATORS: [ Validators.required, Validators.min(-1) ],
104 'required': this.i18n('Daily upload limit is required.'),
105 'min': this.i18n('Daily upload limit must be greater than -1.')
110 VALIDATORS: [ Validators.required ],
112 'required': this.i18n('User role is required.')
116 this.USER_DISPLAY_NAME_REQUIRED = this.getDisplayName(true)
118 this.USER_DESCRIPTION = {
120 Validators.minLength(3),
121 Validators.maxLength(1000)
124 'minlength': this.i18n('Description must be at least 3 characters long.'),
125 'maxlength': this.i18n('Description cannot be more than 1000 characters long.')
131 Validators.requiredTrue
134 'required': this.i18n('You must agree with the instance terms in order to register on it.')
138 this.USER_BAN_REASON = {
140 Validators.minLength(3),
141 Validators.maxLength(250)
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.')
150 private getDisplayName (required: boolean) {
153 Validators.minLength(1),
154 Validators.maxLength(120)
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.')
163 if (required) control.VALIDATORS.push(Validators.required)