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

47 lines
1.2 KiB
TypeScript
Raw Normal View History

import joi from '@hapi/joi';
2020-04-23 13:59:45 +02:00
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})
.lowercase()
.required(),
pass: joi.string()
.pattern(new RegExp('^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!"#$%&\'()*+,-.\\/:;<=>?@[\\]^_`{|}~])(?=\\S+$).{8,}$'))
.required(),
level: joi.string()
.valid(...globals.levels)
.required(),
location: joi.string()
.alphanum()
.required(),
device_name: joi.string()
2020-04-23 13:59:45 +02:00
.allow('')
.required()
}).validate(data);
}
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({
_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;
}
}