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

mongoose-zod-error-formatter

v1.0.0

Published

A powerful error formatter utility for Mongoose and Zod validation errors, designed for Express.js applications. Simplify and unify your API error handling with structured, user-friendly error messages.

Readme

mongoose-zod-error-formatter

npm version
License: MIT


A robust and lightweight utility package to format error objects from Mongoose, Zod, and general errors into consistent, user-friendly responses for Express.js applications written in TypeScript. Perfect for API error handling, validation, and debugging.


Features

  • Format Mongoose validation errors with detailed paths and messages
  • Format Mongoose cast errors (e.g., invalid ObjectId)
  • Format Zod validation errors with clear path and messages
  • Format simple/general errors into structured responses
  • TypeScript-ready with strict types for error messages and responses
  • Easily integrate into any Express middleware or error handler

Installation

npm install mongoose-zod-error-formatter
# or
yarn add mongoose-zod-error-formatter

Usage

1. Format Mongoose Validation Error

import mongoose from "mongoose";
import { formatValidationError } from "mongoose-zod-error-formatter";

try {
  // Some mongoose operation that fails validation
} catch (error) {
  if (error instanceof mongoose.Error.ValidationError) {
    const formatted = formatValidationError(error);
    console.log(formatted);
  }
}

2. Format Mongoose Cast Error (e.g., invalid ObjectId)

import mongoose from "mongoose";
import { formatCastError } from "mongoose-zod-error-formatter";

try {
  // Some mongoose operation that throws CastError
} catch (error) {
  if (error instanceof mongoose.Error.CastError) {
    const formatted = formatCastError(error);
    console.log(formatted);
  }
}

3. Format Zod Validation Error

import { z, ZodError } from "zod";
import { formatZodError } from "mongoose-zod-error-formatter";

const schema = z.object({
  username: z.string().min(3),
  age: z.number().int().positive(),
});

try {
  schema.parse({ username: "ab", age: -1 });
} catch (error) {
  if (error instanceof ZodError) {
    const formatted = formatZodError(error);
    console.log(formatted);
  }
}

4. Format Simple or Custom Error

import { formatSimpleError } from "mongoose-zod-error-formatter";
import { Request } from "express";

const fakeReq = {
  originalUrl: "/api/users",
} as Request;

const error = new Error("Something went wrong");
error.statusCode = 500;

const formatted = formatSimpleError(fakeReq, error);
console.log(formatted);

API Reference

| Function | Description | Input | Output | |------------------------|--------------------------------------------------|----------------------------|----------------------| | formatValidationError| Formats Mongoose validation errors | mongoose.Error.ValidationError | IErrorResponse | | formatCastError | Formats Mongoose cast errors (invalid ObjectId) | mongoose.Error.CastError | IErrorResponse | | formatZodError | Formats Zod validation errors | ZodError | IErrorResponse | | formatSimpleError | Formats simple/general errors with Express Request context | Request, Error & statusCode? | IErrorResponse |


Types

export interface IErrorMessages {
  path?: string | number;
  message: string;
}

export interface IErrorResponse {
  statusCode: number;
  message: string;
  errorMessages: IErrorMessages[];
}

Contributing

Contributions, issues, and feature requests are welcome! Feel free to open issues or submit pull requests.


License

MIT © Nurul Islam Rimon


Made with ❤️ by Nurul Islam Rimon