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) .required(), number: Joi.string() .max(128) .allow('') .required() })) }; static input (data, param) { // validate input, set param to 'new' to make all attributes required 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.required() }).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 and strip unwanted properties, returns null if not valid data = IdValidate.stringify(data); 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; } 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 }); } }