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-validation-error-handler

v1.1.0

Published

Error handler middleware for express-validator formatted responses

Downloads

9

Readme

api-validation-error-handler

A clean reusable Express middleware for handling validation errors produced by express-validator, providing unified API responses and localization support. This middleware reduces repeated error handling logic and keeps controllers clean.


📦 Installation

npm install api-validation-error-handler

or

yarn add api-validation-error-handler

✨ Features

✔ Handles validation errors automatically ✔ Converts errors into key/value structure ✔ Uses standard API response format via express-api-responses ✔ Keeps controllers clean and focused


🚀 Usage

1. Import middleware

import { handleValidationErrors } from "api-validation-error-handler";

or using CommonJS:

const { handleValidationErrors } = require("api-validation-error-handler");

2. Apply validator and middleware

import express from "express";
import { body } from "express-validator";
import { handleValidationErrors } from "api-validation-error-handler";

const router = express.Router();

router.post(
  "/books",
  [
    body("name")
      .notEmpty()
      .withMessage((value, { req }) => "Name is required"),
    body("price")
      .isFloat({ gt: 0 })
      .withMessage((value, { req }) => "Price must be greater than 0"),
  ],
  handleValidationErrors(), // 🚨 always after validators
  (req, res) => {
    res.json({ status: true, message: "Book created successfully" });
  }
);

export default router;

🎛 Custom Message Key

You can override the returned message:

handleValidationErrors("Input validation error")

Example:

router.post(
  "/login",
  [
    body("email")
      .isEmail()
      .withMessage((v, { req }) => "Invalid email format"),
  ],
  handleValidationErrors("Login validation failed"),
  loginController
);

📤 Response Format

If validation fails:

{
    "statusCode": 400,
    "status": false,
    "message": "failed to login",
    "data": null,
    "errors": {
        "email": "email address is invalid"
    }
}

If validation succeeds:

➡ Controller continues normally.


🧠 How It Works

It checks validation errors, formats them, and returns an API response:

  • Key = field name
  • Value = validation message

Example formatted output:

{
  "name": "Name is required",
  "price": "Price must be greater than 0"
}

📌 Notes & Recommendations

  • Put this middleware after validation rules

  • Works with:

    • body()
    • param()
    • query()
  • Keeps response structure consistent across the API


🧑‍💻 Requirements

This package depends on:

  • express-validator
  • express-api-responses

Ensure they are installed in your project.


🏁 Summary

Use handleValidationErrors() in any route to avoid manually checking validation results and formatting error responses. This middleware is ideal for large-scale Express REST APIs where consistent error handling is required.


📌 For suggestions, improvements, or feature requests—open an issue anytime.