{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://stac-extensions.github.io/ml-model/v1.0.0/schema.json#",
  "title": "ML Model Extension",
  "description": "STAC ML Model Extension for STAC Items.",
  "oneOf": [
    {
      "$comment": "This is the schema for STAC Items. Remove this object if this extension only applies to Collections.",
      "allOf": [
        {
          "$ref": "#/definitions/stac_extensions"
        },
        {
          "type": "object",
          "required": [
            "type",
            "properties",
            "assets"
          ],
          "properties": {
            "type": {
              "const": "Feature"
            },
            "properties": {
              "allOf": [
                {
                  "$comment": "Require fields here for Item Properties.",
                  "required": [
                    "ml-model:type",
                    "ml-model:learning_approach",
                    "ml-model:prediction_type",
                    "ml-model:architecture"
                  ]
                },
                {
                  "$ref": "#/definitions/common_fields"
                }
              ]
            },
            "assets": {
              "$comment": "This validates the fields in Item Assets, but does not require them.",
              "type": "object",
              "additionalProperties": {
                "$ref": "#/definitions/common_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 validates the fields in Collection Assets, but does not require them.",
          "required": [
            "assets"
          ],
          "properties": {
            "assets": {
              "type": "object",
              "not": {
                "additionalProperties": {
                  "not": {
                    "allOf": [
                      {
                        "$ref": "#/definitions/require_any_field"
                      },
                      {
                        "$ref": "#/definitions/common_fields"
                      }
                    ]
                  }
                }
              }
            }
          }
        },
        {
          "$comment": "This is the schema for the fields in Item Asset Definitions. It doesn't require any fields.",
          "required": [
            "item_assets"
          ],
          "properties": {
            "item_assets": {
              "type": "object",
              "not": {
                "additionalProperties": {
                  "not": {
                    "allOf": [
                      {
                        "$ref": "#/definitions/require_any_field"
                      },
                      {
                        "$ref": "#/definitions/common_fields"
                      }
                    ]
                  }
                }
              }
            }
          }
        },
        {
          "$comment": "This is the schema for the fields in Summaries. By default, only checks the existance 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://stac-extensions.github.io/ml-model/v1.0.0/schema.json"
          }
        }
      }
    },
    "require_any_field": {
      "$comment": "Please list all fields here so that we can force the existance of one of them in other parts of the schemas.",
      "anyOf": [
        {
          "required": [
            "ml-model:learning_approach"
          ]
        },
        {
          "required": [
            "ml-model:prediction_type"
          ]
        },
        {
          "required": [
            "ml-model:architecture"
          ]
        }
      ]
    },
    "common_fields": {
      "$comment": "Add your new Item fields here. Don't require them here, do that above in the corresponding schema.",
      "type": "object",
      "properties": {
        "ml-model:type": {
          "const": "ml-model"
        },
        "ml-model:learning_approach": {
          "type": "string"
        },
        "ml-model:prediction_type": {
          "type": "string"
        },
        "ml-model:architecture": {
          "type": "string"
        },
        "ml-model:training-os": {
          "type": "string"
        },
        "ml-model:training-processor-type": {
          "type": "string",
          "enum": ["cpu", "gpu"]
        }
      },
      "patternProperties": {
        "^(?!ml-model:)": {}
      },
      "additionalProperties": false
    }
  }
}