]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/users.ts
Type functions
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / users.ts
1 import { values } from 'lodash'
2 import * as validator from 'validator'
3
4 import { exists } from './misc'
5 import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers'
6 const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
7
8 function isUserPasswordValid (value: string) {
9 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
10 }
11
12 function isUserRoleValid (value: string) {
13 return values(USER_ROLES).indexOf(value) !== -1
14 }
15
16 function isUserUsernameValid (value: string) {
17 const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
18 const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
19 return exists(value) && validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`))
20 }
21
22 function isUserDisplayNSFWValid (value: any) {
23 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
24 }
25
26 // ---------------------------------------------------------------------------
27
28 export {
29 isUserPasswordValid,
30 isUserRoleValid,
31 isUserUsernameValid,
32 isUserDisplayNSFWValid
33 }
34
35 declare global {
36 namespace ExpressValidator {
37 export interface Validator {
38 isUserPasswordValid,
39 isUserRoleValid,
40 isUserUsernameValid,
41 isUserDisplayNSFWValid
42 }
43 }
44 }