npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ecogy-energy/json-schema-to-openapi-schema

v0.0.1

Published

Converts a JSON Schema to OpenAPI Schema Object

Downloads

188

Readme

JSON Schema to OpenAPI Schema

This is a modified version of @openapi-contrib/json-schema-to-openapi-schema with some tweaks to integrate with the JSON Schemas that Ecogy uses

A little NodeJS package to convert JSON Schema to a OpenAPI Schema Object.

Treeware

Features

  • converts JSON Schema Draft 04 to OpenAPI 3.0 Schema Object
  • switches type: ['foo', 'null'] to type: foo and nullable: true
  • supports deep structures with nested allOfs etc.
  • switches patternProperties to x-patternProperties
  • converts dependencies to an allOf + oneOf OpenAPI-valid equivalent

Installation

npm install --save @openapi-contrib/json-schema-to-openapi-schema

Requires NodeJS v10 or greater.

Usage

Here's a small example to get the idea:

const convert = require("@openapi-contrib/json-schema-to-openapi-schema");

const schema = {
  $schema: "http://json-schema.org/draft-04/schema#",
  type: ["string", "null"],
  format: "date-time",
};

(async () => {
  const convertedSchema = await convert(schema);
  console.log(convertedSchema);
})();

The example prints out

{
  type: 'string',
  format: 'date-time',
  nullable: true
}

Options

The function accepts options object as the second argument.

cloneSchema (boolean)

If set to false, converts the provided schema in place. If true, clones the schema by converting it to JSON and back. The overhead of the cloning is usually negligible. Defaults to true.

dereference (boolean)

If set to true, all local and remote references (http/https and file) $refs will be dereferenced. Defaults to false.

Command Line

Usage:
    json-schema-to-openapi-schema <command> [options] <file>

Commands:
    convert                 Converts JSON Schema Draft 04 to OpenAPI 3.0 Schema Object

Options:
    -h, --help              Show help for any command
    -v, --version           Output the CLI version number
    -d, --dereference       If set all local and remote references (http/https and file) $refs will be dereferenced

Why?

OpenAPI is often described as an extension of JSON Schema, but both specs have changed over time and grown independently. OpenAPI v2 was based on JSON Schema draft v4 with a long list of deviations, but OpenAPI v3 shrank that list, upping their support to draft v4 and making the list of discrepancies shorter. This has been solved for OpenAPI v3.1, but for those using OpenAPI v3.0, you can use this tool to solve the divergence.

Diagram showing data model (the objects, payload bodies, etc) and service model (endpoints, headers, metadata, etc)

This tool sets out to allow folks to convert from JSON Schema (their one source of truth for everything) to OpenAPI (a thing for HTML docs and making SDKs).

Versions

† Draft v5 is also known as Draft Wright 00, as the drafts are often named after the author, and this was the first one by A. Wright. Amongst other things, draft v5 aimed to rewrite the meta files, but the experiment failed, meaning we need to continue to use the draft v4 metafiles. Ugh.

Converting Back

To convert the other way, check out openapi-schema-to-json-schema, which this package was based on.

Tests

To run the test-suite:

npm test

Treeware

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Thanks