From 0237974c16e860eed2cec008873fb17aa156aa7e Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Tue, 28 Mar 2017 16:11:07 -0500 Subject: [PATCH] Support type property as an array, according to JSON Schema Validation In JSON Schema, the "type" property may be a string or an array of strings. Previously, if type was set as array of strings (i.e., `"type": ["null", "array"]`) and there was also an "items" property defining the array as items of type "integer," then bootprint-json-schema would render the following: null,array Additionally, the "items" property was not rendered. The expected result for the given scenario is: null, integer[] And the "items" property should be rendered with its defintion. This supports the JSON Schema "type" as it is defined to allow an array of strings. If one of the strings in that array is "array," then `items.type` is used to render the datatype, and the "items" property is also rendered. --- handlebars/helpers.js | 11 +++++++++++ handlebars/partials/json-schema/body.hbs | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/handlebars/helpers.js b/handlebars/helpers.js index efa0a92..6740061 100644 --- a/handlebars/helpers.js +++ b/handlebars/helpers.js @@ -101,5 +101,16 @@ function dataType (value) { if (value.type === 'array') { return dataType(value.items || {}) + '[]' } + if (Array.isArray(value.type)) { + var types = '' + value.type.forEach(function (element) { + var subType = {} + subType.type = element + subType.items = value.items || {} + if (types) types += '|' + types += dataType(subType) + }) + if (types) return types + } return value.type } diff --git a/handlebars/partials/json-schema/body.hbs b/handlebars/partials/json-schema/body.hbs index c71356c..6d18e8c 100644 --- a/handlebars/partials/json-schema/body.hbs +++ b/handlebars/partials/json-schema/body.hbs @@ -18,11 +18,11 @@ {{#ifeq type 'object'}} {{>json-schema/type-object}} {{else}} - {{#ifeq type 'array'}} + {{#ifcontains type 'array'}} {{#if items}} {{>json-schema/array-items items}} {{/if}} - {{/ifeq}} + {{/ifcontains}} {{/ifeq}} {{else}} {{>json-schema/type-object}}