]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { values } from 'lodash' |
4d4e5cd4 | 2 | import * as validator from 'validator' |
e4c55619 | 3 | |
69818c93 | 4 | import { exists } from './misc' |
65fcc311 | 5 | import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers' |
ee9e7b61 C |
6 | import { UserRole } from '../../../shared' |
7 | ||
65fcc311 | 8 | const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS |
e4c55619 | 9 | |
69818c93 | 10 | function isUserPasswordValid (value: string) { |
9bd26629 C |
11 | return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD) |
12 | } | |
13 | ||
69818c93 | 14 | function isUserRoleValid (value: string) { |
ee9e7b61 | 15 | return values(USER_ROLES).indexOf(value as UserRole) !== -1 |
9bd26629 C |
16 | } |
17 | ||
b0f9f39e C |
18 | function isUserVideoQuotaValid (value: string) { |
19 | return exists(value) && validator.isInt(value + '', USERS_CONSTRAINTS_FIELDS.VIDEO_QUOTA) | |
20 | } | |
21 | ||
69818c93 | 22 | function isUserUsernameValid (value: string) { |
9bd26629 C |
23 | const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max |
24 | const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min | |
69818c93 | 25 | return exists(value) && validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`)) |
e4c55619 C |
26 | } |
27 | ||
69818c93 C |
28 | function isUserDisplayNSFWValid (value: any) { |
29 | return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) | |
1d49e1e2 C |
30 | } |
31 | ||
e4c55619 C |
32 | // --------------------------------------------------------------------------- |
33 | ||
65fcc311 C |
34 | export { |
35 | isUserPasswordValid, | |
36 | isUserRoleValid, | |
b0f9f39e | 37 | isUserVideoQuotaValid, |
65fcc311 C |
38 | isUserUsernameValid, |
39 | isUserDisplayNSFWValid | |
40 | } | |
69818c93 | 41 | |
556ddc31 C |
42 | declare module 'express-validator' { |
43 | export interface Validator { | |
44 | isUserPasswordValid, | |
45 | isUserRoleValid, | |
46 | isUserUsernameValid, | |
b0f9f39e C |
47 | isUserDisplayNSFWValid, |
48 | isUserVideoQuotaValid | |
69818c93 C |
49 | } |
50 | } |