]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/users.ts
f303ab8db2f5be4ea43bc69ee0663ce06d687371
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / users.ts
1 import { values } from 'lodash'
2 import * as validator from 'validator'
3
4 import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers'
5 const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
6
7 function isUserPasswordValid (value) {
8 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
9 }
10
11 function isUserRoleValid (value) {
12 return values(USER_ROLES).indexOf(value) !== -1
13 }
14
15 function isUserUsernameValid (value) {
16 const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
17 const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
18 return validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`))
19 }
20
21 function isUserDisplayNSFWValid (value) {
22 return validator.isBoolean(value)
23 }
24
25 // ---------------------------------------------------------------------------
26
27 export {
28 isUserPasswordValid,
29 isUserRoleValid,
30 isUserUsernameValid,
31 isUserDisplayNSFWValid
32 }