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_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
20 readonly USER_BAN_REASON: BuildFormValidator
22 constructor (private i18n: I18n) {
24 this.USER_USERNAME = {
27 Validators.minLength(1),
28 Validators.maxLength(50),
29 Validators.pattern(/^[a-z0-9][a-z0-9._]*$/)
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.')
40 VALIDATORS: [ Validators.required, Validators.email ],
42 'required': this.i18n('Email is required.'),
43 'email': this.i18n('Email must be valid.')
47 this.USER_PASSWORD = {
50 Validators.minLength(6),
51 Validators.maxLength(255)
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.')
60 this.USER_PASSWORD_OPTIONAL = {
62 Validators.minLength(6),
63 Validators.maxLength(255)
66 'minlength': this.i18n('Password must be at least 6 characters long.'),
67 'maxlength': this.i18n('Password cannot be more than 255 characters long.')
71 this.USER_CONFIRM_PASSWORD = {
74 'matchPassword': this.i18n('The new password and the confirmed password do not correspond.')
78 this.USER_VIDEO_QUOTA = {
79 VALIDATORS: [ Validators.required, Validators.min(-1) ],
81 'required': this.i18n('Video quota is required.'),
82 'min': this.i18n('Quota must be greater than -1.')
85 this.USER_VIDEO_QUOTA_DAILY = {
86 VALIDATORS: [ Validators.required, Validators.min(-1) ],
88 'required': this.i18n('Daily upload limit is required.'),
89 'min': this.i18n('Daily upload limit must be greater than -1.')
94 VALIDATORS: [ Validators.required ],
96 'required': this.i18n('User role is required.')
100 this.USER_DISPLAY_NAME_REQUIRED = this.getDisplayName(true)
102 this.USER_DESCRIPTION = {
104 Validators.minLength(3),
105 Validators.maxLength(1000)
108 'minlength': this.i18n('Description must be at least 3 characters long.'),
109 'maxlength': this.i18n('Description cannot be more than 1000 characters long.')
115 Validators.requiredTrue
118 'required': this.i18n('You must agree with the instance terms in order to register on it.')
122 this.USER_BAN_REASON = {
124 Validators.minLength(3),
125 Validators.maxLength(250)
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.')
134 private getDisplayName (required: boolean) {
137 Validators.minLength(1),
138 Validators.maxLength(120)
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.')
147 if (required) control.VALIDATORS.push(Validators.required)