diff --git a/bindings/node/index.js b/bindings/node/index.js index 419f5c4..a6030cf 100644 --- a/bindings/node/index.js +++ b/bindings/node/index.js @@ -41,8 +41,8 @@ async function transformer (root, path, ruleset, trails, originalSchema, from) { omit: (object, keys) => { return _.omit(object, _.castArray(keys)) }, - replace: (value, string, replacement) => { - return value.replaceAll(string, replacement) + replace: (value, regex, replacement) => { + return value.replace(new RegExp(regex, 'g'), replacement) } }) diff --git a/rules/jsonschema-2019-09-to-2020-12.json b/rules/jsonschema-2019-09-to-2020-12.json index 8fac8e1..bdaeb79 100644 --- a/rules/jsonschema-2019-09-to-2020-12.json +++ b/rules/jsonschema-2019-09-to-2020-12.json @@ -47,6 +47,32 @@ ] } }, + { + "vocabulary": "https://json-schema.org/draft/2019-09/vocab/validation", + "condition": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://github.com/sourcemeta/alterschema/rules/jsonschema-2019-09-to-2020-12/open-tuple-in-ref", + "$comment": "https://json-schema.org/draft/2020-12/release-notes.html", + "type": "object", + "required": [ "$ref" ], + "properties": { + "$ref": { + "type": "string", + "pattern": "/items/\\d+" + } + } + }, + "transform": { + "$merge": [ + { "$eval": "omit(schema, '$ref')" }, + { + "$$ref": { + "$eval": "replace(schema['$ref'], '/items/(\\d+)', '/prefixItems/$1')" + } + } + ] + } + }, { "vocabulary": "https://json-schema.org/draft/2019-09/vocab/validation", "condition": { diff --git a/rules/jsonschema-draft7-to-2019-09.json b/rules/jsonschema-draft7-to-2019-09.json index abe60c8..6a0621f 100644 --- a/rules/jsonschema-draft7-to-2019-09.json +++ b/rules/jsonschema-draft7-to-2019-09.json @@ -89,7 +89,7 @@ { "$eval": "omit(schema, '$ref')" }, { "$$ref": { - "$eval": "replace(schema['$ref'], 'definitions', '$defs')" + "$eval": "replace(schema['$ref'], '/definitions/', '/$defs/')" } } ] diff --git a/test/rules/jsonschema-2019-09-to-2020-12.json b/test/rules/jsonschema-2019-09-to-2020-12.json index cfeb13c..c1d774f 100644 --- a/test/rules/jsonschema-2019-09-to-2020-12.json +++ b/test/rules/jsonschema-2019-09-to-2020-12.json @@ -167,5 +167,39 @@ "foo": { "$dynamicRef": "#" } } } + }, + { + "name": "items to prefixItems within $ref", + "schema": { + "$schema": "https://json-schema.org/draft/2019-09/schema", + "items": [ + { "type": "integer" }, + { "$ref": "#/items/0" } + ] + }, + "expected": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "prefixItems": [ + { "type": "integer" }, + { "$ref": "#/prefixItems/0" } + ] + } + }, + { + "name": "items not to prefixItems within $ref", + "schema": { + "$schema": "https://json-schema.org/draft/2019-09/schema", + "items": { "type": "integer" }, + "properties": { + "foo": { "$ref": "#/items" } + } + }, + "expected": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "items": { "type": "integer" }, + "properties": { + "foo": { "$ref": "#/items" } + } + } } ] diff --git a/test/rules/jsonschema-draft4-to-2020-12.json b/test/rules/jsonschema-draft4-to-2020-12.json index 2494b0c..a2af82a 100644 --- a/test/rules/jsonschema-draft4-to-2020-12.json +++ b/test/rules/jsonschema-draft4-to-2020-12.json @@ -225,5 +225,22 @@ { "$ref": "#/prefixItems/0" } ] } + }, + { + "name": "items not to prefixItems within $ref", + "schema": { + "$schema": "http://json-schema.org/draft-04/schema#", + "items": { "type": "integer" }, + "properties": { + "foo": { "$ref": "#/items" } + } + }, + "expected": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "items": { "type": "integer" }, + "properties": { + "foo": { "$ref": "#/items" } + } + } } ] diff --git a/test/rules/jsonschema-draft6-to-2020-12.json b/test/rules/jsonschema-draft6-to-2020-12.json index 98bb24c..6226beb 100644 --- a/test/rules/jsonschema-draft6-to-2020-12.json +++ b/test/rules/jsonschema-draft6-to-2020-12.json @@ -149,5 +149,39 @@ "foo": false } } + }, + { + "name": "items to prefixItems within $ref", + "schema": { + "$schema": "http://json-schema.org/draft-06/schema#", + "items": [ + { "type": "integer" }, + { "$ref": "#/items/0" } + ] + }, + "expected": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "prefixItems": [ + { "type": "integer" }, + { "$ref": "#/prefixItems/0" } + ] + } + }, + { + "name": "items not to prefixItems within $ref", + "schema": { + "$schema": "http://json-schema.org/draft-06/schema#", + "items": { "type": "integer" }, + "properties": { + "foo": { "$ref": "#/items" } + } + }, + "expected": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "items": { "type": "integer" }, + "properties": { + "foo": { "$ref": "#/items" } + } + } } ] diff --git a/test/rules/jsonschema-draft7-to-2020-12.json b/test/rules/jsonschema-draft7-to-2020-12.json index 8f92e9b..85e6206 100644 --- a/test/rules/jsonschema-draft7-to-2020-12.json +++ b/test/rules/jsonschema-draft7-to-2020-12.json @@ -183,5 +183,39 @@ } } } + }, + { + "name": "items to prefixItems within $ref", + "schema": { + "$schema": "http://json-schema.org/draft-07/schema#", + "items": [ + { "type": "integer" }, + { "$ref": "#/items/0" } + ] + }, + "expected": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "prefixItems": [ + { "type": "integer" }, + { "$ref": "#/prefixItems/0" } + ] + } + }, + { + "name": "items not to prefixItems within $ref", + "schema": { + "$schema": "http://json-schema.org/draft-07/schema#", + "items": { "type": "integer" }, + "properties": { + "foo": { "$ref": "#/items" } + } + }, + "expected": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "items": { "type": "integer" }, + "properties": { + "foo": { "$ref": "#/items" } + } + } } ]