refs #5878 feat: new middleware to validate body
gitea/salix/pipeline/head There was a failure building this commit
Details
gitea/salix/pipeline/head There was a failure building this commit
Details
This commit is contained in:
parent
f09e249348
commit
ce3da6841c
|
@ -35,6 +35,7 @@
|
|||
}
|
||||
},
|
||||
"auth:after": {
|
||||
"./middleware/validate-model": {},
|
||||
"./middleware/current-user": {},
|
||||
"./middleware/salix-version": {}
|
||||
},
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue