2020-05-27 14:31:17 +02:00
|
|
|
import Joi from '@hapi/joi';
|
2020-04-29 12:10:27 +02:00
|
|
|
|
|
|
|
import IdValidate from './id';
|
|
|
|
|
|
|
|
export default class MaterialValidate { // validate input for material
|
|
|
|
private static material = {
|
2020-05-27 14:31:17 +02:00
|
|
|
name: Joi.string()
|
2020-04-29 12:10:27 +02:00
|
|
|
.max(128),
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
supplier: Joi.string()
|
2020-04-29 12:10:27 +02:00
|
|
|
.max(128),
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
group: Joi.string()
|
2020-04-29 12:10:27 +02:00
|
|
|
.max(128),
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
mineral: Joi.number()
|
2020-04-29 12:10:27 +02:00
|
|
|
.integer()
|
|
|
|
.min(0)
|
|
|
|
.max(100),
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
glass_fiber: Joi.number()
|
2020-04-29 12:10:27 +02:00
|
|
|
.integer()
|
|
|
|
.min(0)
|
|
|
|
.max(100),
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
carbon_fiber: Joi.number()
|
2020-04-29 12:10:27 +02:00
|
|
|
.integer()
|
|
|
|
.min(0)
|
|
|
|
.max(100),
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
numbers: Joi.array()
|
|
|
|
.items(Joi.object({
|
|
|
|
color: Joi.string()
|
2020-05-07 21:55:29 +02:00
|
|
|
.max(128)
|
|
|
|
.required(),
|
2020-05-27 14:31:17 +02:00
|
|
|
number: Joi.string()
|
2020-05-15 14:55:01 +02:00
|
|
|
.max(128)
|
|
|
|
.allow('')
|
2020-05-07 21:55:29 +02:00
|
|
|
.required()
|
2020-04-29 12:10:27 +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-04-29 12:10:27 +02:00
|
|
|
if (param === 'new') {
|
2020-05-27 14:31:17 +02:00
|
|
|
return Joi.object({
|
2020-04-29 12:10:27 +02:00
|
|
|
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(),
|
2020-05-07 21:55:29 +02:00
|
|
|
numbers: this.material.numbers.required()
|
2020-04-29 12:10:27 +02:00
|
|
|
}).validate(data);
|
|
|
|
}
|
|
|
|
else if (param === 'change') {
|
2020-05-27 14:31:17 +02:00
|
|
|
return Joi.object({
|
2020-04-29 12:10:27 +02:00
|
|
|
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: {}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 14:47:22 +02:00
|
|
|
static output (data) { // validate output and strip unwanted properties, returns null if not valid
|
2020-05-06 14:39:04 +02:00
|
|
|
data = IdValidate.stringify(data);
|
2020-05-29 10:40:17 +02:00
|
|
|
data.group = data.group_id.name;
|
|
|
|
data.supplier = data.supplier_id.name;
|
2020-05-27 14:31:17 +02:00
|
|
|
const {value, error} = Joi.object({
|
2020-04-29 12:10:27 +02:00
|
|
|
_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;
|
|
|
|
}
|
2020-05-27 14:31:17 +02:00
|
|
|
|
2020-05-28 17:05:23 +02:00
|
|
|
static outputGroups (data) {// validate groups output and strip unwanted properties, returns null if not valid
|
|
|
|
const {value, error} = this.material.group.validate(data, {stripUnknown: true});
|
|
|
|
return error !== undefined? null : value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static outputSuppliers (data) {// validate suppliers output and strip unwanted properties, returns null if not valid
|
|
|
|
const {value, error} = this.material.supplier.validate(data, {stripUnknown: true});
|
|
|
|
return error !== undefined? null : value;
|
|
|
|
}
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
static outputV() { // return output validator
|
|
|
|
return 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
|
|
|
|
});
|
|
|
|
}
|
2020-06-17 13:42:14 +02:00
|
|
|
|
|
|
|
static query (data) {
|
|
|
|
return Joi.object({
|
|
|
|
status: Joi.string().valid('validated', 'new', 'all')
|
|
|
|
}).validate(data);
|
|
|
|
}
|
2020-04-29 12:10:27 +02:00
|
|
|
}
|