Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 227438d

Browse files
author
Phil Sturgeon
committed
Converts depencies to allOf[oneOf] stuff
1 parent ec803cf commit 227438d

File tree

3 files changed

+76
-19
lines changed

3 files changed

+76
-19
lines changed

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ A little NodeJS package to convert JSON Schema to [OpenAPI Schema Objects](https
77
* converts JSON Schema Draft 00 Wright (a.k.a draft v5) to OpenAPI 3.0 Schema Object
88
* switches `type: ['foo', 'null']` to `type: foo` and `nullable: true`
99
* supports deep structures with nested `allOf`s etc.
10-
* switches `patternProperties` to `x-patternProperties` in the Schema Object
10+
* switches `patternProperties` to `x-patternProperties`
11+
* converts `dependencies` to an allOf + oneOf OpenAPI-valid equivalent
1112

1213
## Installation
1314

‎index.js‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ function convertSchema(schema) {
6161
}
6262

6363
validateType(schema.type);
64+
6465
schema = convertTypes(schema);
66+
schema = convertDependencies(schema);
6567

6668
if (typeof schema['patternProperties'] === 'object') {
6769
schema = convertPatternProperties(schema);
@@ -80,10 +82,9 @@ function validateType(type) {
8082
}
8183

8284
function convertProperties(properties) {
83-
var key
84-
, property
85-
, props = {}
86-
;
85+
let key = {};
86+
let property = {};
87+
let props = {};
8788

8889
for (key in properties) {
8990
property = properties[key];
@@ -93,6 +94,51 @@ function convertProperties(properties) {
9394
return props;
9495
}
9596

97+
function convertDependencies(schema) {
98+
const deps = schema.dependencies;
99+
if (typeof deps !== 'object') {
100+
return schema;
101+
}
102+
103+
// Turns the dependencies keyword into an allOf of oneOf's
104+
// "dependencies": {
105+
// "post-office-box": ["street-address"]
106+
// },
107+
//
108+
// becomes
109+
//
110+
// "allOf": [
111+
// {
112+
// "oneOf": [
113+
// {"not": {"required": ["post-office-box"]}},
114+
// {"required": ["post-office-box", "street-address"]}
115+
// ]
116+
// }
117+
//
118+
119+
delete schema['dependencies'];
120+
if (!Array.isArray(schema.allOf)) {
121+
schema.allOf = [];
122+
}
123+
124+
for (const key in deps) {
125+
const foo = {
126+
'oneOf': [
127+
{
128+
'not': {
129+
'required': [key]
130+
}
131+
},
132+
{
133+
'required': [].concat(key, deps[key])
134+
}
135+
]
136+
};
137+
schema.allOf.push(foo);
138+
}
139+
return schema;
140+
}
141+
96142
function convertTypes(schema) {
97143
var newType;
98144

‎test/schemas/address/openapi.json‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
{
2-
"description": "An Address following the convention of http://microformats.org/wiki/hcard",
3-
"type": "object",
4-
"properties": {
5-
"post-office-box": { "type": "string" },
6-
"extended-address": { "type": "string" },
7-
"street-address": { "type": "string" },
8-
"locality":{ "type": "string" },
9-
"region": { "type": "string" },
10-
"postal-code": { "type": "string" },
11-
"country-name": { "type": "string"}
2+
"description": "An Address following the convention of http://microformats.org/wiki/hcard",
3+
"type": "object",
4+
"properties": {
5+
"post-office-box": { "type": "string" },
6+
"extended-address": { "type": "string" },
7+
"street-address": { "type": "string" },
8+
"locality":{ "type": "string" },
9+
"region": { "type": "string" },
10+
"postal-code": { "type": "string" },
11+
"country-name": { "type": "string"}
12+
},
13+
"required": ["locality", "region", "country-name"],
14+
"allOf": [
15+
{
16+
"oneOf": [
17+
{"not": {"required": ["post-office-box"]}},
18+
{"required": ["post-office-box", "street-address"]}
19+
]
1220
},
13-
"required": ["locality", "region", "country-name"],
14-
"x-json-schema-dependencies": {
15-
"post-office-box": ["street-address"],
16-
"extended-address": ["street-address"]
21+
{
22+
"oneOf": [
23+
{"not": {"required": ["extended-address"]}},
24+
{"required": ["extended-address", "street-address"]}
25+
]
1726
}
27+
]
1828
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /