2016-07-31 20:58:43 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const validator = require('express-validator').validator
|
2016-08-04 22:32:36 +02:00
|
|
|
const values = require('lodash/values')
|
2016-07-31 20:58:43 +02:00
|
|
|
|
|
|
|
const constants = require('../../initializers/constants')
|
|
|
|
const USERS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.USERS
|
|
|
|
|
|
|
|
const usersValidators = {
|
2016-08-04 22:32:36 +02:00
|
|
|
isUserPasswordValid: isUserPasswordValid,
|
|
|
|
isUserRoleValid: isUserRoleValid,
|
2016-07-31 20:58:43 +02:00
|
|
|
isUserUsernameValid: isUserUsernameValid
|
|
|
|
}
|
|
|
|
|
2016-08-04 22:32:36 +02:00
|
|
|
function isUserPasswordValid (value) {
|
|
|
|
return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isUserRoleValid (value) {
|
|
|
|
return values(constants.USER_ROLES).indexOf(value) !== -1
|
|
|
|
}
|
|
|
|
|
2016-07-31 20:58:43 +02:00
|
|
|
function isUserUsernameValid (value) {
|
2016-08-04 22:32:36 +02:00
|
|
|
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}}$`))
|
2016-07-31 20:58:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
module.exports = usersValidators
|