0.4.0 • Published 5 months ago

mongoose-payload-validator v0.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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

  1. Import the validatePayload middleware.
  2. Pass the Mongoose schema and options.
  3. 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

0.3.0

6 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.2.3

7 months ago

0.4.0

5 months ago

0.2.2

8 months ago

0.1.0

9 months ago

0.0.6

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.3

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago