Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

paulsmithkc/valid-oid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

valid-oid

Express middleware for validating an ObjectId in the path.

Install

npm install valid-oid

Usage

Import the middleware as below:

const validId = require('valid-oid');

Install the middleware on a route:

app.get('/api/product/:productId', validId('productId'), (req, res, next) => {
  const productId = req.productId;
  const product = getProduct(productId);
  if (!product) {
    return res.status(404).json({ error: `Product "${productId}" not found.` });
  } else {
    return res.json(product);
  }
});

Implementation

  1. The middleware looks for a path param with the provided name.

  2. If the param exists and is valid, the validated ObjectId is added to the request, and control passes to the next middleware.

  3. Otherwise, a 404 response is sent.

About

Express middleware for validating an ObjectId in the path.

Topics

Resources

License

Stars

Watchers

Forks