import joi from '@hapi/joi';
import globals from '../../globals';
export default class UserValidate { // validate input for user
static input (data) {
return joi.object({
name: joi.string()
.alphanum()
.lowercase()
.required(),
email: joi.string()
.email({minDomainSegments: 2})
pass: joi.string()
.pattern(new RegExp('^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!"#$%&\'()*+,-.\\/:;<=>?@[\\]^_`{|}~])(?=\\S+$).{8,}$'))
level: joi.string()
.valid(...globals.levels)
location: joi.string()
device_name: joi.string()
.allow('')
.required()
}).validate(data);
}
static output (data) { // validate output from database for needed properties, strip everything else
const {value, error} = joi.object({
_id: joi.any(),
name: joi.string(),
email: joi.string(),
level: joi.string(),
location: joi.string(),
device_name: joi.string().allow('')
}).validate(data, {stripUnknown: true})
return error !== undefined? null : value;