json-schema-to-openapi-schema
Advanced tools
Comparing version 0.1.0 to 0.1.1
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; |
{ | ||
"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 |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
40780
24
1540
101
1