Skip to content

Commit

Permalink
Upgrade "definitions" to "$defs" in "$ref" JSON Pointers (#45)
Browse files Browse the repository at this point in the history
While we currently upgrade "definitions" to "$defs" in the JSON Schema
definition, pointers going through "definitions" are currently left
intact.

See: #43
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Aug 10, 2022
1 parent d8e2d70 commit c3d5f2c
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bindings/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})

Expand Down
25 changes: 25 additions & 0 deletions rules/jsonschema-draft7-to-2019-09.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
10 changes: 10 additions & 0 deletions test/rules/jsonschema-draft4-to-2019-09.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
Expand Down
10 changes: 10 additions & 0 deletions test/rules/jsonschema-draft4-to-2020-12.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
Expand Down
10 changes: 10 additions & 0 deletions test/rules/jsonschema-draft6-to-2019-09.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
Expand Down
10 changes: 10 additions & 0 deletions test/rules/jsonschema-draft6-to-2020-12.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
Expand Down
10 changes: 10 additions & 0 deletions test/rules/jsonschema-draft7-to-2019-09.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
Expand Down
25 changes: 25 additions & 0 deletions test/rules/jsonschema-draft7-to-2020-12.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down

0 comments on commit c3d5f2c

Please sign in to comment.