Inian
Inian

Reputation: 85855

Validate object property on sub condition

I have written the following JSON schema validating target image versions for multiple device components, containing an active version and inactive versions for multiple components, i.e. foo, bar.

How do I prevent e.g. if component name is component2, it contains only active property and inactive property is not needed?

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "config.json",
  "type": "object",
  "title": "Config parameters",
  "required": [
    "Versions"
  ],
  "properties": {
    "Versions": {
      "$id": "#/properties/Versions",
      "type": "array",
      "minItems": 1,
      "additionalItems": false,
      "items": {
        "$id": "#/properties/Versions/items",
        "title": "Version Info",
        "description": "Version information",
        "type": "object",
        "properties": {
          "foo": {
            "$ref": "#/properties/Versions/items/definitions/image-info"
          },
          "bar": {
            "$ref": "#/properties/Versions/items/definitions/image-info"
          }
        },
        "definitions": {
          "image-info": {
            "type": "object",
            "properties": {
              "component1": {
                "$ref": "#/properties/Versions/items/definitions/image-settings"
              },
              "component2": {
                "$ref": "#/properties/Versions/items/definitions/image-settings"
              }
            },
            "additionalProperties": false
          },
          "image-settings": {
            "type": "object",
            "properties": {
              "active": {
                "$ref": "#/properties/Versions/items/definitions/image-version"
              },
              "inactive": {
                "$ref": "#/properties/Versions/items/definitions/image-version"
              }
            },
            "additionalProperties": false
          },
          "image-version": {
            "type": "string",
            "pattern": "^20[0-9]{2}-[01][0-9]-[0-3][0-9]-[0-9]{2}$"
          }
        }
      }
    }
  },
  "additionalProperties": false
}

I would like the below JSON to be considered invalid as component2 contains inactive property

{
  "Versions": [
    {
      "foo": {
        "component1": {
          "active": "2021-11-15-01",
          "inactive": "2021-11-15-00"
        }
      }
    },
    {
      "bar": {
        "component2": {
          "active": "2021-11-15-03",
          "inactive": "2021-11-15-04"
        }
      }
    }
  ]
}

But I would like the below to be considered a valid JSON

{
  "Versions": [
    {
      "foo": {
        "component1": {
          "active": "2021-11-15-01",
          "inactive": "2021-11-15-00"
        }
      }
    },
    {
      "bar": {
        "component2": {
          "active": "2021-11-15-03"
        }
      }
    }
  ]
}

Upvotes: 1

Views: 71

Answers (1)

Relequestual
Relequestual

Reputation: 12355

Assuming you don't want to modify the definition of image-setting, and want to avoid duplication as much as possilbe, you can modify your definition of component2 to dissallow inactive using the not keyword.

As you're using a reference and draft-07 of JSON Schema, you'll need to wrap that reference in an allOf. (If you were using 2019-09 or later, you would not, as $ref can now be used alongside other keywords.)

{
  "allOf": [
    {
      "$ref": "#/properties/Versions/items/definitions/image-settings"
    }, {
      "not": {
        "required": ["inactive"]
      }
    }
  ]
}

You can see it working here: https://jsonschema.dev/s/EIGp1

Upvotes: 1

Related Questions