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

116 lines
3.4 KiB
TypeScript
Raw Normal View History

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 = {
name: Joi.string()
2020-04-29 12:10:27 +02:00
.max(128),
supplier: Joi.string()
2020-04-29 12:10:27 +02:00
.max(128),
group: Joi.string()
2020-04-29 12:10:27 +02:00
.max(128),
mineral: Joi.number()
2020-04-29 12:10:27 +02:00
.integer()
.min(0)
.max(100),
glass_fiber: Joi.number()
2020-04-29 12:10:27 +02:00
.integer()
.min(0)
.max(100),
carbon_fiber: Joi.number()
2020-04-29 12:10:27 +02:00
.integer()
.min(0)
.max(100),
numbers: Joi.array()
.items(Joi.object({
color: Joi.string()
2020-05-07 21:55:29 +02:00
.max(128)
.required(),
number: Joi.string()
.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') {
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') {
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);
data.group = data.group_id.name;
data.supplier = data.supplier_id.name;
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;
}
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;
}
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
}