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 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-validation-error-message-handler

v1.2.9

Published

Mongoose Validation Error Message Handler

Downloads

119

Readme

Mongoose Validation Error Message Handler

is responsilbe transfroming mongoose validation error into generic form.

Install

npm i mongoose-validation-error-message-handler

Usages

Example 1 simple

const mongoose = require('mongoose');
const mongooseErrorHandler = require('mongoose-validation-error-message-handler');

const schema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
});

const model = mongoose.model('person', schema);

const object = new model({});
object.save(function (err, doc) {
  if (err) {
    const error = mongooseErrorHandler(err);
    console.log(error);
    /**
     * Error [MongooseValidatorError]: "name" is required
     * message: "name" is required
     * name: 'MongooseValidatorError',
     * path: 'name',
     * kind: 'required',
     * value: undefined
     */
  }
});

Example 2 custom messages

const mongoose = require('mongoose');
const mongooseErrorHandler = require('mongoose-validation-error-message-handler');

const schema = new mongoose.Schema({
  name: {
    type: String,
  },
});

const model = mongoose.model('person', schema);

const object = new model({ name: {} });
object.save(function (err, doc) {
  if (err) {
    const error = mongooseErrorHandler(err, {
      messages: {
        string: '{path} must be a string'
      }
    });
    console.log(error);
    /**
     * Error [MongooseValidatorError]: name must be a string
     * message: name must be a string
     * name: 'MongooseValidatorError',
     * path: 'name',
     * kind: 'string',
     * value: {}
     */
  }
});

Example 3 paths origin message

const mongoose = require('mongoose');
const mongooseErrorHandler = require('mongoose-validation-error-message-handler');

const schema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
});

const model = mongoose.model('person', schema);

const object = new model({});
object.save(function (err, doc) {
  if (err) {
    const error = mongooseErrorHandler(err, {
      paths: {
        name: { origin: true },
        nameX: { origin: true, kind: 'maxlength' },
      }
    });
    console.log(error);
    /**
     * person validation failed: name: Path `name` is required.
     */
  }
});

Example 4 paths custom messages

const mongoose = require('mongoose');
const mongooseErrorHandler = require('mongoose-validation-error-message-handler');

const schema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
});

const model = mongoose.model('person', schema);

const object = new model({});
object.save(function (err, doc) {
  if (err) {
    const error = mongooseErrorHandler(err, {
      paths: {
        name: { message: 'name is required' },
        nameX: { message: 'name length must be less than or equal to {maxlength} characters long', kind: 'maxlength' },
      }
    });
    console.log(error);
    /**
     * Error [MongooseValidatorError]: name is required
     * name: 'MongooseValidatorError',
     * path: 'name',
     * kind: 'required',
     * value: undefined
     */
  }
});

Example 5 unique

You will need to install package mongoose-unique-validator first.

const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const mongooseErrorHandler = require('mongoose-validation-error-message-handler');

mongoose.Promise = Promise;
mongoose.connect('mongodb://localhost/example', {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

mongoose.plugin(uniqueValidator);

const schema = new mongoose.Schema({
  name: {
    type: String,
    unique: true,
  },
});

const model = mongoose.model('person', schema);

const object = new model({ name: 'a' });
object.save(function (err, doc) {
  if (err) {
    const error = mongooseErrorHandler(err);
    console.log(error);
    /**
     * Error [MongooseValidatorError]: "name" already exists
     * name: 'MongooseValidatorError',
     * path: 'name',
     * kind: 'unique',
     * value: "a"
     */
  }
});

Example 6 server express

const express = require('express');
const mongoose = require('mongoose');
const mongooseErrorHandler = require('mongoose-validation-error-message-handler');

mongoose.Promise = Promise;
mongoose.connect('mongodb://localhost/example', {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

const schema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
});

const model = mongoose.model('person', schema);

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

app.post('/', async (req, res, next) => {
  try {
    const object = await model.create(req.body);
    res.json(object);
  } catch (error) {
    next(error)
  }
});

// catch 404 and forward to error handler
app.use((req, res, next) => {
  const err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use((err, req, res) => {
  let error = mongooseErrorHandler(err);
  if (error.name === 'MongooseValidatorError') {
    error.status = 400;
  }
  res.status(error.status || 500);
  res.json({ message: error.message });
});

app.listen(3000)

Options

messages

| Kind | Properties | Message | SchemaTypeOptions |---------------|---------------|------------------------|---------------| | base | path, value | {path} is invalid | | boolean | path, value | {path} must be a boolean | type | | buffer | path, value | {path} must be a buffer | type | | date | path, value | {path} must be a date | type | | date.max | path, value, max | {path} must be less than or equal to {max} | max | | date.min | path, value, min | {path} must be greater than or equal to {min} | min | | enum | path, value, enumValues | {path} is invalid | enum | | maxlength | path, value, maxlength | {path} length must be less than or equal to {maxlength} characters long | maxlength | | minlength | path, value, minlength | {path} length must be at least {minlength} characters long | minlength | | map | path, value | {path} must be a Map | type | | number | path, value | {path} must be a number | type | | number.max | path, value, max | {path} must be greater than or equal to {max} | max | | number.min | path, value, min | {path} must be less than or equal to {min} | min | | objectId | path, value | {path} must be a objectId | type | | regexp | path, value, regexp | {path} format is invalid | match | | required | path, value | {path} is required | required | | string | path, value | {path} must be a string | type | | unique | path, value | "{path}" already exists | unique | | validate | path, value, enumValues | {path} is invalid | validate |

paths

path: String

  • original: Boolean,
  • kind: String,
  • message: String