@sivaguru/mongoose-error-formatter

1.0.11 • Public • Published

@sivaguru/mongoose-error-formatter

Installation

npm i @sivaguru/mongoose-error-formatter

Update Mongoose Validation Error Messages

By default, MongoDB validation errors don't come with a specific format for their error messages. This package offers the function formatMongooseError to format those errors as well.

const mongoose = require('mongoose');
const { formatMongooseError } = require("@sivaguru/mongoose-error-formatter");

  try {
    const { name, completed } = req.body;
    const task = await Task.create({
      name,
      completed: completed ? true : false
    });
    //Task created successfully
  } catch (error) {
    let errors = formatMongooseError(error, mongoose);
    // errors => false -> no validation error got from mongodb
    // otherwise you will get a object like below based on your validation error
    /*
    {
      name: "name is required."
    }
    */
  }

Mongoose errors usually appear as shown below by default.

{
  DocumentNotFoundError: null,
  general: {
    default: 'Validator failed for path `{PATH}` with value `{VALUE}`',
    required: 'Path `{PATH}` is required.'
  },
  Number: {
    min: 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).',
    max: 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).',
    enum: '`{VALUE}` is not a valid enum value for path `{PATH}`.'
  },
  Date: {
    min: 'Path `{PATH}` ({VALUE}) is before minimum allowed value ({MIN}).',
    max: 'Path `{PATH}` ({VALUE}) is after maximum allowed value ({MAX}).'
  },
  String: {
    enum: '`{VALUE}` is not a valid enum value for path `{PATH}`.',
    match: 'Path `{PATH}` is invalid ({VALUE}).',
    minlength: 'Path `{PATH}` (`{VALUE}`) is shorter than the minimum allowed length ({MINLENGTH}).',
    maxlength: 'Path `{PATH}` (`{VALUE}`) is longer than the maximum allowed length ({MAXLENGTH}).'
  }
}

Error messages in Mongoose are prefixed with the Path string, which can be inconvenient. You can easily modify these error messages by accessing mongoose.Error.messages or by using the updateMongooseErrorMessages function from this package. Using this function will update the messages accordingly.

{
  general: {
    default: '{PATH} was invalid.',
    required: '{PATH} is required.',
    unique: '{PATH} is already exists.'
  },
  Number: {
    min: '{PATH} is less than minimum allowed value of ({MIN}).',
    max: '{PATH} is more than maximum allowed value ({MAX}).'
  },
  Date: {
    min: '{PATH} is before minimum allowed value ({MIN}).',
    max: '{PATH} is after maximum allowed value ({MAX}).'
  },
  String: {
    enum: '{PATH} has an invalid selection.',
    match: '{PATH} has an invalid value.',
    minlength:
      '{PATH} is shorter than the minimum allowed length ({MINLENGTH}).',
    maxlength: '{PATH} is longer than the maximum allowed length ({MAXLENGTH}).'
  }
}

Update Mongoose Error Messages

Make sure to use this function at the beginning of your Node.js file (like app.js or index.js) before using mongoose. If you don't, it won't function properly.

const mongoose = require("mongoose");
const { updateMongooseErrorMessages } = require("@sivaguru/mongoose-error-formatter");
updateMongooseErrorMessages(mongoose);

You have the option to change the error messages to your own instead of using the messages from this package.

const { updateMongooseErrorMessages } = require("@sivaguru/mongoose-error-formatter");
updateMongooseErrorMessages(mongoose,{
  general: {
    default: '{PATH} was invalid.',
    required: '{PATH} is required hacked.',
    unique: '{PATH} is already exists.'
  }
});

License

MIT

Free Software, Hell Yeah!

Package Sidebar

Install

npm i @sivaguru/mongoose-error-formatter

Weekly Downloads

0

Version

1.0.11

License

MIT

Unpacked Size

6.26 kB

Total Files

3

Last publish

Collaborators

  • sivaguru