Last updated: Aug 04, 2022

Node.js Middleware to validate key/value pairs of an object

This snippet explains how to create your own middleware that validates the keys and values of an object when accepting requests.

const inspectBody = async (req, res, next) => {
  // ----> check if the body is an object
  //       and remove keys with null values
  const obj = req.body;
  if (typeof obj === 'object') {
    Object.keys(obj).forEach((k) => obj[k] == null && delete obj[k]);
  }

  // we define the employee object type that we need for this request
  const employee = {
    name: 'John Doe',
    salary: '2000000',
    currency: 'USD',
    department: 'Entrepreneur',
    on_contract: false,
    sub_department: 'Engineer',
  };

  // map over the object and validate

  for (x in Object.keys(employee)) {
    // ---> Interesting things happen here
    // -    We check to see if the key exists in our employee type variable
    // -    If it does not exist we return an error
    if (Object.keys(obj).indexOf(Object.keys(employee)[x]) < 0) {
      res
        .status(400)
        .json({ error: \`key "\${Object.keys(employee)[x]}" was not provided\` });
      return;
    } else {
      // ---> Here we check to see if the data type of keys
      //      in the variable employee matches with the recieved object
      Object.keys(obj).find((o, y) => {
        if (o === Object.keys(employee)[x])
          if (
            typeof Object.values(employee)[x] !== typeof Object.values(obj)[y]
          ) {
            res.status(400).json({
              error: \`Data type of "\${
                Object.keys(employee)[x]
              }" is not \${typeof Object.values(employee)[x]}\`,
            });
          }
      });
    }
  }
  return next();
};

module.exports = inspectBody

First, we check if the body of our request contains an object. Then, we remove the null values from it.

Second, we declare a variable called employees. Define the keys that you want to validate and assign it any value so that we can check for the data type of the key later on.

Third, we check to see if the key that we want in the object exists in our employee variable. This validates the key that we want to save.

Fourth, we check and validate the data types of these keys by looping over both the objects i.e. employee and request object

That is it, we have created our middleware that we can use in our node.js or express.js application