{"id":14975668,"url":"https://github.com/vstirbu/mongoose-jsonschema","last_synced_at":"2025-10-27T14:31:04.666Z","repository":{"id":33342233,"uuid":"36987033","full_name":"vstirbu/mongoose-jsonschema","owner":"vstirbu","description":"Mongoose schema to JSON schema and back","archived":false,"fork":false,"pushed_at":"2020-12-08T13:07:19.000Z","size":19,"stargazers_count":11,"open_issues_count":1,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-01T06:11:49.341Z","etag":null,"topics":["conversion","json-schema","mongoose-model","mongoose-schema","schema"],"latest_commit_sha":null,"homepage":null,"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/vstirbu.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-06-06T16:32:35.000Z","updated_at":"2020-12-10T13:48:01.000Z","dependencies_parsed_at":"2022-09-20T16:40:44.809Z","dependency_job_id":null,"html_url":"https://github.com/vstirbu/mongoose-jsonschema","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vstirbu%2Fmongoose-jsonschema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vstirbu%2Fmongoose-jsonschema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vstirbu%2Fmongoose-jsonschema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vstirbu%2Fmongoose-jsonschema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vstirbu","download_url":"https://codeload.github.com/vstirbu/mongoose-jsonschema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238508740,"owners_count":19484197,"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":["conversion","json-schema","mongoose-model","mongoose-schema","schema"],"created_at":"2024-09-24T13:52:21.958Z","updated_at":"2025-10-27T14:31:04.347Z","avatar_url":"https://github.com/vstirbu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mongoose schema to JSON schema and back\n\n[![npm version](https://badge.fury.io/js/mongoose-jsonschema.svg)](https://badge.fury.io/js/mongoose-jsonschema)\n[![Build Status](https://travis-ci.org/vstirbu/mongoose-jsonschema.svg?branch=master)](https://travis-ci.org/vstirbu/mongoose-jsonschema)\n[![Coverage Status](https://coveralls.io/repos/github/vstirbu/mongoose-jsonschema/badge.svg?branch=master)](https://coveralls.io/github/vstirbu/mongoose-jsonschema?branch=master)\n\n## Motivation\n\nThis library represents a practical approach to convert the schemas used in a Mongoose model so that they can conveyed to hypermedia clients that interact with the web service.\n\n## Installation\n\n```\nnpm install mongoose-jsonschema\n```\n\n## Usage\n\n### Adding hypermedia controls in the Mongoose model\n\nA Mongoose model should be augmented so that the schema options contains a JSON tranformation function:\n\n```javascript\nvar mongoose = require('mongoose');\n\nvar schema = new mongoose.Schema({\n\t...\n}, {\n\ttoJSON: {\n\t\ttransform: function (doc, ret, options) {\n\t\t\tret._links = {\n\t\t\t\tdescribedBy: {\n\t\t\t\t\thref: '/meta/schemas/example'\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\t}\n});\n\nvar model = mongoose.model('Example', schema);\n```\n\nNow, every time the model is converted to JSON, the representation will convey to the client the link that describes the schema of the document. The representation uses the [HAL](http://stateless.co/hal_specification.html) convention but other san be used as well.\n\n### Exposing the schemas\n\n```javascript\nvar express =  require('express'),\n\t\tmongoose = require('mongoose'),\n\t\tjsonSchema = require('mongoose-jsonschema').modelToJSONSchema;\n\nvar app = express();\n\napp.get('/meta/schemas/:schema', function (req, res) {\n\tres.set({\n\t\t'Content-Type': 'application/schema+json'\n\t}).send(jsonSchema(mongoose.model(req.params.schema))).end();\n});\n```\n\n## API\n\n### modelToJSONSchema(model, options) ⇒ \u003ccode\u003eObject\u003c/code\u003e ⏏\n**Kind**: Exported function  \n**Returns**: \u003ccode\u003eObject\u003c/code\u003e - JSONSchema  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| model | \u003ccode\u003eobject\u003c/code\u003e | Mongoose model to be converted |\n| options | \u003ccode\u003eobject\u003c/code\u003e | Options for customising model conversion |\n| options.reserved | \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e \u0026#124; \u003ccode\u003eobject\u003c/code\u003e | Model properties found in the array are not included in the schema or map of properties to be converted |\n| options.reserved.property | \u003ccode\u003eboolean\u003c/code\u003e | Include/do not include model `property` into schema |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvstirbu%2Fmongoose-jsonschema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvstirbu%2Fmongoose-jsonschema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvstirbu%2Fmongoose-jsonschema/lists"}