From c9fbcdfddb43e22ba6eaac30dbf4233f25a6ec78 Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Wed, 11 Dec 2019 13:44:47 +0000 Subject: [PATCH] feat(markdown): support item arrays and additionalItems fixes #31 --- examples/schemas/arrays.schema.json | 20 ++++++++++++++++++++ lib/markdownBuilder.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/examples/schemas/arrays.schema.json b/examples/schemas/arrays.schema.json index 64fc5219..944a99ab 100644 --- a/examples/schemas/arrays.schema.json +++ b/examples/schemas/arrays.schema.json @@ -11,6 +11,26 @@ "type": "object", "description": "This is an example schema with examples for multiple array types and their constraints.", "properties": { + "tuple": { + "type": "array", + "description": "This is an array of two values, one positive, one negative. All additional values must be 0.", + "items": [ + { + "title": "Positive Integer", + "type": "integer", + "minimum": 0 + }, + { + "title": "Negative Integer", + "type": "integer", + "maximum": 0 + } + ], + "additionalItems": { + "title": "Zero", + "const": 0 + } + }, "list": { "type": "array", "description": "This is an array", diff --git a/lib/markdownBuilder.js b/lib/markdownBuilder.js index 75e1b38b..8cae01f8 100644 --- a/lib/markdownBuilder.js +++ b/lib/markdownBuilder.js @@ -317,6 +317,31 @@ function build({ header, links = {}, includeproperties = [] } = {}) { ]); } + function makearrayfact(items, additional) { + return listItem([ + paragraph([text(i18n`Type: `), text(i18n`an array where each item follows the corresponding schema in the following list:`)]), + list('ordered', + [...items.map(schema => listItem(paragraph(link( + `${schema[s.slug].md}`, + i18n`check type definition`, + text(gentitle(schema[s.titles], schema.type)), + )))), + ...(() => { + if (additional === true) { + return [listItem(paragraph(text(i18n`and all following items may follow any schema`)))]; + } else if (typeof additional === 'object') { + return [listItem(paragraph([text(i18n`and all following items must follow the schema: `), + link( + `${additional[s.slug].md}`, + i18n`check type definition`, + text(gentitle(additional[s.titles], additional.type)), + )]))]; + } + return []; + })(), + ])]); + } + function maketypefact(definition, isarray = '') { const alltypes = Array.isArray(definition.type) ? definition.type : [definition.type]; // filter out types that are null @@ -332,7 +357,9 @@ function build({ header, links = {}, includeproperties = [] } = {}) { const array = firsttype === keyword`array`; const merged = !!(definition.allOf || definition.anyOf || definition.oneOf || definition.not); - if (array && definition.items) { + if (array && Array.isArray(definition.items)) { + return makearrayfact(definition.items, definition.additionalItems); + } else if (array && definition.items) { return maketypefact(definition.items, `${isarray}[]`); }