refs #5878 feat: new middleware to validate body
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Javier Segarra 2023-11-27 15:07:14 +01:00
parent f09e249348
commit ce3da6841c
2 changed files with 35 additions and 1 deletions

View File

@ -35,11 +35,12 @@
}
},
"auth:after": {
"./middleware/validate-model": {},
"./middleware/current-user": {},
"./middleware/salix-version": {}
},
"parse": {
"body-parser#json":{}
"body-parser#json":{}
},
"routes": {
"loopback#rest": {

View File

@ -0,0 +1,33 @@
const {models} = require('vn-loopback/server/server');
const validatorBodyModel = require('../../../modules/client/back/methods/client/body_model_validator');
const makeSingular = s => {
if (s == null || s.length == 0)
return s;
return s.substring(0, s.length - 1);
};
function blobToB64(data) {
return new Uint8Array(data).reduce(function(data, byte) {
return data + String.fromCharCode(byte);
}, '');
}
module.exports = function(options) {
return function(req, res, next) {
const {method, originalUrl} = req;
if (['GET', 'DELETE'].includes(method)) return next();
let [module, id, path] = originalUrl.split('api/')[1].split('/');
let model = models[module];
if (!model) {
module = makeSingular(module);
model = models[module];
}
const properties = model.definition.rawProperties;
const data = JSON.parse(blobToB64(req.readableBuffer.tail.data));
let isValid = validatorBodyModel(properties, data);
if (!isValid.isValid)
next();
return next();
};
};