104 lines
2.7 KiB
TypeScript
104 lines
2.7 KiB
TypeScript
import joi from '@hapi/joi';
|
|
import IdValidate from './id';
|
|
|
|
export default class TemplateValidate {
|
|
private static template = {
|
|
name: joi.string()
|
|
.max(128),
|
|
|
|
version: joi.number()
|
|
.min(1),
|
|
|
|
number_prefix: joi.string()
|
|
.min(1)
|
|
.max(16),
|
|
|
|
parameters: joi.array()
|
|
.min(1)
|
|
.items(
|
|
joi.object({
|
|
name: joi.string()
|
|
.max(128)
|
|
.required(),
|
|
|
|
range: joi.object({
|
|
values: joi.array()
|
|
.min(1),
|
|
|
|
min: joi.number(),
|
|
|
|
max: joi.number(),
|
|
|
|
type: joi.string()
|
|
.valid('array')
|
|
})
|
|
.oxor('values', 'min')
|
|
.oxor('values', 'max')
|
|
.oxor('type', 'values')
|
|
.oxor('type', 'min')
|
|
.oxor('type', 'max')
|
|
.required()
|
|
})
|
|
)
|
|
};
|
|
|
|
static input (data, param, template) { // validate data, param: new(everything required)/change(available attributes are validated)
|
|
if (param === 'new') {
|
|
if (template === 'treatment') {
|
|
return joi.object({
|
|
name: this.template.name.required(),
|
|
number_prefix: this.template.number_prefix.required(),
|
|
parameters: this.template.parameters.required()
|
|
}).validate(data);
|
|
}
|
|
else {
|
|
return joi.object({
|
|
name: this.template.name.required(),
|
|
parameters: this.template.parameters.required()
|
|
}).validate(data);
|
|
}
|
|
}
|
|
else if (param === 'change') {
|
|
if (template === 'treatment') {
|
|
return joi.object({
|
|
name: this.template.name,
|
|
number_prefix: this.template.number_prefix,
|
|
parameters: this.template.parameters
|
|
}).validate(data);
|
|
}
|
|
else {
|
|
return joi.object({
|
|
name: this.template.name,
|
|
parameters: this.template.parameters
|
|
}).validate(data);
|
|
}
|
|
}
|
|
else {
|
|
return{error: 'No parameter specified!', value: {}};
|
|
}
|
|
}
|
|
|
|
static output (data, template) { // validate output from database for needed properties, strip everything else
|
|
data = IdValidate.stringify(data);
|
|
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
|
|
};
|
|
}
|
|
const {value, error} = joi.object(joiObject).validate(data, {stripUnknown: true});
|
|
return error !== undefined? null : value;
|
|
}
|
|
} |