JSFiddle - React, Tailwind, and code Playground
JavaScript
let json = {
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "First name"
},
"lastName": {
"type": "string",
"description": "Last name"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0,
"default": 19
}
},
"required": ["firstName", "lastName"]
}
let str = ''
function escapeStr (str) {
if (str !== undefined) {
return str.replace(/(([\w-]+)?(_|\+|\*)([\w-]+)?)/g, '`$1`')
} else {
return ''
}
}
function strX (str, json, spaces) {
let x = {}
Object.assign(x, json.properties, json.patternProperties)
let req = json.required
for (let prop in x) {
if (x.hasOwnProperty(prop)) {
let type = x[prop].type
if (Array.isArray(type)) {
type = type[0]
}
let description = escapeStr(x[prop].description)
switch (type) {
case 'integer':
case 'number':
type = 'number'
if (x[prop].hasOwnProperty('minimum')) {
description += ' - Mininum: **' + x[prop].minimum + '**'
}
if (x[prop].hasOwnProperty('maximum')) {
description += ' - Maximum: **' + x[prop].maximum + '**'
}
if (x[prop].hasOwnProperty('multipleOf')) {
description += ' - Max precision: **' + x[prop].multipleOf + '**'
}
break
case 'string':
if (x[prop].hasOwnProperty('enum')) {
type = 'enum'
} else {
if (x[prop].hasOwnProperty('minLength')) {
description += ' - Min length: **' + x[prop].minLength + '**'
}
if (x[prop].hasOwnProperty('maxLength')) {
description += ' - Max length: **' + x[prop].maxLength + '**'
}
if (x[prop].hasOwnProperty('format')) {
...