aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/users.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-06-10 22:15:25 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-06-10 22:15:25 +0200
commit69818c9394366b954b6ba3bd697bd9d2b09f2a16 (patch)
treead199a18ec3c322460d6f9523fc383ee562554e0 /server/helpers/custom-validators/users.ts
parent4d4e5cd4dca78480ec7f40e747f424cd107376a4 (diff)
downloadPeerTube-69818c9394366b954b6ba3bd697bd9d2b09f2a16.tar.gz
PeerTube-69818c9394366b954b6ba3bd697bd9d2b09f2a16.tar.zst
PeerTube-69818c9394366b954b6ba3bd697bd9d2b09f2a16.zip
Type functions
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}