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

84 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-05-07 21:55:29 +02:00
import Joi from '@hapi/joi';
2020-05-06 14:39:04 +02:00
import IdValidate from './id';
export default class SampleValidate {
private static sample = {
2020-05-07 21:55:29 +02:00
number: Joi.string()
2020-05-06 14:39:04 +02:00
.max(128),
2020-05-07 21:55:29 +02:00
color: Joi.string()
2020-05-06 14:39:04 +02:00
.max(128),
2020-05-07 21:55:29 +02:00
type: Joi.string()
2020-05-06 14:39:04 +02:00
.max(128),
2020-05-07 21:55:29 +02:00
batch: Joi.string()
2020-05-06 14:39:04 +02:00
.max(128)
.allow(''),
2020-05-07 21:55:29 +02:00
notes: Joi.object({
comment: Joi.string()
2020-05-06 14:39:04 +02:00
.max(512),
2020-05-07 21:55:29 +02:00
sample_references: Joi.array()
.items(Joi.object({
2020-05-06 14:39:04 +02:00
id: IdValidate.get(),
2020-05-07 21:55:29 +02:00
relation: Joi.string()
2020-05-06 14:39:04 +02:00
.max(128)
})),
2020-05-07 21:55:29 +02:00
custom_fields: Joi.object()
.pattern(/.*/, Joi.alternatives()
2020-05-06 14:39:04 +02:00
.try(
2020-05-07 21:55:29 +02:00
Joi.string().max(128),
Joi.number(),
Joi.boolean(),
Joi.date()
2020-05-06 14:39:04 +02:00
)
)
})
};
static input (data, param) { // validate data, param: new(everything required)/change(available attributes are validated)
if (param === 'new') {
2020-05-07 21:55:29 +02:00
return Joi.object({
2020-05-06 14:39:04 +02:00
number: this.sample.number.required(),
color: this.sample.color.required(),
type: this.sample.type.required(),
batch: this.sample.batch.required(),
material_id: IdValidate.get().required(),
notes: this.sample.notes.required()
}).validate(data);
}
else if (param === 'change') {
2020-05-07 21:55:29 +02:00
return Joi.object({
number: this.sample.number,
color: this.sample.color,
type: this.sample.type,
batch: this.sample.batch,
material_id: IdValidate.get(),
notes: this.sample.notes,
}).validate(data);
2020-05-06 14:39:04 +02:00
}
else {
return{error: 'No parameter specified!', value: {}};
}
}
static output (data) {
data = IdValidate.stringify(data);
2020-05-07 21:55:29 +02:00
const {value, error} = Joi.object({
2020-05-06 14:39:04 +02:00
_id: IdValidate.get(),
number: this.sample.number,
color: this.sample.color,
type: this.sample.type,
batch: this.sample.batch,
material_id: IdValidate.get(),
note_id: IdValidate.get().allow(null),
user_id: IdValidate.get()
}).validate(data, {stripUnknown: true});
return error !== undefined? null : value;
}
}