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

45 lines
1.1 KiB
TypeScript
Raw Normal View History

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})
.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()
.required()
}).validate(data);
}
static output (data) { // validate output from database for needed properties, strip everything else
return joi.object({
_id: joi.any(),
name: joi.string(),
email: joi.string(),
level: joi.string(),
location: joi.string(),
device_name: joi.string()
}).validate(data, {stripUnknown: true}).value;
}
}