diff options
Diffstat (limited to 'server/helpers/custom-validators/users.ts')
-rw-r--r-- | server/helpers/custom-validators/users.ts | 24 |
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 @@ | |||
1 | import { values } from 'lodash' | 1 | import { values } from 'lodash' |
2 | import * as validator from 'validator' | 2 | import * as validator from 'validator' |
3 | 3 | ||
4 | import { exists } from './misc' | ||
4 | import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers' | 5 | import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers' |
5 | const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS | 6 | const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS |
6 | 7 | ||
7 | function isUserPasswordValid (value) { | 8 | function 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 | ||
11 | function isUserRoleValid (value) { | 12 | function isUserRoleValid (value: string) { |
12 | return values(USER_ROLES).indexOf(value) !== -1 | 13 | return values(USER_ROLES).indexOf(value) !== -1 |
13 | } | 14 | } |
14 | 15 | ||
15 | function isUserUsernameValid (value) { | 16 | function 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 | ||
21 | function isUserDisplayNSFWValid (value) { | 22 | function 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 | |||
35 | declare global { | ||
36 | namespace ExpressValidator { | ||
37 | export interface Validator { | ||
38 | isUserPasswordValid, | ||
39 | isUserRoleValid, | ||
40 | isUserUsernameValid, | ||
41 | isUserDisplayNSFWValid | ||
42 | } | ||
43 | } | ||
44 | } | ||