{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://pdssp.github.io/STAC-Validity-Extension/v1.0.0/schema.json#",
  "title": "Validity Extension",
  "description": "STAC Validity Extension for STAC Items and STAC Collections.",
  "oneOf": [
    {
      "$comment": "This is the schema for STAC Items.",
      "allOf": [
        {
          "$ref": "#/definitions/stac_extensions"
        },
        {
          "type": "object",
          "required": [
            "type",
            "properties"
          ],
          "properties": {
            "type": {
              "const": "Feature"
            },
            "properties": {
              "allOf": [
                {
                  "$ref": "#/definitions/require_any_field"
                },
                {
                  "$ref": "#/definitions/fields"
                }
              ]
            }
          }
        }
      ]
    },
    {
      "$comment": "This is the schema for STAC Collections.",
      "type": "object",
      "allOf": [
        {
          "required": [
            "type"
          ],
          "properties": {
            "type": {
              "const": "Collection"
            }
          }
        },
        {
          "$ref": "#/definitions/stac_extensions"
        }
      ],
      "anyOf": [
        {
          "$comment": "This is the schema for the top-level fields in a Collection.",
          "allOf": [
            {
              "$ref": "#/definitions/require_any_field"
            },
            {
              "$ref": "#/definitions/fields"
            }
          ]
        },
        {
          "$comment": "This is the schema for the fields in Summaries. By default, only checks the existence of the properties, but not the schema of the summaries.",
          "required": [
            "summaries"
          ],
          "properties": {
            "summaries": {
              "$ref": "#/definitions/require_any_field"
            }
          }
        }
      ]
    }
  ],
  "definitions": {
    "stac_extensions": {
      "type": "object",
      "required": [
        "stac_extensions"
      ],
      "properties": {
        "stac_extensions": {
          "type": "array",
          "contains": {
            "const": "https://pdssp.github.io/STAC-Validity-Extension/v1.0.0/schema.json"
          }
        }
      }
    },
    "require_any_field": {
      "$comment": "At least one of the validity fields must be present for this extension to apply.",
      "anyOf": [
        {
          "required": [
            "validity:start_datetime"
          ]
        },
        {
          "required": [
            "validity:end_datetime"
          ]
        }
      ]
    },
    "fields": {
      "type": "object",
      "properties": {
        "validity:start_datetime": {
          "type": "string",
          "format": "date-time"
        },
        "validity:end_datetime": {
          "anyOf": [
            {
              "type": "string",
              "format": "date-time"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "patternProperties": {
        "^(?!validity:)": {}
      },
      "additionalProperties": false
    }
  }
}
