1
0
Fork 0
peertube/server/helpers/custom-validators/users.ts

35 lines
1 KiB
TypeScript
Raw Normal View History

2017-05-15 16:22:03 -04:00
import { values } from 'lodash'
import expressValidator = require('express-validator')
// TODO: use .validator when express-validator typing will have validator field
const validator = expressValidator['validator']
2016-07-31 14:58:43 -04:00
2017-05-15 16:22:03 -04:00
import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers'
const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
2016-07-31 14:58:43 -04:00
function isUserPasswordValid (value) {
return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
}
function isUserRoleValid (value) {
2017-05-15 16:22:03 -04:00
return values(USER_ROLES).indexOf(value) !== -1
}
2016-07-31 14:58:43 -04:00
function isUserUsernameValid (value) {
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 14:58:43 -04:00
}
2017-04-03 15:24:36 -04:00
function isUserDisplayNSFWValid (value) {
return validator.isBoolean(value)
}
2016-07-31 14:58:43 -04:00
// ---------------------------------------------------------------------------
2017-05-15 16:22:03 -04:00
export {
isUserPasswordValid,
isUserRoleValid,
isUserUsernameValid,
isUserDisplayNSFWValid
}