From 69818c9394366b954b6ba3bd697bd9d2b09f2a16 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 10 Jun 2017 22:15:25 +0200 Subject: Type functions --- server/helpers/custom-validators/users.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'server/helpers/custom-validators/users.ts') 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 @@ import { values } from 'lodash' import * as validator from 'validator' +import { exists } from './misc' import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers' const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS -function isUserPasswordValid (value) { +function isUserPasswordValid (value: string) { return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD) } -function isUserRoleValid (value) { +function isUserRoleValid (value: string) { return values(USER_ROLES).indexOf(value) !== -1 } -function isUserUsernameValid (value) { +function isUserUsernameValid (value: string) { const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min - return validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`)) + return exists(value) && validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`)) } -function isUserDisplayNSFWValid (value) { - return validator.isBoolean(value) +function isUserDisplayNSFWValid (value: any) { + return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) } // --------------------------------------------------------------------------- @@ -30,3 +31,14 @@ export { isUserUsernameValid, isUserDisplayNSFWValid } + +declare global { + namespace ExpressValidator { + export interface Validator { + isUserPasswordValid, + isUserRoleValid, + isUserUsernameValid, + isUserDisplayNSFWValid + } + } +} -- cgit v1.2.3