1 import { I18n } from '@ngx-translate/i18n-polyfill'
2 import { Validators } from '@angular/forms'
3 import { BuildFormValidator } from '@app/shared'
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_CONFIRM_PASSWORD: BuildFormValidator
12 readonly USER_VIDEO_QUOTA: BuildFormValidator
13 readonly USER_VIDEO_QUOTA_DAILY: BuildFormValidator
14 readonly USER_ROLE: BuildFormValidator
15 readonly USER_DISPLAY_NAME_REQUIRED: BuildFormValidator
16 readonly USER_DESCRIPTION: BuildFormValidator
17 readonly USER_TERMS: BuildFormValidator
19 readonly USER_BAN_REASON: BuildFormValidator
21 constructor (private i18n: I18n) {
23 this.USER_USERNAME = {
26 Validators.minLength(1),
27 Validators.maxLength(50),
28 Validators.pattern(/^[a-z0-9][a-z0-9._]*$/)
31 'required': this.i18n('Username is required.'),
32 'minlength': this.i18n('Username must be at least 1 character long.'),
33 'maxlength': this.i18n('Username cannot be more than 50 characters long.'),
34 'pattern': this.i18n('Username should be lowercase alphanumeric; dots and underscores are allowed.')
39 VALIDATORS: [ Validators.required, Validators.email ],
41 'required': this.i18n('Email is required.'),
42 'email': this.i18n('Email must be valid.')
46 this.USER_PASSWORD = {
49 Validators.minLength(6),
50 Validators.maxLength(255)
53 'required': this.i18n('Password is required.'),
54 'minlength': this.i18n('Password must be at least 6 characters long.'),
55 'maxlength': this.i18n('Password cannot be more than 255 characters long.')
59 this.USER_CONFIRM_PASSWORD = {
62 'matchPassword': this.i18n('The new password and the confirmed password do not correspond.')
66 this.USER_VIDEO_QUOTA = {
67 VALIDATORS: [ Validators.required, Validators.min(-1) ],
69 'required': this.i18n('Video quota is required.'),
70 'min': this.i18n('Quota must be greater than -1.')
73 this.USER_VIDEO_QUOTA_DAILY = {
74 VALIDATORS: [ Validators.required, Validators.min(-1) ],
76 'required': this.i18n('Daily upload limit is required.'),
77 'min': this.i18n('Daily upload limit must be greater than -1.')
82 VALIDATORS: [ Validators.required ],
84 'required': this.i18n('User role is required.')
88 this.USER_DISPLAY_NAME_REQUIRED = this.getDisplayName(true)
90 this.USER_DESCRIPTION = {
92 Validators.minLength(3),
93 Validators.maxLength(1000)
96 'minlength': this.i18n('Description must be at least 3 characters long.'),
97 'maxlength': this.i18n('Description cannot be more than 1000 characters long.')
103 Validators.requiredTrue
106 'required': this.i18n('You must to agree with the instance terms in order to registering on it.')
110 this.USER_BAN_REASON = {
112 Validators.minLength(3),
113 Validators.maxLength(250)
116 'minlength': this.i18n('Ban reason must be at least 3 characters long.'),
117 'maxlength': this.i18n('Ban reason cannot be more than 250 characters long.')
122 private getDisplayName (required: boolean) {
125 Validators.minLength(1),
126 Validators.maxLength(120)
129 'required': this.i18n('Display name is required.'),
130 'minlength': this.i18n('Display name must be at least 1 character long.'),
131 'maxlength': this.i18n('Display name cannot be more than 50 characters long.')
135 if (required) control.VALIDATORS.push(Validators.required)