Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-schema-to-openapi-schema

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-schema-to-openapi-schema - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

CHANGELOG.md

79

index.js
const structs = ['allOf', 'anyOf', 'oneOf', 'not', 'items', 'additionalProperties'];
function InvalidTypeError(message) {
this.name = 'InvalidTypeError';
this.message = message;
}
InvalidTypeError.prototype = new Error();
function convert(schema, options) {

@@ -11,3 +18,3 @@ options = options || {};

delete schema['$schema'];
schema = removeRootKeywords(schema);
schema = convertSchema(schema);

@@ -18,2 +25,8 @@

function removeRootKeywords(schema) {
delete schema['$schema'];
delete schema['id'];
return schema;
}
function convertSchema(schema) {

@@ -52,3 +65,6 @@ let i = 0;

validateType(schema.type);
schema = convertTypes(schema);
schema = convertDependencies(schema);

@@ -62,7 +78,15 @@ if (typeof schema['patternProperties'] === 'object') {

function validateType(type) {
const validTypes = ['null', 'boolean', 'object', 'array', 'number', 'string', 'integer'];
const types = Array.isArray(type) ? type : [type];
types.forEach(type => {
if (validTypes.indexOf(type) < 0 && type !== undefined)
throw new InvalidTypeError('Type "' + type + '" is not a valid type');
});
}
function convertProperties(properties) {
var key
, property
, props = {}
;
let key = {};
let property = {};
let props = {};

@@ -77,2 +101,47 @@ for (key in properties) {

function convertDependencies(schema) {
const deps = schema.dependencies;
if (typeof deps !== 'object') {
return schema;
}
// Turns the dependencies keyword into an allOf of oneOf's
// "dependencies": {
// "post-office-box": ["street-address"]
// },
//
// becomes
//
// "allOf": [
// {
// "oneOf": [
// {"not": {"required": ["post-office-box"]}},
// {"required": ["post-office-box", "street-address"]}
// ]
// }
//
delete schema['dependencies'];
if (!Array.isArray(schema.allOf)) {
schema.allOf = [];
}
for (const key in deps) {
const foo = {
'oneOf': [
{
'not': {
'required': [key]
}
},
{
'required': [].concat(key, deps[key])
}
]
};
schema.allOf.push(foo);
}
return schema;
}
function convertTypes(schema) {

@@ -79,0 +148,0 @@ var newType;

2

package.json
{
"name": "json-schema-to-openapi-schema",
"version": "0.1.0",
"version": "0.1.1",
"description": "Converts a JSON Schema to OpenAPI Schema Object",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -1,2 +0,2 @@

# OpenAPI Schema to JSON Schema
# JSON Schema to OpenAPI Schema

@@ -10,3 +10,4 @@ A little NodeJS package to convert JSON Schema to [OpenAPI Schema Objects](https://swagger.io/specification/#schemaObject).

* supports deep structures with nested `allOf`s etc.
* switches `patternProperties` to `x-patternProperties` in the Schema Object
* switches `patternProperties` to `x-patternProperties`
* converts `dependencies` to an allOf + oneOf OpenAPI-valid equivalent

@@ -13,0 +14,0 @@ ## Installation

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc