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';
|
2020-05-27 14:31:17 +02:00
|
|
|
import UserValidate from './user';
|
|
|
|
import MaterialValidate from './material';
|
2020-06-18 08:57:50 +02:00
|
|
|
import MeasurementValidate from './measurement';
|
2020-05-06 14:39:04 +02:00
|
|
|
|
|
|
|
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-27 14:31:17 +02:00
|
|
|
condition: Joi.object(),
|
|
|
|
|
2020-05-07 21:55:29 +02:00
|
|
|
notes: Joi.object({
|
|
|
|
comment: Joi.string()
|
2020-05-27 14:31:17 +02:00
|
|
|
.max(512)
|
|
|
|
.allow(''),
|
2020-05-06 14:39:04 +02:00
|
|
|
|
2020-05-07 21:55:29 +02:00
|
|
|
sample_references: Joi.array()
|
|
|
|
.items(Joi.object({
|
2020-05-27 14:31:17 +02:00
|
|
|
sample_id: IdValidate.get(),
|
2020-05-06 14:39:04 +02:00
|
|
|
|
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
|
|
|
)
|
|
|
|
)
|
2020-06-25 10:44:55 +02:00
|
|
|
}),
|
|
|
|
|
|
|
|
added: Joi.date()
|
|
|
|
.iso()
|
|
|
|
.min('1970-01-01T00:00:00.000Z')
|
2020-05-06 14:39:04 +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-06 14:39:04 +02:00
|
|
|
if (param === 'new') {
|
2020-05-07 21:55:29 +02:00
|
|
|
return Joi.object({
|
2020-05-06 14:39:04 +02:00
|
|
|
color: this.sample.color.required(),
|
|
|
|
type: this.sample.type.required(),
|
|
|
|
batch: this.sample.batch.required(),
|
2020-05-27 14:31:17 +02:00
|
|
|
condition: this.sample.condition.required(),
|
2020-05-06 14:39:04 +02:00
|
|
|
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({
|
|
|
|
color: this.sample.color,
|
|
|
|
type: this.sample.type,
|
|
|
|
batch: this.sample.batch,
|
2020-05-27 14:31:17 +02:00
|
|
|
condition: this.sample.condition,
|
2020-05-07 21:55:29 +02:00
|
|
|
material_id: IdValidate.get(),
|
|
|
|
notes: this.sample.notes,
|
|
|
|
}).validate(data);
|
2020-05-06 14:39:04 +02:00
|
|
|
}
|
2020-06-02 10:24:22 +02:00
|
|
|
else if (param === 'new-admin') {
|
|
|
|
return Joi.object({
|
2020-06-05 08:50:06 +02:00
|
|
|
number: this.sample.number,
|
2020-06-02 10:24:22 +02:00
|
|
|
color: this.sample.color.required(),
|
|
|
|
type: this.sample.type.required(),
|
|
|
|
batch: this.sample.batch.required(),
|
|
|
|
condition: this.sample.condition.required(),
|
|
|
|
material_id: IdValidate.get().required(),
|
|
|
|
notes: this.sample.notes.required()
|
|
|
|
}).validate(data);
|
|
|
|
}
|
2020-05-06 14:39:04 +02:00
|
|
|
else {
|
|
|
|
return{error: 'No parameter specified!', value: {}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
static output (data, param = 'refs') { // validate output and strip unwanted properties, returns null if not valid
|
2020-06-25 10:44:55 +02:00
|
|
|
data.added = data._id.getTimestamp();
|
2020-05-06 14:39:04 +02:00
|
|
|
data = IdValidate.stringify(data);
|
2020-05-27 14:31:17 +02:00
|
|
|
let joiObject;
|
|
|
|
if (param === 'refs') {
|
|
|
|
joiObject = {
|
|
|
|
_id: IdValidate.get(),
|
|
|
|
number: this.sample.number,
|
|
|
|
color: this.sample.color,
|
|
|
|
type: this.sample.type,
|
|
|
|
batch: this.sample.batch,
|
|
|
|
condition: this.sample.condition,
|
|
|
|
material_id: IdValidate.get(),
|
|
|
|
note_id: IdValidate.get().allow(null),
|
2020-06-25 10:44:55 +02:00
|
|
|
user_id: IdValidate.get(),
|
|
|
|
added: this.sample.added
|
2020-05-27 14:31:17 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else if(param === 'details') {
|
|
|
|
joiObject = {
|
|
|
|
_id: IdValidate.get(),
|
|
|
|
number: this.sample.number,
|
|
|
|
color: this.sample.color,
|
|
|
|
type: this.sample.type,
|
|
|
|
batch: this.sample.batch,
|
|
|
|
condition: this.sample.condition,
|
|
|
|
material: MaterialValidate.outputV(),
|
2020-06-18 08:57:50 +02:00
|
|
|
measurements: Joi.array().items(MeasurementValidate.outputV()),
|
2020-05-27 14:31:17 +02:00
|
|
|
notes: this.sample.notes,
|
|
|
|
user: UserValidate.username()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const {value, error} = Joi.object(joiObject).validate(data, {stripUnknown: true});
|
2020-05-06 14:39:04 +02:00
|
|
|
return error !== undefined? null : value;
|
|
|
|
}
|
2020-06-15 12:49:32 +02:00
|
|
|
|
|
|
|
static query (data) {
|
|
|
|
return Joi.object({
|
2020-06-25 10:44:55 +02:00
|
|
|
status: Joi.string().valid('validated', 'new', 'all'),
|
2020-06-25 11:59:36 +02:00
|
|
|
'from-id': IdValidate.get(),
|
2020-06-25 10:44:55 +02:00
|
|
|
'to-page': Joi.number().integer(),
|
2020-06-26 09:38:28 +02:00
|
|
|
'page-size': Joi.number().integer().min(1),
|
2020-06-29 12:27:39 +02:00
|
|
|
sort: Joi.string().pattern(/^(_id|color|number|type|batch|added|material\.name|material\.supplier|material\.group|material\.mineral|material\.glass_fiber|material\.carbon_fiber|material\.number)-(asc|desc)$/m).default('_id-asc'),
|
|
|
|
csv: Joi.boolean().default(false)
|
2020-06-25 10:44:55 +02:00
|
|
|
}).with('to-page', 'page-size').validate(data);
|
2020-06-15 12:49:32 +02:00
|
|
|
}
|
2020-05-06 14:39:04 +02:00
|
|
|
}
|