aboutsummaryrefslogblamecommitdiffhomepage
path: root/server/helpers/custom-validators/users.ts
blob: 7792ffd74eb82865f65dede8f37a36a6373b5dc7 (plain) (tree)
1
2
3
4
5
6
7
8
                               
                                      
 
                               

                                                                   
 
                                              


                                                                     
                                          
                                                 

 
                                              

                                                   
                                                                                                

 

                                                                                                

 

                                                                              





                        










                                
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: string) {
  return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
}

function isUserRoleValid (value: string) {
  return values(USER_ROLES).indexOf(value) !== -1
}

function isUserUsernameValid (value: string) {
  const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
  const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
  return exists(value) && validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`))
}

function isUserDisplayNSFWValid (value: any) {
  return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
}

// ---------------------------------------------------------------------------

export {
  isUserPasswordValid,
  isUserRoleValid,
  isUserUsernameValid,
  isUserDisplayNSFWValid
}

declare global {
  namespace ExpressValidator {
    export interface Validator {
      isUserPasswordValid,
      isUserRoleValid,
      isUserUsernameValid,
      isUserDisplayNSFWValid
    }
  }
}