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/measurement.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-05-12 12:15:36 +02:00
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()
)
2020-05-27 17:03:03 +02:00
.allow(null)
2020-05-12 12:15:36 +02:00
)
};
2020-05-18 14:47:22 +02:00
static input (data, param) { // validate input, set param to 'new' to make all attributes required
2020-05-12 12:15:36 +02:00
if (param === 'new') {
return Joi.object({
2020-05-27 17:03:03 +02:00
sample_id: IdValidate.get().required(),
2020-05-12 12:15:36 +02:00
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: {}};
}
}
2020-05-18 14:47:22 +02:00
static output (data) { // validate output and strip unwanted properties, returns null if not valid
2020-05-12 12:15:36 +02:00
data = IdValidate.stringify(data);
const {value, error} = Joi.object({
_id: IdValidate.get(),
2020-05-27 17:03:03 +02:00
sample_id: IdValidate.get(),
2020-05-12 12:15:36 +02:00
values: this.measurement.values,
measurement_template: IdValidate.get()
}).validate(data, {stripUnknown: true});
return error !== undefined? null : value;
}
}