aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/form-validators/user-validators.ts
blob: ed6e0582eaad8b4078742c208ca9a8ebc607de22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { Validators } from '@angular/forms'
import { BuildFormValidator } from './form-validator.model'

export const USER_USERNAME_REGEX_CHARACTERS = '[a-z0-9][a-z0-9._]'

export const USER_USERNAME_VALIDATOR: BuildFormValidator = {
  VALIDATORS: [
    Validators.required,
    Validators.minLength(1),
    Validators.maxLength(50),
    Validators.pattern(new RegExp(`^${USER_USERNAME_REGEX_CHARACTERS}*$`))
  ],
  MESSAGES: {
    required: $localize`Username is required.`,
    minlength: $localize`Username must be at least 1 character long.`,
    maxlength: $localize`Username cannot be more than 50 characters long.`,
    pattern: $localize`Username should be lowercase alphanumeric; dots and underscores are allowed.`
  }
}

export const USER_CHANNEL_NAME_VALIDATOR: BuildFormValidator = {
  VALIDATORS: [
    Validators.required,
    Validators.minLength(1),
    Validators.maxLength(50),
    Validators.pattern(/^[a-z0-9][a-z0-9._]*$/)
  ],
  MESSAGES: {
    required: $localize`Channel name is required.`,
    minlength: $localize`Channel name must be at least 1 character long.`,
    maxlength: $localize`Channel name cannot be more than 50 characters long.`,
    pattern: $localize`Channel name should be lowercase, and can contain only alphanumeric characters, dots and underscores.`
  }
}

export const USER_EMAIL_VALIDATOR: BuildFormValidator = {
  VALIDATORS: [ Validators.required, Validators.email ],
  MESSAGES: {
    required: $localize`Email is required.`,
    email: $localize`Email must be valid.`
  }
}

export const USER_HANDLE_VALIDATOR: BuildFormValidator = {
  VALIDATORS: [
    Validators.required,
    Validators.pattern(/@.+/)
  ],
  MESSAGES: {
    required: $localize`Handle is required.`,
    pattern: $localize`Handle must be valid (eg. chocobozzz@example.com).`
  }
}

export const USER_EXISTING_PASSWORD_VALIDATOR: BuildFormValidator = {
  VALIDATORS: [
    Validators.required
  ],
  MESSAGES: {
    required: $localize`Password is required.`
  }
}

export const USER_OTP_TOKEN_VALIDATOR: BuildFormValidator = {
  VALIDATORS: [
    Validators.required
  ],
  MESSAGES: {
    required: $localize`OTP token is required.`
  }
}

export const USER_PASSWORD_VALIDATOR = {
  VALIDATORS: [
    Validators.required,
    Validators.minLength(6),
    Validators.maxLength(255)
  ],
  MESSAGES: {
    required: $localize`Password is required.`,
    minlength: $localize`Password must be at least 6 characters long.`,
    maxlength: $localize`Password cannot be more than 255 characters long.`
  }
}

export const USER_PASSWORD_OPTIONAL_VALIDATOR: BuildFormValidator = {
  VALIDATORS: [
    Validators.minLength(6),
    Validators.maxLength(255)
  ],
  MESSAGES: {
    minlength: $localize`Password must be at least 6 characters long.`,
    maxlength: $localize`Password cannot be more than 255 characters long.`
  }
}

export const USER_CONFIRM_PASSWORD_VALIDATOR: BuildFormValidator = {
  VALIDATORS: [],
  MESSAGES: {
    matchPassword: $localize`The new password and the confirmed password do not correspond.`
  }
}

export const USER_VIDEO_QUOTA_VALIDATOR: BuildFormValidator = {
  VALIDATORS: [ Validators.required, Validators.min(-1) ],
  MESSAGES: {
    required: $localize`Video quota is required.`,
    min: $localize`Quota must be greater than -1.`
  }
}
export const USER_VIDEO_QUOTA_DAILY_VALIDATOR: BuildFormValidator = {
  VALIDATORS: [ Validators.required, Validators.min(-1) ],
  MESSAGES: {
    required: $localize`Daily upload limit is required.`,
    min: $localize`Daily upload limit must be greater than -1.`
  }
}

export const USER_ROLE_VALIDATOR: BuildFormValidator = {
  VALIDATORS: [ Validators.required ],
  MESSAGES: {
    required: $localize`User role is required.`
  }
}

export const USER_DISPLAY_NAME_REQUIRED_VALIDATOR = buildDisplayNameValidator(true)

export const USER_DESCRIPTION_VALIDATOR: BuildFormValidator = {
  VALIDATORS: [
    Validators.minLength(3),
    Validators.maxLength(1000)
  ],
  MESSAGES: {
    minlength: $localize`Description must be at least 3 characters long.`,
    maxlength: $localize`Description cannot be more than 1000 characters long.`
  }
}

export const USER_BAN_REASON_VALIDATOR: BuildFormValidator = {
  VALIDATORS: [
    Validators.minLength(3),
    Validators.maxLength(250)
  ],
  MESSAGES: {
    minlength: $localize`Ban reason must be at least 3 characters long.`,
    maxlength: $localize`Ban reason cannot be more than 250 characters long.`
  }
}

function buildDisplayNameValidator (required: boolean) {
  const control = {
    VALIDATORS: [
      Validators.minLength(1),
      Validators.maxLength(120)
    ],
    MESSAGES: {
      required: $localize`Display name is required.`,
      minlength: $localize`Display name must be at least 1 character long.`,
      maxlength: $localize`Display name cannot be more than 50 characters long.`
    }
  }

  if (required) control.VALIDATORS.push(Validators.required)

  return control
}