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
20
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.2.0 to 0.3.0

test/array-items.test.js

11

CHANGELOG.md

@@ -9,5 +9,12 @@ # Changelog

## [0.3.0] - 2018-12-18
### Added
- Create empty items, as it must always be present for type: array
- Rewrite exclusiveMinimum/exclusiveMaximum
- Rewrite if/then/else as oneOf + allOf
- Rewrite const as single element enum
## [0.2.0] - 2018-05-10
### Fixed
* Implemented [@cloudflare/json-schema-walker] to make sure all subschemas are
- Implemented [@cloudflare/json-schema-walker] to make sure all subschemas are
processed

@@ -19,2 +26,2 @@

### Added
* Convert `dependencies` to an allOf + oneOf OpenAPI-valid equivalent
- Convert `dependencies` to an allOf + oneOf OpenAPI-valid equivalent

@@ -32,3 +32,6 @@ const schemaWalker = require('@cloudflare/json-schema-walker');

schema = convertTypes(schema);
schema = rewriteConst(schema);
schema = convertDependencies(schema);
schema = rewriteIfThenElse(schema);
schema = rewriteExclusiveMinMax(schema);

@@ -39,2 +42,6 @@ if (typeof schema['patternProperties'] === 'object') {

if (schema.type === 'array' && typeof schema.items === 'undefined') {
schema.items = {};
}
return schema;

@@ -142,5 +149,49 @@ }

delete schema['patternProperties'];
if (typeof schema.additionalProperties === 'undefined') schema.additionalProperties = true;
return schema;
}
function rewriteConst(schema) {
if (schema.const) {
schema.enum = [ schema.const ];
delete schema.const;
}
return schema;
}
function rewriteIfThenElse(schema) {
/* @handrews https://github.com/OAI/OpenAPI-Specification/pull/1766#issuecomment-442652805
if and the *Of keywords
There is a really easy solution for implementations, which is that
if: X, then: Y, else: Z
is equivalent to
oneOf: [allOf: [X, Y], allOf: [not: X, Z]]
*/
if (schema.if && schema.then) {
schema.oneOf = [ { allOf: [ schema.if, schema.then ] },
{ allOf: [ { not: schema.if }, schema.else ] } ];
delete schema.if;
delete schema.then;
delete schema.else;
}
return schema;
}
function rewriteExclusiveMinMax(schema) {
if (typeof schema.exclusiveMaximum === 'number') {
schema.maximum = schema.exclusiveMaximum;
schema.exclusiveMaximum = true;
}
if (typeof schema.exclusiveMinimum === 'number') {
schema.minimum = schema.exclusiveMinimum;
schema.exclusiveMinimum = true;
}
return schema;
}
module.exports = convert;

4

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

@@ -11,3 +11,3 @@ "main": "index.js",

"repository": "github:wework/json-schema-to-openapi-schema",
"author": "Phil Sturgeon",
"author": "Phil Sturgeon <phil.sturgeon@wework.com>",
"license": "MIT",

@@ -14,0 +14,0 @@ "devDependencies": {

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