Skip to content

Commit

Permalink
feat(markdown): support item arrays and additionalItems
Browse files Browse the repository at this point in the history
fixes #31
  • Loading branch information
trieloff committed Dec 11, 2019
1 parent 1386ee3 commit c9fbcdf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
20 changes: 20 additions & 0 deletions examples/schemas/arrays.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 28 additions & 1 deletion lib/markdownBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}[]`);
}

Expand Down

0 comments on commit c9fbcdf

Please sign in to comment.