Issue
I have written a JSON schema file for my Kubernetes Helm values files.
The schema defined some href="https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.5.3" rel="nofollow noreferrer">required properties. I would like to publish modified schema without all required properties.
Background: With helm, it's possible to define a subset of the values files. IntelliJ highlights some error on such subset YAML files, since it misses some "required keys". Thats fine in this context, since the YAML files which holds a subset of the configuration will be merged with the base configuration.
Example JSON
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"blackbox": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
}
},
"required": ["enabled"]
}
},
"required": ["blackbox"]
}
The schema itself is repeatable, the "required" key can also be found in a 9th or 10th nested level. That's why I need a solution which enables to do it recursively.
The step should be executed in GitHub Action, however any shell based solution is fine, too.
I was trying to play with jq, but I don't find the way here.
Example Output:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"blackbox": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
}
}
}
}
}
Solution
Recursively remove one key
jq 'walk(if type == "object" then del(.required) else . end)'
# same as
jq 'del(..|.required?)'
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"blackbox": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
}
}
}
}
}
Recursively remove multiple keys
jq 'walk(if type == "object" then del(.type,.required) else . end)'
# same as
jq 'del(..|.type?,.required?)'
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"blackbox": {
"properties": {
"enabled": {}
}
}
}
}
Answered By - nntrn Answer Checked By - Willingham (WPSolving Volunteer)