objectid-validator

1.0.2 • Public • Published

objectid-validator

FindById method in mongoose will give errors if object id is not in correct format. This module help to validate object id to avoid errors. It works as a middleware like bodymen.

Installation

npm install objectid-validator

Usage

import { idValidate, idValidateInParam } from "objectid-validator";
 
router.post("/create/", idValidate({ id: { required: true } }), create);
// This will return Network status code 400 with error messages if "id" in "req.body" is not valid
 
router.get(
  "/getData/:id",
  token({ required: true }),
  idValidateInParam("id"),
  getData
);
// This will return Network status code 400 with error messages if "id" in "req.params" is not valid
 
router.get(
  "/getData/:id",
  token({ required: true }),
  idValidateInParam("id", ["me"]),
  getData
);
// This will return Network status code 400 with error messages if "id" in "req.params" is not valid
// And it will not validate "id" if we passed string "me" instead of an ObjectId

Package Sidebar

Install

npm i objectid-validator

Weekly Downloads

4

Version

1.0.2

License

MIT

Unpacked Size

4.59 kB

Total Files

4

Last publish

Collaborators

  • arjupba