{"id":15894456,"url":"https://github.com/rusty1s/mongoose-i18n-error","last_synced_at":"2025-08-01T07:31:23.060Z","repository":{"id":57302088,"uuid":"47214507","full_name":"rusty1s/mongoose-i18n-error","owner":"rusty1s","description":"lightweight module for node.js/express.js to create beautiful mongoose i18n validation error messages","archived":false,"fork":false,"pushed_at":"2019-03-29T19:22:58.000Z","size":17,"stargazers_count":2,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-02T12:36:32.698Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rusty1s.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":"2015-12-01T20:10:32.000Z","updated_at":"2022-02-10T17:48:38.000Z","dependencies_parsed_at":"2022-09-07T00:13:14.041Z","dependency_job_id":null,"html_url":"https://github.com/rusty1s/mongoose-i18n-error","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rusty1s%2Fmongoose-i18n-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rusty1s%2Fmongoose-i18n-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rusty1s%2Fmongoose-i18n-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rusty1s%2Fmongoose-i18n-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rusty1s","download_url":"https://codeload.github.com/rusty1s/mongoose-i18n-error/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228348360,"owners_count":17905897,"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":[],"created_at":"2024-10-06T08:40:20.678Z","updated_at":"2024-12-05T17:50:03.717Z","avatar_url":"https://github.com/rusty1s.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoose-i18n-error\n\nmongoose-i18n-error is a lightweight module for node.js/express.js to create beautiful mongoose i18n validation error messages.\nIt is built on top of the awesome [i18n-node](https://github.com/mashpie/i18n-node) translation module.\n\nI never liked the mongoose/mongo error messages caused by mongoose validation or duplicate keys. So I decided to write a simple express middleware module to create consistent error messages with i18n support.\n\n```js\n{\n\tusername: {\n\t\ttype: 'required',\n\t\tmessage: 'is required',\n\t\tvalue: undefined\n\t},\n\temail: {\n\t\ttype: 'regexp',\n\t\tmessage: 'is no valid email',\n\t\tvalue: 'johndoe.de'\n\t}\n}\n```\n\n## Usage\n\n```\nnpm install mongoose-i18n-error\n```\n\nConfigure your app to support i18n:\n\n```js\nvar i18n = require('i18n');\n\ni18n.configure({\n\tlocales: ['en', 'de'],\n\tdirectory: './locales'\n});\n\napp.use(i18n.init);\n```\n\nThen, simply add the module as an [express error handler middleware](http://expressjs.com/en/guide/error-handling.html):\n\n```js\nvar i18nMongooseError = new (require('mongoose-i18n-error'))();\n\napp.use(i18nMongooseError.handler(function(err, req, res, next) {\n\tres.status(422).json(err);\n}));\n```\n\n## Options\n\nBy default, the plugin will prefix the error messages in your locale files with `'error.'`:\n\n```js\n\"error.required\": \"is required\",\n\"error.minlength\": \"needs a minimum length of %s\",\n\"error.user.password.regexp\": \"is no valid email\",\n\"error.cast\": \"is not valid\"\n```\n\nYou can change the prefix by passing the prefix attribute as options:\n\n```js\nvar i18nMongooseError = new (require('mongoose-i18n-error'))({\n\tprefix: 'err.'\n});\n```\n\n## Custom validators\n\nIn your custom mongoose validators your error message should now apply to the key in your locales.\n\n```js\nvar Schema = new mongoose.Schema({\n\tname: {\n\t\ttype: String,\n\t\tvalidate: {\n\t\t\tvalidator: function(v) {\n\t\t\t\treturn /^[A-Z]*$/i.test(v);\n\t\t\t},\n\t\t\tmessage: 'user.name.alpha'\t// or 'alpha'\n\t\t}\n\t}\n});\n```\n\n# Tests\n\nTo run the tests you need a local MongoDB instance available. Run with:\n\n```\nnpm test\n```\n\n# Issues\n\nPlease use the GitHub issue tracker to raise any problems or feature requests.\n\nIf you would like to submit a pull request with any changes you make, please feel free!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frusty1s%2Fmongoose-i18n-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frusty1s%2Fmongoose-i18n-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frusty1s%2Fmongoose-i18n-error/lists"}