From 82fa587f0ddff574874dddcd72019a38b155005b Mon Sep 17 00:00:00 2001 From: Lars Trieloff <trieloff@adobe.com> Date: Thu, 14 Dec 2017 14:03:52 +0000 Subject: [PATCH] #14 boolean types and constant values --- examples/docs/README.md | 1 + examples/docs/constants.schema.md | 46 +++++++++++++++++++ examples/docs/simpletypes.schema.md | 12 ++--- .../generated-schemas/constants.schema.json | 23 ++++++++++ examples/schemas/constants.schema.json | 23 ++++++++++ lib/markdownWriter.js | 6 ++- templates/md/boolean-type.ejs | 8 ++++ templates/md/property.ejs | 11 ++++- 8 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 examples/docs/constants.schema.md create mode 100644 examples/generated-schemas/constants.schema.json create mode 100644 examples/schemas/constants.schema.json create mode 100644 templates/md/boolean-type.ejs diff --git a/examples/docs/README.md b/examples/docs/README.md index 264fafc..ea96b3f 100644 --- a/examples/docs/README.md +++ b/examples/docs/README.md @@ -7,6 +7,7 @@ ## / * [Abstract](./abstract.schema.md) – `https://example.com/schemas/abstract` +* [Constant Types](./constants.schema.md) – `https://example.com/schemas/constants` * [Custom](./custom.schema.md) – `https://example.com/schemas/custom` * [Custom](./deepextending.schema.md) – `https://example.com/schemas/deepextending` * [Definitions](./definitions.schema.md) – `https://example.com/schemas/definitions` diff --git a/examples/docs/constants.schema.md b/examples/docs/constants.schema.md new file mode 100644 index 0000000..32320f0 --- /dev/null +++ b/examples/docs/constants.schema.md @@ -0,0 +1,46 @@ +--- +template: reference +foo: bar +--- + +# Constant Types Schema + +``` +https://example.com/schemas/constants +``` + +This is an example schema with examples for properties with constant values + +| Abstract | Extensible | Custom Properties | Defined In | +|----------|------------|-------------------|------------| +| Can be instantiated | No | Forbidden | [constants.schema.json](constants.schema.json) | + +# Constant Types Properties + +| Property | Type | Required | Defined by | +|----------|------|----------|------------| +| [hello](#hello) | `const` | **Required** | Constant Types (this schema) | + +## hello + +A simple string, without strong constraints. + +`hello` +* is **required** +* type: `const` +* defined in this schema + +The value of this property **must** be equal to: + +```json +"World" +``` + +### Known Values + +| Value | Description | +|-------|-------------| +| `hi` | Welcome | +| `bye` | Farewell | + + diff --git a/examples/docs/simpletypes.schema.md b/examples/docs/simpletypes.schema.md index a528e8b..b6354de 100644 --- a/examples/docs/simpletypes.schema.md +++ b/examples/docs/simpletypes.schema.md @@ -31,10 +31,10 @@ This is an example schema with examples for multiple types and their constraints | [string_ipv6](#string_ipv6) | `string` | Optional | Simple Types (this schema) | | [string_uri](#string_uri) | `string` | Optional | Simple Types (this schema) | | [number_unconstrained](#number_unconstrained) | `number` | Optional | Simple Types (this schema) | -| [interger_unconstrained](#interger_unconstrained) | complex | Optional | Simple Types (this schema) | -| [interger_constrained](#interger_constrained) | complex | Optional | Simple Types (this schema) | +| [interger_unconstrained](#interger_unconstrained) | `integer` | Optional | Simple Types (this schema) | +| [interger_constrained](#interger_constrained) | `integer` | Optional | Simple Types (this schema) | | [number_constrained](#number_constrained) | `number` | Optional | Simple Types (this schema) | -| [integer_threes](#integer_threes) | complex | Optional | Simple Types (this schema) | +| [integer_threes](#integer_threes) | `integer` | Optional | Simple Types (this schema) | | [yesno](#yesno) | `boolean` | Optional | Simple Types (this schema) | ## string_unconstrained @@ -400,7 +400,7 @@ Just a whole number. I don't like fractions. `interger_unconstrained` * is optional -* type: complex +* type: `integer` * defined in this schema ### interger_unconstrained Type @@ -425,7 +425,7 @@ Just a whole number. I don't like fractions. Don't get too small `interger_constrained` * is optional -* type: complex +* type: `integer` * defined in this schema ### interger_constrained Type @@ -475,7 +475,7 @@ Guess what number is valid `integer_threes` * is optional -* type: complex +* type: `integer` * defined in this schema ### integer_threes Type diff --git a/examples/generated-schemas/constants.schema.json b/examples/generated-schemas/constants.schema.json new file mode 100644 index 0000000..6ac15c7 --- /dev/null +++ b/examples/generated-schemas/constants.schema.json @@ -0,0 +1,23 @@ +{ + "meta:license": [ + "Copyright 2017 Adobe Systems Incorporated. All rights reserved.", + "This file is licensed to you under the Apache License, Version 2.0 (the 'License');", + "you may not use this file except in compliance with the License. You may obtain a copy", + "of the License at http://www.apache.org/licenses/LICENSE-2.0" + ], + "$schema": "http://json-schema.org/draft-06/schema#", + "$id": "https://example.com/schemas/constants", + "title": "Constant Types", + "type": "object", + "description": "This is an example schema with examples for properties with constant values", + "properties": { + "hello": { + "type": "string", + "const": "World", + "description": "A simple string, without strong constraints." + } + }, + "required": [ + "hello" + ] +} \ No newline at end of file diff --git a/examples/schemas/constants.schema.json b/examples/schemas/constants.schema.json new file mode 100644 index 0000000..f3843c3 --- /dev/null +++ b/examples/schemas/constants.schema.json @@ -0,0 +1,23 @@ +{ + "meta:license": [ + "Copyright 2017 Adobe Systems Incorporated. All rights reserved.", + "This file is licensed to you under the Apache License, Version 2.0 (the 'License');", + "you may not use this file except in compliance with the License. You may obtain a copy", + "of the License at http://www.apache.org/licenses/LICENSE-2.0" + ], + "$schema": "http://json-schema.org/draft-06/schema#", + "$id": "https://example.com/schemas/constants", + "title": "Constant Types", + "type": "object", + "description": "This is an example schema with examples for properties with constant values", + "properties": { + "hello": { + "type": "string", + "const": "World", + "description": "A simple string, without strong constraints." + } + }, + "required": [ + "hello" + ] +} diff --git a/lib/markdownWriter.js b/lib/markdownWriter.js index 8342788..2ad3140 100644 --- a/lib/markdownWriter.js +++ b/lib/markdownWriter.js @@ -78,12 +78,16 @@ function stringifyExamples(examples) { function simpletype(prop) { const type = prop.type; - if (type==='string') { + if (prop.const!==undefined) { + prop.simpletype = '`const`'; + } else if (type==='string') { prop.simpletype = '`string`'; } else if (type==='number') { prop.simpletype = '`number`'; } else if (type==='boolean') { prop.simpletype = '`boolean`'; + } else if (type==='integer') { + prop.simpletype = '`integer`'; } else if (type==='object') { prop.simpletype = '`object`'; } else if (type==='array') { diff --git a/templates/md/boolean-type.ejs b/templates/md/boolean-type.ejs new file mode 100644 index 0000000..a5789ce --- /dev/null +++ b/templates/md/boolean-type.ejs @@ -0,0 +1,8 @@ +<% /** + * Copyright 2017 Adobe Systems Incorporated. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + */ %> + +`<%=schema.type %>` diff --git a/templates/md/property.ejs b/templates/md/property.ejs index b738fbc..3289130 100644 --- a/templates/md/property.ejs +++ b/templates/md/property.ejs @@ -17,6 +17,15 @@ * type: <%=schema.simpletype %> * defined in <% if (schema.$oSchema) { %>[<%= schema.$oSchema.$linkVal %>](<%= schema.$oSchema.$linkPath %>#<%= name %>)<% } else { %>this schema<% } %> +<% if (schema.const!==undefined) { %> +The value of this property **must** be equal to: + + +```json +<%- JSON.stringify(schema.const, null, 2) %> +``` + +<% } else { %> ### <%=name %> Type @@ -34,7 +43,7 @@ Unknown type `<%= schema.type %>`. <%- JSON.stringify(schema, null, 2) %> ``` -<% } %> +<% }} %> ### Known Values -- GitLab