From 4bf35ddacdd53a1e5d1112fb0f2a0f28e3568323 Mon Sep 17 00:00:00 2001 From: Lars Trieloff <trieloff@adobe.com> Date: Fri, 5 Jan 2018 17:51:44 +0000 Subject: [PATCH] Better handling of nested object types #32 --- examples/docs/abstract.schema.md | 20 +++++++ examples/docs/complex.schema.md | 58 +++++++++++++++---- .../generated-schemas/abstract.schema.json | 5 ++ examples/schemas/abstract.schema.json | 5 ++ templates/md/nested-properties.ejs | 2 +- templates/md/object-type.ejs | 2 +- templates/md/property.ejs | 2 + 7 files changed, 80 insertions(+), 14 deletions(-) diff --git a/examples/docs/abstract.schema.md b/examples/docs/abstract.schema.md index 72799ba..55b9dca 100644 --- a/examples/docs/abstract.schema.md +++ b/examples/docs/abstract.schema.md @@ -21,6 +21,7 @@ This is an abstract schema. It has `definitions`, but does not declare any prope |----------|------|-------| | [bar](#bar) | `string` | `https://example.com/schemas/abstract#/definitions/second` | | [foo](#foo) | `string` | `https://example.com/schemas/abstract#/definitions/first` | +| [nonfoo](#nonfoo) | `const` | `https://example.com/schemas/abstract#/definitions/first` | ## bar @@ -59,3 +60,22 @@ A unique identifier given to every addressable thing. + +## nonfoo + +This is not foo. + +`nonfoo` +* is optional +* type: `const` +* defined in this schema + +The value of this property **must** be equal to: + +```json +false +``` + + + + diff --git a/examples/docs/complex.schema.md b/examples/docs/complex.schema.md index d9d54ff..774ce4f 100644 --- a/examples/docs/complex.schema.md +++ b/examples/docs/complex.schema.md @@ -113,26 +113,60 @@ String or number… ### refabstract Type -Unknown type `object`. + +`object` with following properties: + + +| Property | Type | Required +|----------|------|----------| +| `foo`| string | Optional | +| `nonfoo`| boolean | Optional | + + + +#### foo + +A unique identifier given to every addressable thing. + +`foo` +* is optional +* type: `string` + +##### foo Type + + +`string` + + + + + + + + +#### nonfoo + +This is not foo. + +`nonfoo` +* is optional +* type: `boolean` + +The value of this property **must** be equal to: ```json -{ - "type": "object", - "properties": { - "foo": { - "type": "string", - "description": "A unique identifier given to every addressable thing." - } - }, - "required": true, - "simpletype": "`object`" -} +false ``` + + + + + ## reflist diff --git a/examples/generated-schemas/abstract.schema.json b/examples/generated-schemas/abstract.schema.json index f202ac4..780b192 100644 --- a/examples/generated-schemas/abstract.schema.json +++ b/examples/generated-schemas/abstract.schema.json @@ -17,6 +17,11 @@ "foo": { "type": "string", "description": "A unique identifier given to every addressable thing." + }, + "nonfoo": { + "type": "boolean", + "const": false, + "description": "This is not foo." } } }, diff --git a/examples/schemas/abstract.schema.json b/examples/schemas/abstract.schema.json index 2f170c0..5d3a9ba 100644 --- a/examples/schemas/abstract.schema.json +++ b/examples/schemas/abstract.schema.json @@ -17,6 +17,11 @@ "foo": { "type": "string", "description": "A unique identifier given to every addressable thing." + }, + "nonfoo": { + "type": "boolean", + "const": false, + "description": "This is not foo." } } }, diff --git a/templates/md/nested-properties.ejs b/templates/md/nested-properties.ejs index 66143c0..1cc93ad 100644 --- a/templates/md/nested-properties.ejs +++ b/templates/md/nested-properties.ejs @@ -10,7 +10,7 @@ |----------|------|----------| <% _.keys(props).sort().forEach(property => { const schema = props[property]; %> -| `<%= property %>`| <%= schema.type %> | <%= (outer.required&&outer.required.indexOf(property)>=0) ? "**Required**" : "Optional" %> | +| `<%= property %>`| <%= schema.type %> | <%= (Array.isArray(outer.required)&&outer.required.indexOf(property)>=0) ? "**Required**" : "Optional" %> | <% }); %> \ No newline at end of file diff --git a/templates/md/object-type.ejs b/templates/md/object-type.ejs index cc1902a..00cbd60 100644 --- a/templates/md/object-type.ejs +++ b/templates/md/object-type.ejs @@ -12,7 +12,7 @@ <% _.keys(schema.properties).sort().forEach(property => { const inner = schema.properties[property]; inner.simpletype = "`" + inner.type + "`"; - const required = (schema.required&&schema.required.indexOf(property)>=0); %> + const required = (Array.isArray(schema.required)&&schema.required.indexOf(property)>=0); %> <%- include("nested-property",{ _:_, diff --git a/templates/md/property.ejs b/templates/md/property.ejs index f1dafd0..522c68f 100644 --- a/templates/md/property.ejs +++ b/templates/md/property.ejs @@ -41,6 +41,8 @@ The value of this property **must** be equal to one of the [known values below]( <%- include("number-type",{schema:schema,_:_}) %> <% } else if (schema.type==="boolean") { %> <%- include("boolean-type",{schema:schema,_:_}) %> +<% } else if (schema.type==="object") { %> +<%- include("object-type",{schema:schema,_:_}) %> <% } else if (schema.type==="array") { %> <%- include("array-type",{schema:schema,_:_,nested:false,ejs:ejs}) %> <% } else if (schema.$ref!==undefined) { %> -- GitLab