2020-04-22 17:24:15 +02:00
|
|
|
import joi from '@hapi/joi';
|
2020-04-23 13:59:45 +02:00
|
|
|
import globals from '../../globals';
|
2020-04-22 17:24:15 +02:00
|
|
|
|
|
|
|
export default class UserValidate { // validate input for user
|
2020-04-24 17:36:39 +02:00
|
|
|
private static user = {
|
|
|
|
_id: joi.any(),
|
|
|
|
name: joi.string()
|
|
|
|
.alphanum()
|
|
|
|
.lowercase(),
|
|
|
|
|
|
|
|
email: joi.string()
|
|
|
|
.email({minDomainSegments: 2})
|
|
|
|
.lowercase(),
|
|
|
|
|
|
|
|
pass: joi.string()
|
|
|
|
.pattern(new RegExp('^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!"#%&\'()*+,-.\\/:;<=>?@[\\]^_`{|}~])(?=\\S+$).{8,}$')),
|
|
|
|
|
|
|
|
level: joi.string()
|
|
|
|
.valid(...globals.levels),
|
|
|
|
|
|
|
|
location: joi.string()
|
|
|
|
.alphanum(),
|
|
|
|
|
|
|
|
device_name: joi.string()
|
|
|
|
.allow('')
|
|
|
|
};
|
|
|
|
|
2020-04-27 15:10:14 +02:00
|
|
|
private static specialUsernames = ['admin', 'user', 'key', 'new', 'passreset']; // names a user cannot take
|
|
|
|
|
2020-04-24 17:36:39 +02:00
|
|
|
static input (data, param) {
|
|
|
|
if (param === 'new') {
|
|
|
|
return joi.object({
|
|
|
|
name: this.user.name.required(),
|
|
|
|
email: this.user.email.required(),
|
|
|
|
pass: this.user.pass.required(),
|
|
|
|
level: this.user.level.required(),
|
|
|
|
location: this.user.location.required(),
|
|
|
|
device_name: this.user.device_name.required()
|
|
|
|
}).validate(data);
|
|
|
|
}
|
|
|
|
else if (param === 'change') {
|
|
|
|
return joi.object({
|
|
|
|
name: this.user.name,
|
|
|
|
email: this.user.email,
|
|
|
|
pass: this.user.pass,
|
|
|
|
location: this.user.location,
|
|
|
|
device_name: this.user.device_name
|
|
|
|
}).validate(data);
|
|
|
|
}
|
|
|
|
else if (param === 'changeadmin') {
|
|
|
|
return joi.object({
|
|
|
|
name: this.user.name,
|
|
|
|
email: this.user.email,
|
|
|
|
pass: this.user.pass,
|
|
|
|
level: this.user.level,
|
|
|
|
location: this.user.location,
|
|
|
|
device_name: this.user.device_name
|
|
|
|
}).validate(data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return{error: 'No parameter specified!', value: {}};
|
|
|
|
}
|
2020-04-22 17:24:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static output (data) { // validate output from database for needed properties, strip everything else
|
2020-04-24 12:25:32 +02:00
|
|
|
const {value, error} = joi.object({
|
2020-04-22 17:24:15 +02:00
|
|
|
_id: joi.any(),
|
|
|
|
name: joi.string(),
|
|
|
|
email: joi.string(),
|
|
|
|
level: joi.string(),
|
|
|
|
location: joi.string(),
|
2020-04-24 12:25:32 +02:00
|
|
|
device_name: joi.string().allow('')
|
|
|
|
}).validate(data, {stripUnknown: true})
|
|
|
|
return error !== undefined? null : value;
|
2020-04-22 17:24:15 +02:00
|
|
|
}
|
2020-04-27 15:10:14 +02:00
|
|
|
|
|
|
|
static isSpecialName (name) { // true if name belongs to special names
|
|
|
|
return this.specialUsernames.indexOf(name) > -1;
|
|
|
|
}
|
2020-04-22 17:24:15 +02:00
|
|
|
}
|