37 lines
1.2 KiB
TypeScript
37 lines
1.2 KiB
TypeScript
|
import Joi from '@hapi/joi';
|
||
|
|
||
|
export default class ParametersValidate {
|
||
|
static input (data, parameters) {
|
||
|
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)
|
||
|
.required();
|
||
|
}
|
||
|
else if (parameter.range.hasOwnProperty('min') && parameter.range.hasOwnProperty('max')) {
|
||
|
joiObject[parameter.name] = Joi.number()
|
||
|
.min(parameter.range.min)
|
||
|
.max(parameter.range.max)
|
||
|
.required();
|
||
|
}
|
||
|
else if (parameter.range.hasOwnProperty('min')) {
|
||
|
joiObject[parameter.name] = Joi.number()
|
||
|
.min(parameter.range.min)
|
||
|
.required();
|
||
|
}
|
||
|
else if (parameter.range.hasOwnProperty('max')) {
|
||
|
joiObject[parameter.name] = Joi.number()
|
||
|
.max(parameter.range.max)
|
||
|
.required();
|
||
|
}
|
||
|
else {
|
||
|
joiObject[parameter.name] = Joi.alternatives()
|
||
|
.try(Joi.string(), Joi.number(), Joi.boolean())
|
||
|
.required();
|
||
|
}
|
||
|
});
|
||
|
return Joi.object(joiObject).validate(data);
|
||
|
}
|
||
|
}
|