From c3d5f2c7a216d3ea2380a64159566096a1c7731e Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 10 Aug 2022 08:36:43 -0400 Subject: [PATCH] Upgrade "definitions" to "$defs" in "$ref" JSON Pointers (#45) While we currently upgrade "definitions" to "$defs" in the JSON Schema definition, pointers going through "definitions" are currently left intact. See: https://github.com/sourcemeta/alterschema/issues/43 Signed-off-by: Juan Cruz Viotti --- bindings/node/index.js | 3 +++ rules/jsonschema-draft7-to-2019-09.json | 25 ++++++++++++++++++++ test/rules/jsonschema-draft4-to-2019-09.json | 10 ++++++++ test/rules/jsonschema-draft4-to-2020-12.json | 10 ++++++++ test/rules/jsonschema-draft6-to-2019-09.json | 10 ++++++++ test/rules/jsonschema-draft6-to-2020-12.json | 10 ++++++++ test/rules/jsonschema-draft7-to-2019-09.json | 10 ++++++++ test/rules/jsonschema-draft7-to-2020-12.json | 25 ++++++++++++++++++++ 8 files changed, 103 insertions(+) diff --git a/bindings/node/index.js b/bindings/node/index.js index 0e1589d..4e44a36 100644 --- a/bindings/node/index.js +++ b/bindings/node/index.js @@ -18,6 +18,9 @@ async function transformer (root, path, ruleset) { // TODO: Use standard JSON-e operators instead omit: (object, keys) => { return _.omit(object, _.castArray(keys)) + }, + replace: (value, string, replacement) => { + return value.replaceAll(string, replacement) } }) diff --git a/rules/jsonschema-draft7-to-2019-09.json b/rules/jsonschema-draft7-to-2019-09.json index f93a61f..9940ab2 100644 --- a/rules/jsonschema-draft7-to-2019-09.json +++ b/rules/jsonschema-draft7-to-2019-09.json @@ -70,6 +70,31 @@ ] } }, + { + "vocabulary": "core", + "condition": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://github.com/sourcemeta/alterschema/rules/jsonschema-draft7-to-2019-09/definitions-in-ref", + "type": "object", + "required": [ "$ref" ], + "properties": { + "$ref": { + "type": "string", + "pattern": "/definitions/" + } + } + }, + "transform": { + "$merge": [ + { "$eval": "omit(schema, '$ref')" }, + { + "$$ref": { + "$eval": "replace(schema['$ref'], 'definitions', '$defs')" + } + } + ] + } + }, { "vocabulary": "core", "condition": { diff --git a/test/rules/jsonschema-draft4-to-2019-09.json b/test/rules/jsonschema-draft4-to-2019-09.json index 271994b..b018fda 100644 --- a/test/rules/jsonschema-draft4-to-2019-09.json +++ b/test/rules/jsonschema-draft4-to-2019-09.json @@ -82,12 +82,22 @@ "name": "draft4 definitions", "schema": { "$schema": "http://json-schema.org/draft-04/schema#", + "properties": { + "foo": { + "$ref": "#/definitions/foo" + } + }, "definitions": { "foo": { "type": "string" } } }, "expected": { "$schema": "https://json-schema.org/draft/2019-09/schema", + "properties": { + "foo": { + "$ref": "#/$defs/foo" + } + }, "$defs": { "foo": { "type": "string" } } diff --git a/test/rules/jsonschema-draft4-to-2020-12.json b/test/rules/jsonschema-draft4-to-2020-12.json index 6495461..226f92d 100644 --- a/test/rules/jsonschema-draft4-to-2020-12.json +++ b/test/rules/jsonschema-draft4-to-2020-12.json @@ -82,12 +82,22 @@ "name": "draft4 definitions", "schema": { "$schema": "http://json-schema.org/draft-04/schema#", + "properties": { + "foo": { + "$ref": "#/definitions/foo" + } + }, "definitions": { "foo": { "type": "string" } } }, "expected": { "$schema": "https://json-schema.org/draft/2020-12/schema", + "properties": { + "foo": { + "$ref": "#/$defs/foo" + } + }, "$defs": { "foo": { "type": "string" } } diff --git a/test/rules/jsonschema-draft6-to-2019-09.json b/test/rules/jsonschema-draft6-to-2019-09.json index 1caf57b..64aa2d7 100644 --- a/test/rules/jsonschema-draft6-to-2019-09.json +++ b/test/rules/jsonschema-draft6-to-2019-09.json @@ -23,12 +23,22 @@ "name": "draft6 definitions", "schema": { "$schema": "http://json-schema.org/draft-06/schema#", + "properties": { + "foo": { + "$ref": "#/definitions/foo" + } + }, "definitions": { "foo": { "type": "string" } } }, "expected": { "$schema": "https://json-schema.org/draft/2019-09/schema", + "properties": { + "foo": { + "$ref": "#/$defs/foo" + } + }, "$defs": { "foo": { "type": "string" } } diff --git a/test/rules/jsonschema-draft6-to-2020-12.json b/test/rules/jsonschema-draft6-to-2020-12.json index d50f382..65849cd 100644 --- a/test/rules/jsonschema-draft6-to-2020-12.json +++ b/test/rules/jsonschema-draft6-to-2020-12.json @@ -23,12 +23,22 @@ "name": "draft6 definitions", "schema": { "$schema": "http://json-schema.org/draft-06/schema#", + "properties": { + "foo": { + "$ref": "#/definitions/foo" + } + }, "definitions": { "foo": { "type": "string" } } }, "expected": { "$schema": "https://json-schema.org/draft/2020-12/schema", + "properties": { + "foo": { + "$ref": "#/$defs/foo" + } + }, "$defs": { "foo": { "type": "string" } } diff --git a/test/rules/jsonschema-draft7-to-2019-09.json b/test/rules/jsonschema-draft7-to-2019-09.json index d80a8f1..b2e2d48 100644 --- a/test/rules/jsonschema-draft7-to-2019-09.json +++ b/test/rules/jsonschema-draft7-to-2019-09.json @@ -23,12 +23,22 @@ "name": "draft7 definitions", "schema": { "$schema": "http://json-schema.org/draft-07/schema#", + "properties": { + "foo": { + "$ref": "#/definitions/foo" + } + }, "definitions": { "foo": { "type": "string" } } }, "expected": { "$schema": "https://json-schema.org/draft/2019-09/schema", + "properties": { + "foo": { + "$ref": "#/$defs/foo" + } + }, "$defs": { "foo": { "type": "string" } } diff --git a/test/rules/jsonschema-draft7-to-2020-12.json b/test/rules/jsonschema-draft7-to-2020-12.json index 6e84193..ecb926b 100644 --- a/test/rules/jsonschema-draft7-to-2020-12.json +++ b/test/rules/jsonschema-draft7-to-2020-12.json @@ -34,6 +34,31 @@ } } }, + { + "name": "draft7 definitions", + "schema": { + "$schema": "http://json-schema.org/draft-07/schema#", + "properties": { + "foo": { + "$ref": "#/definitions/foo" + } + }, + "definitions": { + "foo": { "type": "string" } + } + }, + "expected": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "properties": { + "foo": { + "$ref": "#/$defs/foo" + } + }, + "$defs": { + "foo": { "type": "string" } + } + } + }, { "name": "true no metaschema", "schema": {},