diff options
Diffstat (limited to 'server/helpers/custom-validators/users.js')
-rw-r--r-- | server/helpers/custom-validators/users.js | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/users.js b/server/helpers/custom-validators/users.js new file mode 100644 index 000000000..88fa1592e --- /dev/null +++ b/server/helpers/custom-validators/users.js | |||
@@ -0,0 +1,31 @@ | |||
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, | ||
11 | isUserRoleValid, | ||
12 | 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 | ||