1 import { values } from 'lodash'
2 import * as validator from 'validator'
4 import { exists } from './misc'
5 import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers'
6 const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
8 function isUserPasswordValid (value: string) {
9 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
12 function isUserRoleValid (value: string) {
13 return values(USER_ROLES).indexOf(value) !== -1
16 function isUserUsernameValid (value: string) {
17 const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
18 const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
19 return exists(value) && validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`))
22 function isUserDisplayNSFWValid (value: any) {
23 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
26 // ---------------------------------------------------------------------------
32 isUserDisplayNSFWValid
36 namespace ExpressValidator {
37 export interface Validator {
41 isUserDisplayNSFWValid