aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/users.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/custom-validators/users.ts')
-rw-r--r--server/helpers/custom-validators/users.ts24
1 files changed, 18 insertions, 6 deletions
diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts
index f303ab8db..7792ffd74 100644
--- a/server/helpers/custom-validators/users.ts
+++ b/server/helpers/custom-validators/users.ts
@@ -1,25 +1,26 @@
1import { values } from 'lodash' 1import { values } from 'lodash'
2import * as validator from 'validator' 2import * as validator from 'validator'
3 3
4import { exists } from './misc'
4import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers' 5import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers'
5const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS 6const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
6 7
7function isUserPasswordValid (value) { 8function isUserPasswordValid (value: string) {
8 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD) 9 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
9} 10}
10 11
11function isUserRoleValid (value) { 12function isUserRoleValid (value: string) {
12 return values(USER_ROLES).indexOf(value) !== -1 13 return values(USER_ROLES).indexOf(value) !== -1
13} 14}
14 15
15function isUserUsernameValid (value) { 16function isUserUsernameValid (value: string) {
16 const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max 17 const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
17 const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min 18 const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
18 return validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`)) 19 return exists(value) && validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`))
19} 20}
20 21
21function isUserDisplayNSFWValid (value) { 22function isUserDisplayNSFWValid (value: any) {
22 return validator.isBoolean(value) 23 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
23} 24}
24 25
25// --------------------------------------------------------------------------- 26// ---------------------------------------------------------------------------
@@ -30,3 +31,14 @@ export {
30 isUserUsernameValid, 31 isUserUsernameValid,
31 isUserDisplayNSFWValid 32 isUserDisplayNSFWValid
32} 33}
34
35declare global {
36 namespace ExpressValidator {
37 export interface Validator {
38 isUserPasswordValid,
39 isUserRoleValid,
40 isUserUsernameValid,
41 isUserDisplayNSFWValid
42 }
43 }
44}