diff --git a/lib/renderer-markdown.js b/lib/renderer-markdown.js index 2ebd831..231358d 100644 --- a/lib/renderer-markdown.js +++ b/lib/renderer-markdown.js @@ -8,13 +8,17 @@ const readFile = util.promisify(fs.readFile); let refLinkBasePath = ""; +const upperCaseFirstCharacter = (value) => { + return value.substr(0, 1).toUpperCase() + value.substr(1); +}; + const typeToLinkLabel = (label, x) => { if (Array.isArray(label)) { label = label.join(", "); label = `[${label}]`; } else { label = label || ""; - label = label.substr(0, 1).toUpperCase() + label.substr(1); + label = upperCaseFirstCharacter(label); label = label.replace(".md", "html"); } return label; @@ -106,6 +110,13 @@ const attributeRow = (name, property, parentKey) => { href = parentKey.toLowerCase(); } href += name.toLowerCase(); + + let typeLabel; + if (property.const) { + typeLabel = `${upperCaseFirstCharacter(property.type)}=${property.const}`; + } else { + typeLabel = jsonSchemaType(property); + } let html = "" + '" + "" + - jsonSchemaType(property) + + typeLabel + "" + ""; diff --git a/package-lock.json b/package-lock.json index 5502d29..54f7c38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "json-schema-static-docs", - "version": "0.13.0", + "version": "0.15.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1462,9 +1462,9 @@ "dev": true }, "caniuse-lite": { - "version": "1.0.30001280", - "resolved": "https://nexus.tooling.dvla.gov.uk/repository/npm-group/caniuse-lite/-/caniuse-lite-1.0.30001280.tgz", - "integrity": "sha512-kFXwYvHe5rix25uwueBxC569o53J6TpnGu0BEEn+6Lhl2vsnAumRFWEBhDft1fwyo6m1r4i+RqA4+163FpeFcA==", + "version": "1.0.30001439", + "resolved": "https://nexus.tooling.dvla.gov.uk/repository/npm-group/caniuse-lite/-/caniuse-lite-1.0.30001439.tgz", + "integrity": "sha512-1MgUzEkoMO6gKfXflStpYgZDlFM7M/ck/bgfVCACO5vnAf0fXoNVHdWtqGU+MYca+4bL9Z5bpOVmR33cWW9G2A==", "dev": true }, "chalk": { diff --git a/package.json b/package.json index 463a774..9674962 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,8 @@ "extend": "^3.0.2", "fast-glob": "^3.2.12", "handlebars": "^4.7.7", - "jsonpointer": "^5.0.1", "json-schema-ref-parser": "^9.0.9", + "jsonpointer": "^5.0.1", "lodash": ">=4.17.21", "make-dir": "^3.1.0", "mkdirp": "^1.0.4", diff --git a/templates/schema.hbs b/templates/schema.hbs index c39fc6a..f6a305c 100644 --- a/templates/schema.hbs +++ b/templates/schema.hbs @@ -94,6 +94,10 @@ {{/if}} {{propertyTypeRow this }} + {{#if this.const}} + Const + {{this.const}} + {{/if}} {{#if this.enum}} Enum {{enumValues this.enum}} diff --git a/tests/renderer-markdown.test.js b/tests/renderer-markdown.test.js index 74d632a..9244343 100644 --- a/tests/renderer-markdown.test.js +++ b/tests/renderer-markdown.test.js @@ -34,6 +34,18 @@ let defaultMergedSchema = { }, }, }; +const defaultMergedSchemaWithConst = { + schema: { + title: "My Schema", + properties: { + property1: { + title: "Property 1", + type: "string", + const: "foo", + }, + }, + }, +}; let mergedSchema = {}; const removeFormatting = (value) => { @@ -61,16 +73,35 @@ test("renders attributes", async () => { let result = rendererMarkdown.renderSchema(mergedSchema); result = result.match(/## Properties(.*\n)*/)[0]; + result = removeFormatting(result); let expectedText = - "## Properties\n\n" + + "## Properties" + '' + '' + '' + '' + "
NameType
property1String
property2[string, integer]
property3Array [Property3.html]
"; - expect(result).toEqual(expect.stringContaining(expectedText)); + expect(result).toContain(expectedText); +}); + +test("renders attributes with const", async () => { + expect.assertions(1); + rendererMarkdown = new RendererMarkdown(defaultTemplatePath); + await rendererMarkdown.setup(); + let result = rendererMarkdown.renderSchema(defaultMergedSchemaWithConst); + + result = result.match(/## Properties(.*\n)*/)[0]; + result = removeFormatting(result); + + let expectedText = + "## Properties" + + '' + + '' + + "
NameType
property1String=foo
"; + + expect(result).toContain(expectedText); }); test("renders string property enums", async () => { @@ -83,12 +114,30 @@ test("renders string property enums", async () => { result = removeFormatting(result); let expectedText = - "" + 'TitleProperty 1'; - +'RequiredYes' + + 'TitleProperty 1' + + 'RequiredYes' + 'TypeString' + 'Enum'; - expect(result).toEqual(expect.stringContaining(expectedText)); + expect(result).toContain(expectedText); +}); + +test("renders string property with const", async () => { + expect.assertions(1); + mergedSchema = _.cloneDeep(defaultMergedSchemaWithConst); + rendererMarkdown = new RendererMarkdown(defaultTemplatePath); + await rendererMarkdown.setup(); + let result = rendererMarkdown.renderSchema(mergedSchema); + + result = result.match(/## property1(.*\n)*/)[0]; + result = removeFormatting(result); + + let expectedText = + 'TitleProperty 1' + + 'TypeString' + + 'Constfoo'; + + expect(result).toContain(expectedText); }); test("renders array property types", async () => { @@ -106,5 +155,5 @@ test("renders array property types", async () => { let expectedType = 'TypeArray [
Property3.html]'; - expect(result).toEqual(expect.stringContaining(expectedType)); + expect(result).toContain(expectedType); });