skip to Main Content

I have written a JSON schema file for my Kubernetes Helm values files.

The schema defined some 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"
                }
            }
        }
    }
}

2

Answers


  1. If you want to delete all "required" properties at any level, the following expression should work:

    del(..|.required?)
    

    ? is the Optional Object Identifier-Index which will suppress errors.

    Sample output with your given input:

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "properties": {
        "blackbox": {
          "type": "object",
          "properties": {
            "enabled": {
              "type": "boolean"
            }
          }
        }
      }
    }
    
    Login or Signup to reply.
  2. 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": {}
          }
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search