🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

mongoose-payload-validator

Package Overview
Dependencies
Maintainers
0
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-payload-validator

A lightweight middleware for schema validation in Mongoose and Express.

0.4.0
latest
Source
npm
Version published
Weekly downloads
0
Maintainers
0
Weekly downloads
 
Created
Source

Mongoose Payload Validator (Express.js)

A middleware for validating request payloads against a Mongoose schema in Express.js applications using JavaScript. This package ensures the incoming request body adheres to the Mongoose schema structure, type definitions, and required constraints.

Features

  • Express.js Only: This middleware is designed specifically for Express.js applications.
  • Validates payloads against a Mongoose schema.
  • Supports nested objects, arrays, and enums.
  • Handles required fields and type validation.
  • Easy-to-use as Express middleware.

Installation

Install via npm:

npm install mongoose-payload-validator

Usage

  • Import the validatePayload middleware.
  • Pass the Mongoose schema and options.
  • Use it in your Express routes.

Example

const express = require("express");
const mongoose = require("mongoose");
const validatePayload = require("mongoose-payload-validator");

const app = express();
app.use(express.json());

// Define a Mongoose schema
const userSchema = new mongoose.Schema({
  name: { type: String, required: true },
  email: { type: String, required: true },
  age: { type: Number },
  role: { type: String, enum: ["admin", "user", "guest"] },
});

// Middleware to validate request body
app.post("/user", validatePayload(userSchema), (req, res) => {
  res.json({ message: "User data is valid!" });
});

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

Nested Objects and Arrays

const postSchema = new mongoose.Schema({
  title: { type: String, required: true },
  author: {
    name: { type: String, required: true },
    email: { type: String, required: true },
  },
  tags: [{ type: String }],
});

// Apply middleware
app.post("/post", validatePayload(postSchema), (req, res) => {
  res.json({ message: "Post data is valid!" });
});

Error Responses

When validation fails, the middleware responds with a detailed error message:

  • 400 BadRequestError: If the request body is empty or not provided.
  • 422 UnprocessableEntityError: If the payload fails validation.

Example error response:

{
  "error": {
    "statusCode": 422,
    "name": "UnprocessableEntityError",
    "message": "The request body is invalid. See error object `details` property for more info.",
    "details": [
      {
        "path": "email",
        "message": "must have required property 'email'"
      },
      {
        "path": "age",
        "message": "must be of type 'number', received 'string'"
      }
    ]
  }
}

License

This project is licensed under the MIT License. See the LICENSE file for details.

This version clearly states that the middleware is specifically for use in Express.js with JavaScript

Keywords

mongoose

FAQs

Package last updated on 22 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts