Skip to content

Commit 43122ac

Browse files
committed
improve readme with information about conversions
1 parent 9fcb31d commit 43122ac

File tree

1 file changed

+145
-1
lines changed

1 file changed

+145
-1
lines changed

README.md

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,156 @@ Converts a standard [JSON Schema](https://json-schema.org/understanding-json-sch
88

99
As of version 0.3.0, it is now advised to run a schema through a de-referencer like: https://apitools.dev/json-schema-ref-parser/ to properly deal with `$ref`. I have removed my own poor implementation of de-referencing JSON schemas since there are libraries that can do it better than I can.
1010

11-
It should be noted, that de-referencing libraries have their own issues and might not be able to properly parse your JSON. Due to the way OpenAPI v3.0.X Schema Object's are handled, should the referencing not be 100% correct you might face issues using this library and it's output to be used with OpenAPI 3.0.X.
11+
It should be noted, that de-referencing libraries have their own issues and might not be able to properly parse your JSON/output a schema you might expect. Due to the way OpenAPI v3.0.X Schema Object's are handled, should the referencing not be 100% correct you might face issues using this library and it's output to be used with OpenAPI 3.0.X.
1212

1313
## Conversions
1414

1515
This attempts to massage the standard JSON Schema to a compatible OpenAPI v3.0.X Schema Object. There are many properties that are not supported by OpenAPI v3.0.X Schema Object, though have now been supported by [OpenAPI v3.1.X](https://spec.openapis.org/oas/v3.1.0#schema-object). This library should only be used if working with OpenAPI v3.0.X.
1616

17+
### Items as an Array to Object
18+
19+
This will convert a schema of:
20+
```json
21+
{
22+
"type": "object",
23+
"properties": {
24+
"example": {
25+
"type": "array",
26+
"items": [
27+
{
28+
"example2": {
29+
"type": "string"
30+
}
31+
}
32+
]
33+
}
34+
}
35+
}
36+
```
37+
38+
To:
39+
40+
```json
41+
{
42+
"type": "object",
43+
"properties": {
44+
"example": {
45+
"type": "array",
46+
"items": {
47+
"example2": {
48+
"type": "string"
49+
}
50+
}
51+
}
52+
}
53+
}
54+
```
55+
56+
### Types in an array to OneOf
57+
58+
This will convert a schema of:
59+
60+
```json
61+
{
62+
"type": "object",
63+
"properties": {
64+
"example": {
65+
"type": ["string", "number"],
66+
}
67+
}
68+
}
69+
```
70+
71+
To:
72+
73+
```json
74+
{
75+
"type": "object",
76+
"properties": {
77+
"example": {
78+
"oneOf": [
79+
{
80+
"type": "string"
81+
},
82+
{
83+
"type": "number"
84+
}
85+
]
86+
}
87+
}
88+
}
89+
```
90+
91+
### Const to enum
92+
93+
This will convert a schema of:
94+
95+
```json
96+
{
97+
"type": "object",
98+
"properties": {
99+
"example": {
100+
"type": "string",
101+
"const": "Surburbia"
102+
}
103+
}
104+
}
105+
```
106+
107+
To:
108+
109+
```json
110+
{
111+
"type": "object",
112+
"properties": {
113+
"example": {
114+
"type": "string",
115+
"enum": ["Surburbia"]
116+
}
117+
}
118+
}
119+
```
120+
121+
### null types
122+
123+
OpenAPI 3.0.X does not allow for null as a type, so will convert a schema of:
124+
125+
```json
126+
{
127+
"type": "object",
128+
"properties": {
129+
"example": {
130+
"type": "null",
131+
}
132+
}
133+
}
134+
```
135+
136+
To:
137+
138+
```json
139+
{
140+
"type": "object",
141+
"properties": {
142+
"example": {
143+
"nullable": true,
144+
}
145+
}
146+
}
147+
```
148+
149+
### Default to their type
150+
151+
This will convert the `"default":` value to the relevant type. A String to a String, a Number/Integer to a number/Integer, a Boolean to a Boolean and try to manipulate an Object or an Array to either an Object or an Array
152+
153+
### Dependencies, DependentRequired, DependentSchemas
154+
155+
This will try to convert `"dependencies":`, `"dependentRequired":` and `"dependentSchemas":` to a valid `"allOf"` in the case of a `"dependentSchemas"` or an `"anyOf":` schema in the case of a `"dependentRequired"`.
156+
157+
### If/Then/Else
158+
159+
It will try to convert an If/Then/Else schema statement to a valid `"OneOf"` schema.
160+
17161
## Installation and Usage:
18162

19163
Install via npm: `npm install json-schema-for-openapi`.

0 commit comments

Comments
 (0)