]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as validator from 'validator' |
fdbda9e3 | 2 | import 'express-validator' |
e4c55619 | 3 | |
c5911fd3 | 4 | import { exists, isArray } from './misc' |
954605a8 | 5 | import { CONSTRAINTS_FIELDS } 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 | ||
b0f9f39e C |
14 | function isUserVideoQuotaValid (value: string) { |
15 | return exists(value) && validator.isInt(value + '', USERS_CONSTRAINTS_FIELDS.VIDEO_QUOTA) | |
16 | } | |
17 | ||
69818c93 | 18 | function isUserUsernameValid (value: string) { |
9bd26629 C |
19 | const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max |
20 | const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min | |
563d032e | 21 | return exists(value) && validator.matches(value, new RegExp(`^[a-z0-9._]{${min},${max}}$`)) |
e4c55619 C |
22 | } |
23 | ||
7efe153b | 24 | function isBoolean (value: any) { |
69818c93 | 25 | return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) |
1d49e1e2 C |
26 | } |
27 | ||
7efe153b AL |
28 | function isUserDisplayNSFWValid (value: any) { |
29 | return isBoolean(value) | |
30 | } | |
31 | ||
32 | function isUserAutoPlayVideoValid (value: any) { | |
33 | return isBoolean(value) | |
34 | } | |
35 | ||
954605a8 C |
36 | function isUserRoleValid (value: any) { |
37 | return exists(value) && validator.isInt('' + value) && UserRole[value] !== undefined | |
38 | } | |
39 | ||
c5911fd3 C |
40 | function isAvatarFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { |
41 | // Should have files | |
42 | if (!files) return false | |
43 | if (isArray(files)) return false | |
44 | ||
45 | // Should have videofile file | |
46 | const avatarfile = files['avatarfile'] | |
47 | if (!avatarfile || avatarfile.length === 0) return false | |
48 | ||
49 | // The file should exist | |
50 | const file = avatarfile[0] | |
51 | if (!file || !file.originalname) return false | |
52 | ||
53 | return new RegExp('^image/(png|jpeg)$', 'i').test(file.mimetype) | |
54 | } | |
55 | ||
e4c55619 C |
56 | // --------------------------------------------------------------------------- |
57 | ||
65fcc311 C |
58 | export { |
59 | isUserPasswordValid, | |
60 | isUserRoleValid, | |
b0f9f39e | 61 | isUserVideoQuotaValid, |
65fcc311 | 62 | isUserUsernameValid, |
7efe153b | 63 | isUserDisplayNSFWValid, |
c5911fd3 C |
64 | isUserAutoPlayVideoValid, |
65 | isAvatarFile | |
65fcc311 | 66 | } |