JSPM

  • Created
  • Published
  • Downloads 5808
  • Score
    100M100P100Q137800F
  • License MIT

mongoose.Schema extention with mehtod jsonShema

Package Exports

  • mongoose-schema-jsonschema

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mongoose-schema-jsonschema) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

mongoose-schema-jsonschema

The module allows to create json schema from Mongoose schema by adding jsonSchema method to mongoose.Schema and mongoose.Model classes

Installation

npm install mongoose-schema-jsonschema

Sample

Let's build json schema from simple mongoose schema

'use strict';

const mongoose = require('mongoose');
require('mongoose-schema-jsonschema')(mongoose);

const Schema = mongoose.Schema;

const BookSchema = new Schema({
  title: {type: String, required: true},
  year: Number,
  author: {type: String, required: true}
});

let jsonShema = BookSchema.jsonShema;

console.dir(jsonShema, {depth: null});

Output:

{
  type: 'object',
  properties: {
    title: { type: 'string' },
    year: { type: 'number' },
    author: { type: 'string' },
    _id: { type: 'string', format: 'uuid', pattern: '^[0-9a-fA-F]{24}$' }
  },
  required: [ 'title', 'author' ]
}

The mongoose.Model.jsonSchema method allows to build json schema considering the field selection and population

'use strict';

const mongoose = require('mongoose');
require('mongoose-schema-jsonschema')(mongoose);

const Schema = mongoose.Schema;

const BookSchema = new Schema({
  title: {type: String, required: true},
  year: Number,
  author: {type: Schema.Types.ObjectId, required: true, ref: 'Person'}
});

const PersonSchema = new Schema({
  firstName: {type: String, required: true},
  lastName: {type: String, required: true},
  dateOfBirth: Date
});

const Book = mongoose.model('Book', BookSchema);
const Person = mongoose.model('Person', PersonSchema)

console.dir(Book.jsonSchema('title year'), {depth: null});
console.dir(Book.jsonSchema('', 'author'), {depth: null});

Output:

{
  title: 'Book',
  type: 'object',
  properties: {
    title: { type: 'string' },
    year: { type: 'number' },
    _id: { type: 'string', format: 'uuid', pattern: '^[0-9a-fA-F]{24}$' }
  }
}
{
  title: 'Book',
  type: 'object',
  properties: {
    title: { type: 'string' },
    year: { type: 'number' },
    author: {
      title: 'Person',
      type: 'object',
      properties: {
        firstName: { type: 'string' },
        lastName: { type: 'string' },
        dateOfBirth: { type: 'string', format: 'date-time' },
        _id: { type: 'string', format: 'uuid', pattern: '^[0-9a-fA-F]{24}$' },
        __v: { type: 'number' }
      },
      required: [ 'firstName', 'lastName' ],
      'x-ref': 'Person',
      description: 'Refers to Person'
    },
    _id: { type: 'string', format: 'uuid', pattern: '^[0-9a-fA-F]{24}$' },
    __v: { type: 'number' }
  },
  required: [ 'title', 'author' ]
}

Specifications

mongoose.Schema.prototpye.jsonSchema

Builds the json schema based on the Mongooose schema. if schema has been already built the method returns new deep copy

Method considers the schema.options.toJSON.virtuals to included the virtual paths (without detailed description)

Declaration:

function schema_jsonSchema(name) { ... }

Parameters:

  • name: String - Name of the object
  • Returns Object - json schema

mongoose.Model.jsonSchema

Builds json schema for model considering the selection and population

if fields specified the method removes required contraints

Declaration:

function model_jsonSchema(fields, populate) { ... }

Parameters:

  • fields: String|Array|Object - mongoose selection object
  • populate: String|Object - mongoose population options
  • Returns Object - json schema