Archived
2
This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
definma-api/src/routes/validate/user.ts

92 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-05-07 21:55:29 +02:00
import Joi from '@hapi/joi';
2020-04-23 13:59:45 +02:00
import globals from '../../globals';
2020-04-29 12:10:27 +02:00
import IdValidate from './id';
export default class UserValidate { // validate input for user
2020-04-24 17:36:39 +02:00
private static user = {
name: Joi.string()
2020-04-29 12:10:27 +02:00
.lowercase()
.pattern(new RegExp('^[a-z0-9-_.]+$'))
2020-04-29 12:10:27 +02:00
.max(128),
2020-04-24 17:36:39 +02:00
2020-05-07 21:55:29 +02:00
email: Joi.string()
2020-04-24 17:36:39 +02:00
.email({minDomainSegments: 2})
2020-04-29 12:10:27 +02:00
.lowercase()
.max(128),
2020-04-24 17:36:39 +02:00
2020-05-07 21:55:29 +02:00
pass: Joi.string()
.pattern(/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!"#%&'()*+,-.\/:;<=>?@[\]^_`{|}~])(?=\S+$)[a-zA-Z0-9!"#%&'()*+,\-.\/:;<=>?@[\]^_`{|}~]{8,}$/)
2020-04-29 12:10:27 +02:00
.max(128),
2020-04-24 17:36:39 +02:00
2020-05-07 21:55:29 +02:00
level: Joi.string()
2020-04-24 17:36:39 +02:00
.valid(...globals.levels),
2020-05-07 21:55:29 +02:00
location: Joi.string()
2020-04-29 12:10:27 +02:00
.alphanum()
.max(128),
2020-04-24 17:36:39 +02:00
2020-05-07 21:55:29 +02:00
device_name: Joi.string()
2020-04-24 17:36:39 +02:00
.allow('')
2020-04-29 12:10:27 +02:00
.max(128),
2020-04-24 17:36:39 +02:00
};
2020-04-27 15:10:14 +02:00
private static specialUsernames = ['admin', 'user', 'key', 'new', 'passreset']; // names a user cannot take
2020-05-18 14:47:22 +02:00
static input (data, param) { // validate input, set param to 'new' to make all attributes required
2020-04-24 17:36:39 +02:00
if (param === 'new') {
2020-05-07 21:55:29 +02:00
return Joi.object({
2020-04-24 17:36:39 +02:00
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') {
2020-05-07 21:55:29 +02:00
return Joi.object({
2020-04-24 17:36:39 +02:00
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') {
2020-05-07 21:55:29 +02:00
return Joi.object({
2020-04-24 17:36:39 +02:00
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-05-18 14:47:22 +02:00
static output (data) { // validate output and strip unwanted properties, returns null if not valid
2020-05-06 14:39:04 +02:00
data = IdValidate.stringify(data);
2020-05-07 21:55:29 +02:00
const {value, error} = Joi.object({
2020-04-29 12:10:27 +02:00
_id: IdValidate.get(),
name: this.user.name,
email: this.user.email,
level: this.user.level,
location: this.user.location,
device_name: this.user.device_name
}).validate(data, {stripUnknown: true});
2020-04-24 12:25:32 +02:00
return error !== undefined? null : value;
}
2020-04-27 15:10:14 +02:00
static isSpecialName (name) { // true if name belongs to special names
return this.specialUsernames.indexOf(name) > -1;
}
static username() {
return this.user.name;
}
}