Skip to content

Commit 8cff104

Browse files
authored
Merge pull request #12 from JaredAAT/test-real-schemas
Test real schemas
2 parents 018b476 + c52da8c commit 8cff104

File tree

9 files changed

+550
-7
lines changed

9 files changed

+550
-7
lines changed

package-lock.json

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"devDependencies": {
2727
"chai": "^4.3.6",
2828
"mocha": "^10.0.0",
29+
"node-fetch": "^2.6.7",
2930
"oas-validator": "^5.0.8",
3031
"sinon": "^14.0.0"
3132
},

src/Convertor.js

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ class Convertor {
77
constructor(schema) {
88
this.schema = JSON.parse(JSON.stringify(schema))
99

10-
// this.specialProperties = ['allOf', 'oneOf', 'anyOf', 'not', 'items', 'properties', 'additionalProperties']
11-
this.specialProperties = ['allOf', 'anyOf', 'items', 'oneOf', 'not', 'properties']
10+
this.specialProperties = ['allOf', 'anyOf', 'items', 'oneOf', 'not', 'properties', 'additionalProperties']
1211
this.ofProperties = ['allOf', 'anyOf', 'oneOf']
1312
this.referencedSchemas = {}
14-
this.bannedKeyWords = ['$schema', '$comment', '$id', 'version', 'examples']
13+
this.bannedKeyWords = ['$schema', '$comment', '$id', 'version', 'examples', 'id']
1514

1615
this.components = {
1716
schemas: {}
@@ -53,16 +52,107 @@ class Convertor {
5352
}
5453
}
5554

56-
bannedWordsRemoval()
57-
5855
const convertNull = () => {
5956
if (schema.type === 'null') {
6057
schema.nullable = true
6158
delete schema.type
6259
}
6360
}
6461

62+
const convertTypeArrays = () => {
63+
if (Array.isArray(schema.type)) {
64+
const oneOf = []
65+
for (const type of schema.type) {
66+
const obj = {}
67+
if (type === 'null') {
68+
obj.nullable = true
69+
} else {
70+
obj.type = type
71+
if (schema.default) {
72+
obj.default = schema.default
73+
delete schema.default
74+
}
75+
76+
for (const property of Object.keys(schema)) {
77+
if (type === 'array' && property === 'items') {
78+
obj.items = schema[property]
79+
delete schema.items
80+
}
81+
82+
if (type === 'object' && property === 'properties') {
83+
obj.properties = schema[property]
84+
delete schema.properties
85+
}
86+
}
87+
}
88+
89+
oneOf.push(obj)
90+
}
91+
schema.oneOf = oneOf
92+
delete schema.type
93+
}
94+
}
95+
96+
const convertItemArrays = () => {
97+
if (Array.isArray(schema.items)) {
98+
const obj = {}
99+
for (const item of schema.items) {
100+
Object.assign(obj, item)
101+
}
102+
schema.items = obj
103+
}
104+
}
105+
106+
const convertDefaultValues = () => {
107+
if (schema.type === 'boolean') {
108+
if (schema.default === 'true' || schema.default === 'false') {
109+
if (schema.default === 'true')
110+
schema.default = true
111+
else
112+
schema.default = false
113+
}
114+
}
115+
116+
if (schema.type === 'number' || schema.type === 'integer') {
117+
if (typeof schema.default === 'string') {
118+
schema.default = parseInt(schema.default, 10) || 1
119+
}
120+
}
121+
122+
if (schema.type === 'array' && schema.items === undefined) {
123+
schema.items = {nullable: true}
124+
}
125+
126+
if (schema.type === 'string') {
127+
if (Object.keys(schema).indexOf('default') !== -1) {
128+
schema.default = `${schema.default}`
129+
}
130+
}
131+
}
132+
133+
const defaultValuesForOfProperties = () => {
134+
if (schema.type === undefined && this.ofProperties.some(element => Object.keys(schema).includes(element))) {
135+
if (Object.keys(schema).includes('default')) {
136+
for (const property of this.ofProperties) {
137+
if (Object.keys(schema).includes(property)) {
138+
for (const ofProperty of schema[property]) {
139+
if (ofProperty.type !== 'null' || ofProperty.nullable) {
140+
ofProperty.default = schema.default
141+
delete schema.default
142+
}
143+
}
144+
}
145+
}
146+
}
147+
}
148+
}
149+
65150
convertNull()
151+
convertTypeArrays()
152+
convertItemArrays()
153+
bannedWordsRemoval()
154+
convertDefaultValues()
155+
defaultValuesForOfProperties()
66156

67157
if (this.specialProperties.indexOf(parentKeyword) !== -1) {
68158
if (this.ofProperties.indexOf(parentKeyword) !== -1) {
@@ -111,7 +201,7 @@ class Convertor {
111201

112202
removeEmpty(schema) {
113203
Object.keys(schema).forEach(key => {
114-
if (schema[key]
204+
if (schema[key]
115205
&& typeof schema[key] === 'object'
116206
&& this.removeEmpty(schema[key]) === null) {
117207
delete schema[key]
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"title": "JSON API Schema",
4+
"description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
5+
"type": "object",
6+
"required": [
7+
"errors"
8+
],
9+
"properties": {
10+
"errors": {
11+
"type": "object"
12+
},
13+
"Logging": {
14+
"$ref": "#/definitions/logging"
15+
}
16+
},
17+
"definitions": {
18+
"logLevel": {
19+
"description": "Log level configurations used when creating logs. Only logs that exceeds its matching log level will be enabled. Each log level configuration has a category specified by its JSON property name. For more information about configuring log levels, see https://docs.microsoft.com/aspnet/core/fundamentals/logging/#configure-logging.",
20+
"type": "object",
21+
"additionalProperties": {
22+
"$ref": "#/definitions/logLevelThreshold"
23+
}
24+
},
25+
"logLevelThreshold": {
26+
"description": "Log level threshold.",
27+
"type": "string",
28+
"enum": [
29+
"Trace",
30+
"Debug",
31+
"Information",
32+
"Warning",
33+
"Error",
34+
"Critical",
35+
"None"
36+
]
37+
},
38+
"logging": {
39+
"type": "object",
40+
"description": "Configuration for Microsoft.Extensions.Logging.",
41+
"properties": {
42+
"LogLevel": {
43+
"$ref": "#/definitions/logLevel"
44+
},
45+
"Console": {
46+
"properties": {
47+
"LogLevel": {
48+
"$ref": "#/definitions/logLevel"
49+
},
50+
"FormatterName": {
51+
"description": "Name of the log message formatter to use. Defaults to 'simple'.",
52+
"type": "string",
53+
"default": "simple"
54+
},
55+
"FormatterOptions": {
56+
"description": "Log message formatter options. Additional properties are available on the options depending on the configured formatter. The formatter is specified by FormatterName.",
57+
"type": "object",
58+
"properties": {
59+
"IncludeScopes": {
60+
"description": "Include scopes when true. Defaults to false.",
61+
"type": "boolean",
62+
"default": false
63+
},
64+
"TimestampFormat": {
65+
"description": "Format string used to format timestamp in logging messages. Defaults to null.",
66+
"type": "string"
67+
},
68+
"UseUtcTimestamp": {
69+
"description": "Indication whether or not UTC timezone should be used to for timestamps in logging messages. Defaults to false.",
70+
"type": "boolean",
71+
"default": false
72+
}
73+
}
74+
},
75+
"LogToStandardErrorThreshold": {
76+
"description": "The minimum level of messages are written to Console.Error.",
77+
"$ref": "#/definitions/logLevelThreshold"
78+
}
79+
}
80+
},
81+
"EventSource": {
82+
"properties": {
83+
"LogLevel": {
84+
"$ref": "#/definitions/logLevel"
85+
}
86+
}
87+
},
88+
"Debug": {
89+
"properties": {
90+
"LogLevel": {
91+
"$ref": "#/definitions/logLevel"
92+
}
93+
}
94+
},
95+
"EventLog": {
96+
"properties": {
97+
"LogLevel": {
98+
"$ref": "#/definitions/logLevel"
99+
}
100+
}
101+
},
102+
"ElmahIo": {
103+
"properties": {
104+
"LogLevel": {
105+
"$ref": "#/definitions/logLevel"
106+
}
107+
}
108+
},
109+
"ElmahIoBreadcrumbs": {
110+
"properties": {
111+
"LogLevel": {
112+
"$ref": "#/definitions/logLevel"
113+
}
114+
}
115+
}
116+
},
117+
"additionalProperties": {
118+
"type": "object",
119+
"description": "Logging configuration for a provider. The provider name must match the configuration's JSON property property name.",
120+
"properties": {
121+
"LogLevel": {
122+
"$ref": "#/definitions/logLevel"
123+
}
124+
}
125+
}
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)