2020-04-29 12:10:27 +02:00
|
|
|
import joi from '@hapi/joi';
|
|
|
|
|
|
|
|
import IdValidate from './id';
|
|
|
|
|
|
|
|
export default class MaterialValidate { // validate input for material
|
|
|
|
private static material = {
|
|
|
|
name: joi.string()
|
|
|
|
.max(128),
|
|
|
|
|
|
|
|
supplier: joi.string()
|
|
|
|
.max(128),
|
|
|
|
|
|
|
|
group: joi.string()
|
|
|
|
.max(128),
|
|
|
|
|
|
|
|
mineral: joi.number()
|
|
|
|
.integer()
|
|
|
|
.min(0)
|
|
|
|
.max(100),
|
|
|
|
|
|
|
|
glass_fiber: joi.number()
|
|
|
|
.integer()
|
|
|
|
.min(0)
|
|
|
|
.max(100),
|
|
|
|
|
|
|
|
carbon_fiber: joi.number()
|
|
|
|
.integer()
|
|
|
|
.min(0)
|
|
|
|
.max(100),
|
|
|
|
|
|
|
|
numbers: joi.array()
|
|
|
|
.items(joi.object({
|
|
|
|
color: joi.string()
|
|
|
|
.max(128),
|
|
|
|
number: joi.number()
|
|
|
|
.min(0)
|
|
|
|
}))
|
|
|
|
};
|
|
|
|
|
|
|
|
static input (data, param) { // validate data, param: new(everything required)/change(available attributes are validated)
|
|
|
|
if (param === 'new') {
|
|
|
|
return joi.object({
|
|
|
|
name: this.material.name.required(),
|
|
|
|
supplier: this.material.supplier.required(),
|
|
|
|
group: this.material.group.required(),
|
|
|
|
mineral: this.material.mineral.required(),
|
|
|
|
glass_fiber: this.material.glass_fiber.required(),
|
|
|
|
carbon_fiber: this.material.carbon_fiber.required(),
|
|
|
|
numbers: this.material.numbers
|
|
|
|
}).validate(data);
|
|
|
|
}
|
|
|
|
else if (param === 'change') {
|
|
|
|
return joi.object({
|
|
|
|
name: this.material.name,
|
|
|
|
supplier: this.material.supplier,
|
|
|
|
group: this.material.group,
|
|
|
|
mineral: this.material.mineral,
|
|
|
|
glass_fiber: this.material.glass_fiber,
|
|
|
|
carbon_fiber: this.material.carbon_fiber,
|
|
|
|
numbers: this.material.numbers
|
|
|
|
}).validate(data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return{error: 'No parameter specified!', value: {}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static output (data) { // validate output from database for needed properties, strip everything else
|
2020-05-06 14:39:04 +02:00
|
|
|
data = IdValidate.stringify(data);
|
2020-04-29 12:10:27 +02:00
|
|
|
const {value, error} = joi.object({
|
|
|
|
_id: IdValidate.get(),
|
|
|
|
name: this.material.name,
|
|
|
|
supplier: this.material.supplier,
|
|
|
|
group: this.material.group,
|
|
|
|
mineral: this.material.mineral,
|
|
|
|
glass_fiber: this.material.glass_fiber,
|
|
|
|
carbon_fiber: this.material.carbon_fiber,
|
|
|
|
numbers: this.material.numbers
|
|
|
|
}).validate(data, {stripUnknown: true});
|
|
|
|
return error !== undefined? null : value;
|
|
|
|
}
|
|
|
|
}
|