2.0.7 • Published 1 year ago

valid-oid v2.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

valid-oid

Express middleware for validating a MongoDB ObjectId in the request 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/id/:productId', validId('productId'), (req, res) => {
  const productId = req.productId;
  const product = getProductById(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.

2.0.3

1 year ago

2.0.2

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.7

1 year ago

2.0.6

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.0

2 years ago

0.0.1

3 years ago