From 96ca3a62dbe8b0624bfa0f1627896f2b3721e69a Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Mon, 16 Dec 2019 09:51:33 +0000 Subject: [PATCH] feat(schema): add full support for "A Vocabulary for the Contents of String-Encoded Data" --- lib/markdownBuilder.js | 13 +++++++++++++ schemasupport.md | 10 +++++----- test/fixtures/content/html.schema.json | 5 +++++ test/fixtures/content/jwt.schema.json | 26 ++++++++++++++++++++++++++ test/fixtures/content/png.schema.json | 6 ++++++ test/markdownBuilder.test.js | 26 ++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/content/html.schema.json create mode 100644 test/fixtures/content/jwt.schema.json create mode 100644 test/fixtures/content/png.schema.json diff --git a/lib/markdownBuilder.js b/lib/markdownBuilder.js index a3462b6b..9332af42 100644 --- a/lib/markdownBuilder.js +++ b/lib/markdownBuilder.js @@ -578,6 +578,19 @@ function build({ constraints.push(paragraph([strong(text(i18n`unknown format`)), text(': '), text(i18n`the value of this string must follow the format: `), inlineCode(String(schema.format))])); } + if (schema[keyword`contentEncoding`]) { + constraints.push(paragraph([strong(text(i18n`encoding`)), text(': '), text(i18n`the string content must be using the ${schema[keyword`contentEncoding`]} content encoding.`)])); + } + if (schema[keyword`contentMediaType`]) { + constraints.push(paragraph([strong(text(i18n`media type`)), text(': '), text(i18n`the media type of the contents of this string is: `), inlineCode(String(schema[keyword`contentMediaType`]))])); + } + if (schema[keyword`contentSchema`]) { + constraints.push(paragraph([ + strong(text(i18n`schema`)), text(': '), + text(i18n`the contents of this string should follow this schema: `), + link(`${schema[keyword`contentSchema`][s.slug]}.md`, i18n`check type definition`, text(gentitle(schema[keyword`contentSchema`][s.titles], schema[keyword`contentSchema`][keyword`type`])))])); + } + // https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.4 if (schema[keyword`maxItems`] !== undefined) { // console.log('maxItems!', schema[s.filename], schema[s.pointer]); diff --git a/schemasupport.md b/schemasupport.md index f73f700d..84cc5a23 100644 --- a/schemasupport.md +++ b/schemasupport.md @@ -1,6 +1,6 @@ # JSON Schema Spec Coverage Report -This report lists the keywords of the JSON Schema spec that are covered in the tests. The overall coverage is 69% +This report lists the keywords of the JSON Schema spec that are covered in the tests. The overall coverage is 73% ## The JSON Schema Core Vocabulary @@ -125,13 +125,13 @@ Coverage for [Defined Formats](https://json-schema.org/draft/2019-09/json-schema ## A Vocabulary for the Contents of String-Encoded Data -Coverage for [A Vocabulary for the Contents of String-Encoded Data](https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.8) is 0%. +Coverage for [A Vocabulary for the Contents of String-Encoded Data](https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.8) is 100%. | Keyword | Supported | | :----------------- | --------- | -| `contentEncoding` | No | -| `contentMediaType` | No | -| `contentSchema` | No | +| `contentEncoding` | Yes | +| `contentMediaType` | Yes | +| `contentSchema` | Yes | ## A Vocabulary for Basic Meta-Data Annotations diff --git a/test/fixtures/content/html.schema.json b/test/fixtures/content/html.schema.json new file mode 100644 index 00000000..49011b86 --- /dev/null +++ b/test/fixtures/content/html.schema.json @@ -0,0 +1,5 @@ +{ + "type": "string", + "title": "HTML", + "contentMediaType": "text/html" +} \ No newline at end of file diff --git a/test/fixtures/content/jwt.schema.json b/test/fixtures/content/jwt.schema.json new file mode 100644 index 00000000..06129014 --- /dev/null +++ b/test/fixtures/content/jwt.schema.json @@ -0,0 +1,26 @@ +{ + "type": "string", + "title": "JWT", + "contentMediaType": "application/jwt", + "contentSchema": { + "title": "JSON Web Token", + "type": "array", + "minItems": 2, + "items": [ + { + "const": { + "typ": "JWT", + "alg": "HS256" + } + }, + { + "type": "object", + "required": ["iss", "exp"], + "properties": { + "iss": {"type": "string"}, + "exp": {"type": "integer"} + } + } + ] + } +} \ No newline at end of file diff --git a/test/fixtures/content/png.schema.json b/test/fixtures/content/png.schema.json new file mode 100644 index 00000000..14ebd39b --- /dev/null +++ b/test/fixtures/content/png.schema.json @@ -0,0 +1,6 @@ +{ + "type": "string", + "title": "PNG", + "contentEncoding": "base64", + "contentMediaType": "image/png" +} \ No newline at end of file diff --git a/test/markdownBuilder.test.js b/test/markdownBuilder.test.js index 6dec88c7..273769fd 100644 --- a/test/markdownBuilder.test.js +++ b/test/markdownBuilder.test.js @@ -15,6 +15,32 @@ const { assertMarkdown, loadschemas } = require('./testUtils'); const build = require('../lib/markdownBuilder'); +describe('Testing Markdown Builder: content', () => { + let results; + + before(async () => { + const schemas = await loadschemas('content'); + const builder = build({ header: false }); + results = builder(schemas); + }); + + it('PNG Schema looks OK', () => { + assertMarkdown(results.png) + .contains('**encoding**: the string content must be using the base64 content encoding.'); + }); + + it('HTML Schema looks OK', () => { + assertMarkdown(results.html) + .contains('**media type**: the media type of the contents of this string is: `text/html`'); + }); + + it('JWT Schema looks OK', () => { + assertMarkdown(results.jwt) + .contains('**schema**: the contents of this string should follow this schema: [JSON Web Token](jwt-json-web-token.md "check type definition")') + .contains('**media type**: the media type of the contents of this string is: `application/jwt`'); + }); +}); + describe('Testing Markdown Builder: not', () => { let results;