46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
|
import Joi from '@hapi/joi';
|
||
|
|
||
|
import IdValidate from './id';
|
||
|
|
||
|
export default class MeasurementValidate {
|
||
|
private static measurement = {
|
||
|
values: Joi.object()
|
||
|
.pattern(/.*/, Joi.alternatives()
|
||
|
.try(
|
||
|
Joi.string().max(128),
|
||
|
Joi.number(),
|
||
|
Joi.boolean(),
|
||
|
Joi.array()
|
||
|
)
|
||
|
)
|
||
|
};
|
||
|
|
||
|
static input (data, param) {
|
||
|
if (param === 'new') {
|
||
|
return Joi.object({
|
||
|
condition_id: IdValidate.get().required(),
|
||
|
values: this.measurement.values.required(),
|
||
|
measurement_template: IdValidate.get().required()
|
||
|
}).validate(data);
|
||
|
}
|
||
|
else if (param === 'change') {
|
||
|
return Joi.object({
|
||
|
values: this.measurement.values
|
||
|
}).validate(data);
|
||
|
}
|
||
|
else {
|
||
|
return{error: 'No parameter specified!', value: {}};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static output (data) {
|
||
|
data = IdValidate.stringify(data);
|
||
|
const {value, error} = Joi.object({
|
||
|
_id: IdValidate.get(),
|
||
|
condition_id: IdValidate.get(),
|
||
|
values: this.measurement.values,
|
||
|
measurement_template: IdValidate.get()
|
||
|
}).validate(data, {stripUnknown: true});
|
||
|
return error !== undefined? null : value;
|
||
|
}
|
||
|
}
|