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.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.10.2
    12
    • latest

Version History

Package Sidebar

Install

npm i json-schema-flatten

Weekly Downloads

12

Version

0.10.2

License

ISC

Last publish

Collaborators

  • moeriki