From 70b63c817fe6a1495858a178a7dc0031cecb639a Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Mon, 16 Dec 2019 11:02:30 +0000 Subject: [PATCH] feat(schema): add support for keyword `$defs` --- lib/markdownBuilder.js | 41 +++++++++++---------- schemasupport.md | 6 +-- test/fixtures/readme-1/abstract.schema.json | 2 +- test/markdownBuilder.test.js | 4 +- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/lib/markdownBuilder.js b/lib/markdownBuilder.js index b3bf813c..4b8a9bef 100644 --- a/lib/markdownBuilder.js +++ b/lib/markdownBuilder.js @@ -765,29 +765,32 @@ function build({ * @param {*} schema */ function makedefinitions(schema, slugger) { - if (schema.definitions) { - const defgroups = Object.entries(schema.definitions).map(([groupname, subschema]) => { - const grouptable = makeproptable( - subschema[keyword`properties`], - subschema[keyword`patternProperties`], - subschema[keyword`additionalProperties`], - subschema[keyword`required`], - slugger, - ); - return [ - heading(2, text(i18n`Definitions group ${groupname}`)), - paragraph(text(i18n`Reference this group by using`)), - code('json', JSON.stringify({ $ref: `${subschema[s.id]}#${subschema[s.pointer]}` })), - grouptable, - ...makeproplist( + if (schema.definitions || schema[keyword`$defs`]) { + const defgroups = [ + ...Object.entries(schema[keyword`$defs`] || {}), + ...Object.entries(schema.definitions || {})] + .map(([groupname, subschema]) => { + const grouptable = makeproptable( subschema[keyword`properties`], subschema[keyword`patternProperties`], subschema[keyword`additionalProperties`], subschema[keyword`required`], - 2, - ), - ]; - }); + slugger, + ); + return [ + heading(2, text(i18n`Definitions group ${groupname}`)), + paragraph(text(i18n`Reference this group by using`)), + code('json', JSON.stringify({ $ref: `${subschema[s.id]}#${subschema[s.pointer]}` })), + grouptable, + ...makeproplist( + subschema[keyword`properties`], + subschema[keyword`patternProperties`], + subschema[keyword`additionalProperties`], + subschema[keyword`required`], + 2, + ), + ]; + }); return [ heading(1, text(i18n`${gentitle(schema[s.titles], schema[keyword`type`])} Definitions`)), diff --git a/schemasupport.md b/schemasupport.md index 8f7f8245..ed4fca74 100644 --- a/schemasupport.md +++ b/schemasupport.md @@ -1,16 +1,16 @@ # 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 81% +This report lists the keywords of the JSON Schema spec that are covered in the tests. The overall coverage is 82% ## The JSON Schema Core Vocabulary -Coverage for [The JSON Schema Core Vocabulary](https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.8.1) is 33%. +Coverage for [The JSON Schema Core Vocabulary](https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.8.1) is 44%. | Keyword | Supported | | :----------------- | --------- | | `$anchor` | No | | `$comment` | Yes | -| `$defs` | No | +| `$defs` | Yes | | `$id` | Yes | | `$recursiveAnchor` | No | | `$recursiveRef` | No | diff --git a/test/fixtures/readme-1/abstract.schema.json b/test/fixtures/readme-1/abstract.schema.json index 0ae16f3d..a3917e1b 100644 --- a/test/fixtures/readme-1/abstract.schema.json +++ b/test/fixtures/readme-1/abstract.schema.json @@ -12,7 +12,7 @@ "writeOnly": true, "description": "This is an abstract schema. It has `definitions`, but does not declare any properties", "type": "object", - "definitions": { + "$defs": { "first": { "type": "object", "properties": { diff --git a/test/markdownBuilder.test.js b/test/markdownBuilder.test.js index e8ac3c69..3efc3e1f 100644 --- a/test/markdownBuilder.test.js +++ b/test/markdownBuilder.test.js @@ -258,7 +258,7 @@ describe('Testing Markdown Builder: readme-1', () => { Reference this group by using \`\`\`json -{"$ref":"https://example.com/schemas/abstract#/definitions/second"} +{"$ref":"https://example.com/schemas/abstract#/$defs/second"} \`\`\`` .fuzzy` \`bar\` @@ -266,7 +266,7 @@ Reference this group by using - is optional - Type: \`string\` - cannot be null -- defined in: [Abstract](abstract-definitions-second-properties-bar.md "https://example.com/schemas/abstract#/definitions/second/properties/bar") +- defined in: [Abstract](abstract-defs-second-properties-bar.md "https://example.com/schemas/abstract#/$defs/second/properties/bar") #### bar Type