Coder Social home page Coder Social logo

jaredaat / json-schema-to-openapi-schema-object Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jaredce/json-schema-to-openapi-schema-object

0.0 0.0 0.0 171 KB

Converts a standard JSON Schema to a compatible Open API v3 Schema Object

License: Apache License 2.0

JavaScript 100.00%

json-schema-to-openapi-schema-object's Introduction

example workflow

json-schema-to-openAPI-schema-object

Converts a standard JSON Schema to a compatible Open API v3 Schema Object

A json schema with definitions can be converted to an OpenAPI components object containing the main schema and the definitions:

simple.schema.json

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "JSON API Schema",
    "description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
    "type": "object",
    "required": [
        "errors"
    ],
    "properties": {
        "errors": {
            "type": "object",
            "properties": {
                "message": {
                    "allOf": [
                        {
                            "$ref": "#/definitions/message"
                        }
                    ]
                }
            }
        }
    },
    "definitions": {
        "message": {
            "type": "string"
        }
    }
}

This will return an object:

{
  schemas: {
    message: {
      "type": "string"
    }
    main: {
      "title": "JSON API Schema",
      "description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
      "type": "object",
      "required": [
          "errors"
      ],
      "properties": {
          "errors": {
              "type": "object",
              "properties": {
                  "message": {
                      "allOf": [
                          {
                              "$ref": "#/components/schemas/message"
                          }
                      ]
                  }
              }
          }
      }
    }
  }
}

To run:

const jsonSchema = require('simple.json.schema')
const ConvertorFactory = require('index')

const convertedSchema = ConvertorFactory.convert(jsonSchema)

If you already have a referenced schema named "main", it will append a uuid to "main" like thus:

{
  schemas: {
    main: {
      "type": "string"
    }
    "main-547d55a5-87e9-444e-8783-e64f6863d20e": {
      "title": "JSON API Schema",
      "description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
      "type": "object",
      "required": [
          "errors"
      ],
      "properties": {
          "errors": {
              "type": "object",
              "properties": {
                  "message": {
                      "allOf": [
                          {
                              "$ref": "#/components/schemas/main"
                          }
                      ]
                  }
              }
          }
      }
    }
  }
}

json-schema-to-openapi-schema-object's People

Contributors

jaredce avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.