Skip to content

Commit

Permalink
Using pure JSON-e operators instead of replace and omit functions (
Browse files Browse the repository at this point in the history
…#184)

Signed-off-by: Suprith KG <suprith7kg@gmail.com>
  • Loading branch information
Era-cell committed Mar 18, 2024
1 parent 1df369d commit 2e272b1
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 36 deletions.
8 changes: 0 additions & 8 deletions bindings/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ async function transformer (root, path, ruleset, trails, originalSchema, from) {
}

return false
},

// TODO: Use standard JSON-e operators instead
omit: (object, keys) => {
return _.omit(object, _.castArray(keys))
},
replace: (value, regex, replacement) => {
return value.replace(new RegExp(regex, 'g'), replacement)
}
})

Expand Down
24 changes: 22 additions & 2 deletions rules/jsonschema-2019-09-to-2019-09.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@
}
},
"transform": {
"$eval": "omit(schema, 'dependentSchemas')"
"$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='dependentSchemas'",
"then": {
"$eval": "v"
}
}
}
}
},
{
Expand All @@ -32,7 +42,17 @@
}
},
"transform": {
"$eval": "omit(schema, 'dependentRequired')"
"$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='dependentRequired'",
"then": {
"$eval": "v"
}
}
}
}
}
]
Expand Down
103 changes: 93 additions & 10 deletions rules/jsonschema-2019-09-to-2020-12.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, 'items')" },
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='items'",
"then": {
"$eval": "v"
}
}
} },
{
"prefixItems": {
"$eval": "schema.items"
Expand All @@ -64,10 +74,23 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, '$ref')" },
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='$ref'",
"then": {
"$eval": "v"
}
}
} },
{
"$$ref": {
"$eval": "replace(schema['$ref'], '/items/(\\d+)', '/prefixItems/$1')"
"$if": "split(schema['$ref'], 'items')[1][1] in '1234567890'",
"then":{
"$eval": "join(split(schema['$ref'], 'items'), 'prefixItems')"
}
}
}
]
Expand All @@ -90,10 +113,20 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, '$ref')" },
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='$ref'",
"then": {
"$eval": "v"
}
}
} },
{
"$$ref": {
"$eval": "replace(schema['$ref'], '/additionalItems', '/items')"
"$eval": "join(split(schema['$ref'], 'additionalItems'), 'items')"
}
}
]
Expand All @@ -116,7 +149,17 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, 'additionalItems')" },
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='additionalItems'",
"then": {
"$eval": "v"
}
}
} },
{
"items": {
"$eval": "schema.additionalItems"
Expand Down Expand Up @@ -167,7 +210,17 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, 'enum')" },
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='enum'",
"then": {
"$eval": "v"
}
}
} },
{
"const": {
"$eval": "schema.enum[0]"
Expand All @@ -190,7 +243,17 @@
}
},
"transform": {
"$eval": "omit(schema, '$recursiveAnchor')"
"$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='$recursiveAnchor'",
"then": {
"$eval": "v"
}
}
}
}
},
{
Expand All @@ -208,7 +271,17 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, '$recursiveAnchor')" },
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='$recursiveAnchor'",
"then": {
"$eval": "v"
}
}
} },
{
"$$dynamicAnchor": {
"$eval": "'anchor-' + jsonHash(original)"
Expand All @@ -232,7 +305,17 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, '$recursiveRef')" },
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='$recursiveRef'",
"then": {
"$eval": "v"
}
}
} },
{
"$$dynamicRef": {
"$if": "hasContext('$recursiveAnchor', true)",
Expand Down
36 changes: 33 additions & 3 deletions rules/jsonschema-draft3-to-draft4.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@
}
},
"transform": {
"$eval": "omit(schema, 'type')"
"$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='type'",
"then": {
"$eval": "v"
}
}
}
}
},
{
Expand All @@ -55,7 +65,17 @@
}
},
"transform": {
"$eval": "omit(schema, 'type')"
"$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='type'",
"then": {
"$eval": "v"
}
}
}
}
},
{
Expand All @@ -73,7 +93,17 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, 'divisibleBy')" },
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='divisibleBy'",
"then": {
"$eval": "v"
}
}
} },
{
"multipleOf": {
"$eval": "schema.divisibleBy"
Expand Down
72 changes: 66 additions & 6 deletions rules/jsonschema-draft4-to-draft6.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,17 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, 'id')" },
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='id'",
"then": {
"$eval": "v"
}
}
} },
{
"$$id": {
"$eval": "schema.id"
Expand All @@ -63,7 +73,17 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, 'minimum')" },
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='minimum'",
"then": {
"$eval": "v"
}
}
} },
{
"exclusiveMinimum": {
"$eval": "schema.minimum"
Expand All @@ -90,7 +110,17 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, 'maximum')" },
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='maximum'",
"then": {
"$eval": "v"
}
}
} },
{
"exclusiveMaximum": {
"$eval": "schema.maximum"
Expand All @@ -117,7 +147,17 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, 'exclusiveMinimum')" }
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='exclusiveMinimum'",
"then": {
"$eval": "v"
}
}
} }
]
}
},
Expand All @@ -139,7 +179,17 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, 'exclusiveMaximum')" }
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='exclusiveMaximum'",
"then": {
"$eval": "v"
}
}
} }
]
}
},
Expand Down Expand Up @@ -182,7 +232,17 @@
},
"transform": {
"$merge": [
{ "$eval": "omit(schema, 'enum')" },
{ "$map": {
"$eval": "schema"
},
"each(v,k)": {
"${k}": {
"$if": "k!='enum'",
"then": {
"$eval": "v"
}
}
} },
{
"const": {
"$eval": "schema.enum[0]"
Expand Down

0 comments on commit 2e272b1

Please sign in to comment.