Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vstirbu/mongoose-jsonschema
Mongoose schema to JSON schema and back
https://github.com/vstirbu/mongoose-jsonschema
conversion json-schema mongoose-model mongoose-schema schema
Last synced: 3 months ago
JSON representation
Mongoose schema to JSON schema and back
- Host: GitHub
- URL: https://github.com/vstirbu/mongoose-jsonschema
- Owner: vstirbu
- License: mit
- Created: 2015-06-06T16:32:35.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-12-08T13:07:19.000Z (about 4 years ago)
- Last Synced: 2024-09-28T20:43:00.529Z (3 months ago)
- Topics: conversion, json-schema, mongoose-model, mongoose-schema, schema
- Language: JavaScript
- Size: 18.6 KB
- Stars: 11
- Watchers: 1
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Mongoose schema to JSON schema and back
[![npm version](https://badge.fury.io/js/mongoose-jsonschema.svg)](https://badge.fury.io/js/mongoose-jsonschema)
[![Build Status](https://travis-ci.org/vstirbu/mongoose-jsonschema.svg?branch=master)](https://travis-ci.org/vstirbu/mongoose-jsonschema)
[![Coverage Status](https://coveralls.io/repos/github/vstirbu/mongoose-jsonschema/badge.svg?branch=master)](https://coveralls.io/github/vstirbu/mongoose-jsonschema?branch=master)## Motivation
This 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.
## Installation
```
npm install mongoose-jsonschema
```## Usage
### Adding hypermedia controls in the Mongoose model
A Mongoose model should be augmented so that the schema options contains a JSON tranformation function:
```javascript
var mongoose = require('mongoose');var schema = new mongoose.Schema({
...
}, {
toJSON: {
transform: function (doc, ret, options) {
ret._links = {
describedBy: {
href: '/meta/schemas/example'
}
};
}
}
});var model = mongoose.model('Example', schema);
```Now, 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.
### Exposing the schemas
```javascript
var express = require('express'),
mongoose = require('mongoose'),
jsonSchema = require('mongoose-jsonschema').modelToJSONSchema;var app = express();
app.get('/meta/schemas/:schema', function (req, res) {
res.set({
'Content-Type': 'application/schema+json'
}).send(jsonSchema(mongoose.model(req.params.schema))).end();
});
```## API
### modelToJSONSchema(model, options) ⇒
Object
⏏
**Kind**: Exported function
**Returns**:Object
- JSONSchema| Param | Type | Description |
| --- | --- | --- |
| model |object
| Mongoose model to be converted |
| options |object
| Options for customising model conversion |
| options.reserved |Array.<string>
|object
| Model properties found in the array are not included in the schema or map of properties to be converted |
| options.reserved.property |boolean
| Include/do not include model `property` into schema |