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