{"id":18557034,"url":"https://github.com/anechas/mongoose-validation-error-message-handler","last_synced_at":"2025-04-10T01:31:58.490Z","repository":{"id":57302338,"uuid":"330906735","full_name":"AnechaS/mongoose-validation-error-message-handler","owner":"AnechaS","description":"Mongoose Validation Error Handler","archived":false,"fork":false,"pushed_at":"2023-04-22T21:59:01.000Z","size":465,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-08T01:47:36.544Z","etag":null,"topics":["error-handler","error-messages","mongodb","mongoose","mongoose-validation-error","nodejs","validation","validation-error-message"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AnechaS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-19T08:05:37.000Z","updated_at":"2024-10-04T09:25:40.000Z","dependencies_parsed_at":"2022-09-02T18:22:49.041Z","dependency_job_id":null,"html_url":"https://github.com/AnechaS/mongoose-validation-error-message-handler","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnechaS%2Fmongoose-validation-error-message-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnechaS%2Fmongoose-validation-error-message-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnechaS%2Fmongoose-validation-error-message-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnechaS%2Fmongoose-validation-error-message-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AnechaS","download_url":"https://codeload.github.com/AnechaS/mongoose-validation-error-message-handler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248140618,"owners_count":21054322,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["error-handler","error-messages","mongodb","mongoose","mongoose-validation-error","nodejs","validation","validation-error-message"],"created_at":"2024-11-06T21:34:17.168Z","updated_at":"2025-04-10T01:31:53.471Z","avatar_url":"https://github.com/AnechaS.png","language":"JavaScript","readme":"# Mongoose Validation Error Message Handler\nis responsilbe transfroming mongoose validation error into generic form.\n\n## Install\n\n```bash\nnpm i mongoose-validation-error-message-handler\n```\n\n## Usages\n\n### Example 1 simple\n\n```javascript\nconst mongoose = require('mongoose');\nconst mongooseErrorHandler = require('mongoose-validation-error-message-handler');\n\nconst schema = new mongoose.Schema({\n  name: {\n    type: String,\n    required: true,\n  },\n});\n\nconst model = mongoose.model('person', schema);\n\nconst object = new model({});\nobject.save(function (err, doc) {\n  if (err) {\n    const error = mongooseErrorHandler(err);\n    console.log(error);\n    /**\n     * Error [MongooseValidatorError]: \"name\" is required\n     * message: \"name\" is required\n     * name: 'MongooseValidatorError',\n     * path: 'name',\n     * kind: 'required',\n     * value: undefined\n     */\n  }\n});\n```\n\n### Example 2 custom messages\n\n```javascript\nconst mongoose = require('mongoose');\nconst mongooseErrorHandler = require('mongoose-validation-error-message-handler');\n\nconst schema = new mongoose.Schema({\n  name: {\n    type: String,\n  },\n});\n\nconst model = mongoose.model('person', schema);\n\nconst object = new model({ name: {} });\nobject.save(function (err, doc) {\n  if (err) {\n    const error = mongooseErrorHandler(err, {\n      messages: {\n        string: '{path} must be a string'\n      }\n    });\n    console.log(error);\n    /**\n     * Error [MongooseValidatorError]: name must be a string\n     * message: name must be a string\n     * name: 'MongooseValidatorError',\n     * path: 'name',\n     * kind: 'string',\n     * value: {}\n     */\n  }\n});\n```\n\n### Example 3 paths origin message\n\n```javascript\nconst mongoose = require('mongoose');\nconst mongooseErrorHandler = require('mongoose-validation-error-message-handler');\n\nconst schema = new mongoose.Schema({\n  name: {\n    type: String,\n    required: true,\n  },\n});\n\nconst model = mongoose.model('person', schema);\n\nconst object = new model({});\nobject.save(function (err, doc) {\n  if (err) {\n    const error = mongooseErrorHandler(err, {\n      paths: {\n        name: { origin: true },\n        nameX: { origin: true, kind: 'maxlength' },\n      }\n    });\n    console.log(error);\n    /**\n     * person validation failed: name: Path `name` is required.\n     */\n  }\n});\n```\n\n### Example 4 paths custom messages\n\n```javascript\nconst mongoose = require('mongoose');\nconst mongooseErrorHandler = require('mongoose-validation-error-message-handler');\n\nconst schema = new mongoose.Schema({\n  name: {\n    type: String,\n    required: true,\n  },\n});\n\nconst model = mongoose.model('person', schema);\n\nconst object = new model({});\nobject.save(function (err, doc) {\n  if (err) {\n    const error = mongooseErrorHandler(err, {\n      paths: {\n        name: { message: 'name is required' },\n        nameX: { message: 'name length must be less than or equal to {maxlength} characters long', kind: 'maxlength' },\n      }\n    });\n    console.log(error);\n    /**\n     * Error [MongooseValidatorError]: name is required\n     * name: 'MongooseValidatorError',\n     * path: 'name',\n     * kind: 'required',\n     * value: undefined\n     */\n  }\n});\n```\n\n### Example 5 unique\nYou will need to install package `mongoose-unique-validator` first.\n\n```javascript\nconst mongoose = require('mongoose');\nconst uniqueValidator = require('mongoose-unique-validator');\nconst mongooseErrorHandler = require('mongoose-validation-error-message-handler');\n\nmongoose.Promise = Promise;\nmongoose.connect('mongodb://localhost/example', {\n  useNewUrlParser: true,\n  useUnifiedTopology: true,\n});\n\nmongoose.plugin(uniqueValidator);\n\nconst schema = new mongoose.Schema({\n  name: {\n    type: String,\n    unique: true,\n  },\n});\n\nconst model = mongoose.model('person', schema);\n\nconst object = new model({ name: 'a' });\nobject.save(function (err, doc) {\n  if (err) {\n    const error = mongooseErrorHandler(err);\n    console.log(error);\n    /**\n     * Error [MongooseValidatorError]: \"name\" already exists\n     * name: 'MongooseValidatorError',\n     * path: 'name',\n     * kind: 'unique',\n     * value: \"a\"\n     */\n  }\n});\n```\n\n### Example 6 server express\n\n```javascript\nconst express = require('express');\nconst mongoose = require('mongoose');\nconst mongooseErrorHandler = require('mongoose-validation-error-message-handler');\n\nmongoose.Promise = Promise;\nmongoose.connect('mongodb://localhost/example', {\n  useNewUrlParser: true,\n  useUnifiedTopology: true,\n});\n\nconst schema = new mongoose.Schema({\n  name: {\n    type: String,\n    required: true,\n  },\n});\n\nconst model = mongoose.model('person', schema);\n\nconst app = express();\napp.use(express.json());\n\napp.post('/', async (req, res, next) =\u003e {\n  try {\n    const object = await model.create(req.body);\n    res.json(object);\n  } catch (error) {\n    next(error)\n  }\n});\n\n// catch 404 and forward to error handler\napp.use((req, res, next) =\u003e {\n  const err = new Error('Not Found');\n  err.status = 404;\n  next(err);\n});\n\n// error handler\napp.use((err, req, res) =\u003e {\n  let error = mongooseErrorHandler(err);\n  if (error.name === 'MongooseValidatorError') {\n    error.status = 400;\n  }\n  res.status(error.status || 500);\n  res.json({ message: error.message });\n});\n\napp.listen(3000)\n```\n\n## Options\n\n### messages\n\n| Kind | Properties | Message | SchemaTypeOptions\n|---------------|---------------|------------------------|---------------|\n| base | `path`, `value` | {path} is invalid |\n| boolean | `path`, `value` | {path} must be a boolean | type |\n| buffer | `path`, `value` | {path} must be a buffer | type |\n| date | `path`, `value` | {path} must be a date | type |\n| date.max | `path`, `value`, `max` | {path} must be less than or equal to {max} | max |\n| date.min | `path`, `value`, `min` | {path} must be greater than or equal to {min} | min |\n| enum | `path`, `value`, `enumValues` | {path} is invalid | enum |\n| maxlength | `path`, `value`, `maxlength` | {path} length must be less than or equal to {maxlength} characters long | maxlength |\n| minlength | `path`, `value`, `minlength` | {path} length must be at least {minlength} characters long | minlength |\n| map | `path`, `value` | {path} must be a Map | type |\n| number | `path`, `value` | {path} must be a number | type |\n| number.max | `path`, `value`, `max` | {path} must be greater than or equal to {max} | max |\n| number.min | `path`, `value`, `min` | {path} must be less than or equal to {min} | min |\n| objectId | `path`, `value` | {path} must be a objectId | type |\n| regexp | `path`, `value`, `regexp` | {path} format is invalid | match |\n| required | `path`, `value` | {path} is required | required |\n| string | `path`, `value` | {path} must be a string | type |\n| unique | `path`, `value` | \"{path}\" already exists | unique |\n| validate | `path`, `value`, `enumValues` | {path} is invalid | validate |\n\n### paths\n\n`path`: String\n- `original`: Boolean, \n- `kind`: String, \n- `message`: String\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanechas%2Fmongoose-validation-error-message-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanechas%2Fmongoose-validation-error-message-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanechas%2Fmongoose-validation-error-message-handler/lists"}