35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import Joi from '@hapi/joi';
|
|
|
|
export default class ParametersValidate {
|
|
static input (data, parameters, param) { // data to validate, parameters from template, param: 'new', 'change'
|
|
let joiObject = {};
|
|
parameters.forEach(parameter => {
|
|
if (parameter.range.hasOwnProperty('values')) {
|
|
joiObject[parameter.name] = Joi.alternatives()
|
|
.try(Joi.string(), Joi.number(), Joi.boolean())
|
|
.valid(...parameter.range.values);
|
|
}
|
|
else if (parameter.range.hasOwnProperty('min') && parameter.range.hasOwnProperty('max')) {
|
|
joiObject[parameter.name] = Joi.number()
|
|
.min(parameter.range.min)
|
|
.max(parameter.range.max);
|
|
}
|
|
else if (parameter.range.hasOwnProperty('min')) {
|
|
joiObject[parameter.name] = Joi.number()
|
|
.min(parameter.range.min);
|
|
}
|
|
else if (parameter.range.hasOwnProperty('max')) {
|
|
joiObject[parameter.name] = Joi.number()
|
|
.max(parameter.range.max);
|
|
}
|
|
else {
|
|
joiObject[parameter.name] = Joi.alternatives()
|
|
.try(Joi.string(), Joi.number(), Joi.boolean());
|
|
}
|
|
if (param === 'new') {
|
|
joiObject[parameter.name] = joiObject[parameter.name].required()
|
|
}
|
|
});
|
|
return Joi.object(joiObject).validate(data);
|
|
}
|
|
} |