90 lines
2.7 KiB
TypeScript
90 lines
2.7 KiB
TypeScript
import Joi from '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),
|
|
|
|
properties: Joi.object(),
|
|
|
|
numbers: Joi.array()
|
|
.items(
|
|
Joi.string()
|
|
.max(64)
|
|
)
|
|
};
|
|
|
|
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(),
|
|
properties: this.material.properties.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,
|
|
properties: this.material.properties,
|
|
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);
|
|
data.group = data.group_id.name;
|
|
data.supplier = data.supplier_id.name;
|
|
const {value, error} = Joi.object({
|
|
_id: IdValidate.get(),
|
|
name: this.material.name,
|
|
supplier: this.material.supplier,
|
|
group: this.material.group,
|
|
properties: this.material.properties,
|
|
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,
|
|
properties: this.material.properties,
|
|
numbers: this.material.numbers
|
|
});
|
|
}
|
|
|
|
static query (data) {
|
|
return Joi.object({
|
|
status: Joi.string().valid('validated', 'new', 'all')
|
|
}).validate(data);
|
|
}
|
|
} |