import Joi from '@hapi/joi'; import IdValidate from './id'; import UserValidate from './user'; import MaterialValidate from './material'; import MeasurementValidate from './measurement'; export default class SampleValidate { private static sample = { number: Joi.string() .max(128), color: Joi.string() .max(128), type: Joi.string() .max(128), batch: Joi.string() .max(128) .allow(''), condition: Joi.object(), notes: Joi.object({ comment: Joi.string() .max(512) .allow(''), sample_references: Joi.array() .items(Joi.object({ sample_id: IdValidate.get(), relation: Joi.string() .max(128) })), custom_fields: Joi.object() .pattern(/.*/, Joi.alternatives() .try( Joi.string().max(128), Joi.number(), Joi.boolean(), Joi.date() ) ) }), added: Joi.date() .iso() .min('1970-01-01T00:00:00.000Z') }; static input (data, param) { // validate input, set param to 'new' to make all attributes required if (param === 'new') { return Joi.object({ 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); } else if (param === 'change') { return Joi.object({ color: this.sample.color, type: this.sample.type, batch: this.sample.batch, condition: this.sample.condition, material_id: IdValidate.get(), notes: this.sample.notes, }).validate(data); } else if (param === 'new-admin') { return Joi.object({ number: this.sample.number, 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); } else { return{error: 'No parameter specified!', value: {}}; } } static output (data, param = 'refs') { // validate output and strip unwanted properties, returns null if not valid data.added = data._id.getTimestamp(); data = IdValidate.stringify(data); 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), user_id: IdValidate.get(), added: this.sample.added }; } 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(), measurements: Joi.array().items(MeasurementValidate.outputV()), notes: this.sample.notes, user: UserValidate.username() } } else { return null; } const {value, error} = Joi.object(joiObject).validate(data, {stripUnknown: true}); return error !== undefined? null : value; } static query (data) { return Joi.object({ status: Joi.string().valid('validated', 'new', 'all'), 'from-id': IdValidate.get(), 'to-page': Joi.number().integer(), 'page-size': Joi.number().integer().min(1), 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) }).with('to-page', 'page-size').validate(data); } }