json-schema-to-openapi-schema
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -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 |
51
index.js
@@ -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; | ||
{ | ||
"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": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45941
30
1669