2020-05-15 15:14:18 +02:00
|
|
|
import Joi from '@hapi/joi';
|
2020-05-04 15:48:07 +02:00
|
|
|
import IdValidate from './id';
|
|
|
|
|
|
|
|
export default class TemplateValidate {
|
|
|
|
private static template = {
|
2020-05-15 15:14:18 +02:00
|
|
|
name: Joi.string()
|
2020-05-04 15:48:07 +02:00
|
|
|
.max(128),
|
|
|
|
|
2020-05-15 15:14:18 +02:00
|
|
|
version: Joi.number()
|
2020-05-14 12:31:57 +02:00
|
|
|
.min(1),
|
|
|
|
|
2020-05-15 15:14:18 +02:00
|
|
|
number_prefix: Joi.string()
|
|
|
|
.pattern(/^[a-zA-Z]+$/)
|
2020-05-14 12:31:57 +02:00
|
|
|
.min(1)
|
|
|
|
.max(16),
|
|
|
|
|
2020-05-15 15:14:18 +02:00
|
|
|
parameters: Joi.array()
|
2020-05-04 15:48:07 +02:00
|
|
|
.min(1)
|
|
|
|
.items(
|
2020-05-15 15:14:18 +02:00
|
|
|
Joi.object({
|
|
|
|
name: Joi.string()
|
2020-05-04 15:48:07 +02:00
|
|
|
.max(128)
|
|
|
|
.required(),
|
|
|
|
|
2020-05-15 15:14:18 +02:00
|
|
|
range: Joi.object({
|
|
|
|
values: Joi.array()
|
2020-05-04 15:48:07 +02:00
|
|
|
.min(1),
|
|
|
|
|
2020-05-15 15:14:18 +02:00
|
|
|
min: Joi.number(),
|
2020-05-04 15:48:07 +02:00
|
|
|
|
2020-05-15 15:14:18 +02:00
|
|
|
max: Joi.number(),
|
2020-05-14 15:36:47 +02:00
|
|
|
|
2020-05-15 15:14:18 +02:00
|
|
|
type: Joi.string()
|
2020-05-14 15:36:47 +02:00
|
|
|
.valid('array')
|
2020-05-04 15:48:07 +02:00
|
|
|
})
|
|
|
|
.oxor('values', 'min')
|
|
|
|
.oxor('values', 'max')
|
2020-05-14 15:36:47 +02:00
|
|
|
.oxor('type', 'values')
|
|
|
|
.oxor('type', 'min')
|
|
|
|
.oxor('type', 'max')
|
2020-05-04 15:48:07 +02:00
|
|
|
.required()
|
|
|
|
})
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2020-05-14 12:31:57 +02:00
|
|
|
static input (data, param, template) { // validate data, param: new(everything required)/change(available attributes are validated)
|
2020-05-04 15:48:07 +02:00
|
|
|
if (param === 'new') {
|
2020-05-14 12:31:57 +02:00
|
|
|
if (template === 'treatment') {
|
2020-05-15 15:14:18 +02:00
|
|
|
return Joi.object({
|
2020-05-14 12:31:57 +02:00
|
|
|
name: this.template.name.required(),
|
|
|
|
number_prefix: this.template.number_prefix.required(),
|
|
|
|
parameters: this.template.parameters.required()
|
|
|
|
}).validate(data);
|
|
|
|
}
|
|
|
|
else {
|
2020-05-15 15:14:18 +02:00
|
|
|
return Joi.object({
|
2020-05-14 12:31:57 +02:00
|
|
|
name: this.template.name.required(),
|
|
|
|
parameters: this.template.parameters.required()
|
|
|
|
}).validate(data);
|
|
|
|
}
|
2020-05-04 15:48:07 +02:00
|
|
|
}
|
|
|
|
else if (param === 'change') {
|
2020-05-14 12:31:57 +02:00
|
|
|
if (template === 'treatment') {
|
2020-05-15 15:14:18 +02:00
|
|
|
return Joi.object({
|
2020-05-14 12:31:57 +02:00
|
|
|
name: this.template.name,
|
|
|
|
number_prefix: this.template.number_prefix,
|
|
|
|
parameters: this.template.parameters
|
|
|
|
}).validate(data);
|
|
|
|
}
|
|
|
|
else {
|
2020-05-15 15:14:18 +02:00
|
|
|
return Joi.object({
|
2020-05-14 12:31:57 +02:00
|
|
|
name: this.template.name,
|
|
|
|
parameters: this.template.parameters
|
|
|
|
}).validate(data);
|
|
|
|
}
|
2020-05-04 15:48:07 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return{error: 'No parameter specified!', value: {}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 12:31:57 +02:00
|
|
|
static output (data, template) { // validate output from database for needed properties, strip everything else
|
2020-05-06 14:39:04 +02:00
|
|
|
data = IdValidate.stringify(data);
|
2020-05-14 12:31:57 +02:00
|
|
|
let joiObject;
|
|
|
|
if (template === 'treatment') {
|
|
|
|
joiObject = {
|
|
|
|
_id: IdValidate.get(),
|
|
|
|
name: this.template.name,
|
|
|
|
version: this.template.version,
|
|
|
|
number_prefix: this.template.number_prefix,
|
|
|
|
parameters: this.template.parameters
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
joiObject = {
|
|
|
|
_id: IdValidate.get(),
|
|
|
|
name: this.template.name,
|
|
|
|
version: this.template.version,
|
|
|
|
parameters: this.template.parameters
|
|
|
|
};
|
|
|
|
}
|
2020-05-15 15:14:18 +02:00
|
|
|
const {value, error} = Joi.object(joiObject).validate(data, {stripUnknown: true});
|
2020-05-04 15:48:07 +02:00
|
|
|
return error !== undefined? null : value;
|
|
|
|
}
|
|
|
|
}
|