77 lines
1.9 KiB
TypeScript
77 lines
1.9 KiB
TypeScript
|
import joi from '@hapi/joi';
|
||
|
|
||
|
import IdValidate from './id';
|
||
|
|
||
|
export default class SampleValidate {
|
||
|
private static sample = {
|
||
|
number: joi.string()
|
||
|
.max(128),
|
||
|
|
||
|
color: joi.string()
|
||
|
.max(128),
|
||
|
|
||
|
type: joi.string()
|
||
|
.max(128),
|
||
|
|
||
|
batch: joi.string()
|
||
|
.max(128)
|
||
|
.allow(''),
|
||
|
|
||
|
notes: joi.object({
|
||
|
comment: joi.string()
|
||
|
.max(512),
|
||
|
|
||
|
sample_references: joi.array()
|
||
|
.items(joi.object({
|
||
|
id: IdValidate.get(),
|
||
|
|
||
|
relation: joi.string()
|
||
|
.max(128)
|
||
|
})),
|
||
|
|
||
|
custom_fields: joi.object()
|
||
|
.pattern(/.*/, joi.alternatives()
|
||
|
.try(
|
||
|
joi.string().max(128),
|
||
|
joi.number(),
|
||
|
joi.boolean(),
|
||
|
joi.date()
|
||
|
)
|
||
|
)
|
||
|
})
|
||
|
};
|
||
|
|
||
|
static input (data, param) { // validate data, param: new(everything required)/change(available attributes are validated)
|
||
|
if (param === 'new') {
|
||
|
return joi.object({
|
||
|
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') {
|
||
|
return{error: 'Not implemented!', value: {}};
|
||
|
}
|
||
|
else {
|
||
|
return{error: 'No parameter specified!', value: {}};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static output (data) {
|
||
|
data = IdValidate.stringify(data);
|
||
|
const {value, error} = joi.object({
|
||
|
_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;
|
||
|
}
|
||
|
}
|