Validating Request Body in 3rd Party Service

I can’t seem to find any examples of validating a request body in a Mongo Db Realm 3rd party service function (api). In my node.js api, I use the express-validator middleware package to do this, but I’m not quite sure how to the same in Realm. Has anyone had success with using express-validator in a Realm function or how are you validating your requests?

Hi @Ken_Willa ,

I usually build a custom function to traverse the body and return a valid, not valid.

Additionally, you can also possibly use external dependency and upload the validator of your choice :

https://docs.mongodb.com/realm/functions/upload-external-dependencies/

Thanks
Pavel

@Pavel_Duchovny - thanks - appreciate the reply. I did already try uploading the express-validator package dependency, but it seems that express-validator only works (validates) as a middleware. When I try to use express within my web hook, it does not return any validation errors. From my documentation research, I believe it has to be injected into the custom routes (as I did in my node.js api) or it will not work.

But in case anyone has had success in getting it to work with Realm, I thought I’d post here.

Example code snippet:
exports = async function(payload, response) {
const request = EJSON.parse(payload.body.text())
const { body, validationResult } = require(‘express-validator’)
const rules = [
body(“testId”).notEmpty().withMessage(“testId is required.”)
];
const errors = validationResult(request);

return errors.array();
};

Otherwise, I’ve already been considering your idea of a custom validate function to validate the request body.