10

I am using express-validator in my express app, and I am attempting to prevent additional fields being specified on POST requests. This is because I am passing the value of req.body to my ORM to insert into the database, and I want to avoid having to explicitly map between my inserted object and request body alongside adding validators.

Is this possible? I can't seem to find it in the documentation. Using JSON Schema you can do this with additionalProperties: false

coderdude123
  • 311
  • 2
  • 7

1 Answers1

10

After some more research, I found that it's possible to do with with the Matched Data API

In my express controller I can now do;

const { matchedData } = require('express-validator');
(req, res) => {
  const matched = matchedData(req, {
    includeOptionals: true,
  });
  db.insert(matched)
  ...
}

coderdude123
  • 311
  • 2
  • 7