This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
forked from saibotsivad/json-schema-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
233 lines (208 loc) · 7.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var coreSchemaTypes = [
'array',
'boolean',
'integer',
'number',
'null',
'object',
'string'
]
function generateElementTitle(octothorpes, elementName, elementType, isRequired, isEnum, example) {
var text = [ octothorpes ]
if(elementName) {
text.push(' `' + elementName + '`')
}
if (elementType || isRequired) {
text.push(' (')
if (elementType) {
text.push(elementType)
}
if (isEnum) {
text.push(', enum')
}
if (isRequired) {
text.push(', required')
}
text.push(')')
}
if (example) {
text.push(' eg: `' + example + '`')
}
return text.join('')
}
function generatePropertyRestrictions(schema) {
var generate = generateSinglePropertyRestriction(schema)
return [
generate('minimum', 'Minimum'),
generate('maximum', 'Maximum'),
generate('pattern', 'Regex pattern'),
generate('minItems', 'Minimum items'),
generate('uniqueItems', 'Unique items')
].filter(function(text) {
return text
}).join('\n')
}
function generateSinglePropertyRestriction(schema) {
return function(key, text) {
if (schema[key]) {
return '* ' + text + ': `' + schema[key] + '`'
} else {
return null
}
}
}
function generateSchemaSectionText(octothorpes, name, isRequired, schema, subSchemas) {
var schemaType = getActualType(schema, subSchemas)
var text = [
generateElementTitle(octothorpes, name, schemaType, isRequired, schema.enum, schema.example),
schema.description
]
if (schemaType === 'object') {
if (schema.properties) {
text.push('Properties of the `' + name + '` object:')
generatePropertySection(octothorpes, schema, subSchemas).forEach(function(section) {
text = text.concat(section)
})
}
if (schema.patternProperties) {
text.push('Pattern properties of the `' + name + '` object pattern regexp:')
generatePropertySection(octothorpes, schema, subSchemas, true).forEach(function(section) {
text = text.concat(section)
})
}
} else if (schemaType === 'array') {
var itemsType = schema.items && schema.items.type
if (!itemsType && schema.items['$ref']) {
itemsType = getActualType(schema.items, subSchemas)
}
if (itemsType && name) {
text.push('The object is an array with all elements of the type `' + itemsType + '`.')
} else if (itemsType) {
text.push('The schema defines an array with all elements of the type `' + itemsType + '`.')
} else {
var validationItems = []
if (schema.items.allOf) {
text.push('The elements of the array must match *all* of the following properties:')
validationItems = schema.items.allOf
} else if (schema.items.anyOf) {
text.push('The elements of the array must match *at least one* of the following properties:')
validationItems = schema.items.anyOf
} else if (schema.items.oneOf) {
text.push('The elements of the array must match *exactly one* of the following properties:')
validationItems = schema.items.oneOf
} else if (schema.items.not) {
text.push('The elements of the array must *not* match the following properties:')
validationItems = schema.items.not
}
if (validationItems.length > 0) {
validationItems.forEach(function(item) {
text = text.concat(generateSchemaSectionText(octothorpes, undefined, false, item, subSchemas))
})
}
}
if (itemsType === 'object') {
text.push('The array object has the following properties:')
generatePropertySection(octothorpes, schema.items, subSchemas).forEach(function(section) {
text = text.concat(section)
})
}
} else if (schema.oneOf) {
text.push('The object must be one of the following types:')
text.push(schema.oneOf.map(function(oneOf) {
return '* `' + subSchemas[oneOf['$ref']] + '`'
}).join('\n'))
}
if (schema.enum) {
text.push('This element must be one of the following enum values:');
text.push(schema.enum.map(function(enumItem) {
return '* `' + enumItem + '`'
}).join('\n'))
}
if (schema.default !== undefined) {
if (schema.default === null || [ "boolean", "number", "string" ].indexOf(typeof schema.default) !== -1) {
text.push('Default: `' + JSON.stringify(schema.default) + '`')
} else {
text.push('Default:')
text.push('```\n' + JSON.stringify(schema.default, null, 2) + '\n```')
}
}
var restrictions = generatePropertyRestrictions(schema)
if (restrictions) {
text.push('Additional restrictions:')
text.push(restrictions)
}
return text
}
function generatePropertySection(octothorpes, schema, subSchemas, isPatternProperties) {
if (isPatternProperties) {
return Object.keys(schema.patternProperties).map(function(patternPropertyKey) {
var patternPropertyIsRequired = schema.required && schema.required.indexOf(patternPropertyKey) >= 0
return generateSchemaSectionText(octothorpes + '#', patternPropertyKey, patternPropertyIsRequired, schema.patternProperties[patternPropertyKey], subSchemas)
})
}
else if (schema.properties) {
return Object.keys(schema.properties).map(function(propertyKey) {
var propertyIsRequired = schema.required && schema.required.indexOf(propertyKey) >= 0
return generateSchemaSectionText(octothorpes + '#', propertyKey, propertyIsRequired, schema.properties[propertyKey], subSchemas)
})
} else if (schema.oneOf) {
var oneOfList = schema.oneOf.map(function(innerSchema) {
return '* `' + getActualType(innerSchema, subSchemas) + '`'
}).join('\n')
return ['This property must be one of the following types:', oneOfList]
} else {
return []
}
}
function getActualType(schema, subSchemas) {
if (schema.type) {
return schema.type
} else if (schema['$ref'] && subSchemas[schema['$ref']]) {
return subSchemas[schema['$ref']]
} else {
return undefined
}
}
module.exports = function(schema, startingOctothorpes) {
var subSchemaTypes = Object.keys(schema.definitions || {}).reduce(function(map, subSchemaTypeName) {
map['#/definitions/' + subSchemaTypeName] = subSchemaTypeName
return map
}, {})
var text = []
var octothorpes = startingOctothorpes || ''
if (schema.title) {
octothorpes += '#'
text.push(octothorpes + ' ' + schema.title)
}
if (schema.type === 'object') {
if (schema.description) {
text.push(schema.description)
}
text.push('The schema defines the following properties:')
generatePropertySection(octothorpes, schema, subSchemaTypes).forEach(function(section) {
text = text.concat(section)
})
} else {
text = text.concat(generateSchemaSectionText('#' + octothorpes, undefined, false, schema, subSchemaTypes));
}
if (schema.definitions) {
text.push('---')
text.push('# Sub Schemas')
text.push('The schema defines the following additional types:')
Object.keys(schema.definitions).forEach(function(subSchemaTypeName) {
text.push('## `' + subSchemaTypeName + '` (' + schema.definitions[subSchemaTypeName].type + ')')
text.push(schema.definitions[subSchemaTypeName].description)
if (schema.definitions[subSchemaTypeName].type === 'object') {
if (schema.definitions[subSchemaTypeName].properties) {
text.push('Properties of the `' + subSchemaTypeName + '` object:')
}
}
generatePropertySection('##', schema.definitions[subSchemaTypeName], subSchemaTypes).forEach(function(section) {
text = text.concat(section)
})
})
}
return text.filter(function(line) {
return !!line
}).join('\n\n')
}