Skip to content

Commit

Permalink
Transform items to prefixItems in $ref
Browse files Browse the repository at this point in the history
See: #43
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Aug 24, 2022
1 parent 0813647 commit 01c50ac
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 3 deletions.
4 changes: 2 additions & 2 deletions bindings/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})

Expand Down
26 changes: 26 additions & 0 deletions rules/jsonschema-2019-09-to-2020-12.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion rules/jsonschema-draft7-to-2019-09.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
{ "$eval": "omit(schema, '$ref')" },
{
"$$ref": {
"$eval": "replace(schema['$ref'], 'definitions', '$defs')"
"$eval": "replace(schema['$ref'], '/definitions/', '/$defs/')"
}
}
]
Expand Down
34 changes: 34 additions & 0 deletions test/rules/jsonschema-2019-09-to-2020-12.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
}
}
]
17 changes: 17 additions & 0 deletions test/rules/jsonschema-draft4-to-2020-12.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
}
}
]
34 changes: 34 additions & 0 deletions test/rules/jsonschema-draft6-to-2020-12.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
}
}
]
34 changes: 34 additions & 0 deletions test/rules/jsonschema-draft7-to-2020-12.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
}
}
]

0 comments on commit 01c50ac

Please sign in to comment.