]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/users.js
Implement user API (create, update, remove, list)
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / users.js
1 'use strict'
2
3 const validator = require('express-validator').validator
4 const values = require('lodash/values')
5
6 const constants = require('../../initializers/constants')
7 const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS
8
9 const usersValidators = {
10 isUserPasswordValid: isUserPasswordValid,
11 isUserRoleValid: isUserRoleValid,
12 isUserUsernameValid: isUserUsernameValid
13 }
14
15 function isUserPasswordValid (value) {
16 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
17 }
18
19 function isUserRoleValid (value) {
20 return values(constants.USER_ROLES).indexOf(value) !== -1
21 }
22
23 function isUserUsernameValid (value) {
24 const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
25 const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
26 return validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`))
27 }
28
29 // ---------------------------------------------------------------------------
30
31 module.exports = usersValidators