npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

api-req-validator

v1.0.3

Published

Validate api request against api schema

Readme

Request Validation Middleware for Node.js

This middleware simplifies API request validation in Node.js applications. Instead of writing separate validation logic for each API, you can define a schema in a single configuration file. The middleware dynamically validates all incoming requests against the schema, ensuring consistency and reducing boilerplate code.

Features

  • Dynamic Validation: Validate API requests using a central schema file.
  • Flexible Rules: Supports data types, regex, nested objects, allowed values, and more.
  • Easy Integration: Middleware integrates seamlessly with any Node.js application.
  • Centralized Schema: Define validation rules for all APIs in a single file.

Supported Data Types

The middleware supports validation for the following data types:

  • string
  • email
  • int
  • float
  • alphanumeric
  • alpha
  • bool
  • mobile
  • date
  • array

Validation Checks

The middleware can perform the following checks:

  • Mandatory Checks: Ensures required fields are provided.
  • Data Type Checks: Validates fields against specified data types such as string, int, float, etc.
  • Regex Validation: Applies custom regular expressions for additional checks.
  • Min and Max Value Checks: Ensures numeric values are within the specified range (for int and float).
  • Length Checks: Validates the maximum length of strings.
  • Allowed Values: Ensures fields contain only specified values.
  • Array Validation: Supports arrays of number, string, mobile, bool, and float.

For simple arrays of basic data types, you can use rules like:

  • dataType: "array+numbers"
  • dataType: "array+basicdataType"

For more complex arrays of objects, you can define rules using dot notation. For example:

"users.*.username": { required: true, dataType: "string" },
"users.*.age": { required: true, dataType: "int", min: 18 }

Future Enhancements

In upcoming versions, we plan to introduce support for custom validators. This feature will allow developers to define custom validation logic for specific APIs, providing even greater flexibility.

Installation

npm i api-req-validator

Usage

Step 1: Define the Schema

Create a file (e.g., validationSchema.js) that exports the validation schema. The schema must follow the format below:

What is a Schema?

A schema is a structured representation of the expected fields, their types, and rules for validation for each API endpoint. Each field in the schema can have properties such as:

  • required: Specifies if the field is mandatory.
  • dataType: Defines the type of data expected (e.g., string, int, date).
  • regex: Custom regex for additional validation.
  • length: Maximum length for string fields.
  • allowedValues: A list of permissible values for the field.
  • min and max: Range for numeric fields.

Example Schema File

const schema = {
  "/api/login": {
    username: { required: true, dataType: "string", length: 50 },
    password: { required: true, dataType: "string" },
  },
  "/api/register": {
    email: { required: true, dataType: "email" },
    age: { required: false, dataType: "int", min: 18 },
  },
};

module.exports = {
    schema
};

Writing an API Schema

Each API schema is defined as a key-value pair where the key is the API route, and the value is an object containing the validation rules for the fields in the request body. Example:

"/api/example": {
  "fieldName": {
    required: true,
    dataType: "string",
    length: 100
  },
  "nestedField": {
    key: {
      required: true,
      dataType: "int",
      min: 1
    }
  }
}

Step 2: Use the Middleware

Integrate the middleware into your Node.js application.

const express = require("express");
const {validateRequestHandler}  =  require('api-req-validator');

const app = express();

// Middleware to parse JSON requests
app.use(express.json());

//Get absolute file path
const filePath = path.resolve(__dirname,"./apiSpecification.js");
// Apply the validation middleware and apss the file path
app.use(validateRequestHandler(filePath));
// Define your routes
app.post("/api/login", (req, res) => {
  res.send("Login successful!");
});

app.post("/api/register", (req, res) => {
  res.send("Registration successful!");
});

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

Error Handling

The middleware handles errors as follows:

Application Crashes:

The application will crash in these scenarios:

  1. The schema file is not found.
  2. The schema object is not exported correctly (e.g., module.exports = { schema } is missing).
  3. The schema does not follow the required format.

API-Level Errors:

The middleware will respond with an error message in these cases:

  1. The API route is not defined in the schema file.
  2. The request body does not comply with the validation rules (e.g., missing required fields, incorrect data types).

Error responses will be in the following format:

{
  "error": true,
  "message": "<specific error message>"
}

Notes:

  1. Place the middleware before all routes to ensure requests are validated before hitting the route handlers.
  2. Define the schema accurately for all APIs in your application to avoid validation errors.

Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvement, feel free to create an issue or submit a pull request.

License

Apache-2.0