npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript.Learn more »

json-schema-flatten

0.10.2 • Public • Published

JSON Schema Flatten

Flatten a JSON schema separating all nested objects into referenced definitions.

Usage

var flatten = require('json-schema-flatten/es5');
 
var schema = {
  type: 'object',
  properties: {
    name: {
      type: 'object', //= nested object
      properties: {
        first: { type: 'string' },
        last: { type: 'string' },
      }
    }
  }
};
 
console.log(flatten(schema));

Will output.

{
  "type": "object",
  "properties": {
    "name": {
      "$ref": "#/definitions/name"
    }
  },
  "definitions": {
    "name": {
      "type": "object",
      "properties": {
        "first": { "type": "string" },
        "last": { "type": "string" }
      }
    }
  }
}

Why

Swagger UI doesn't generate documentation well when you have nested object structures. Running it through this fixes it for me.

API

var flatten = require('json-schema-flatten');

flatten( schema :object ) :object

  • schema — a JSON schema. Won't be modified.

Keywords

none

install

npm i json-schema-flatten

Downloadsweekly downloads

77

version

0.10.2

license

ISC

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability