{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://interchange.stellarview.net/schemas/source.schema.json",
  "title": "A Collector source: who sends, how it is signed, how its items are de-duplicated and classified",
  "type": "object",
  "required": [
    "id",
    "name",
    "kind",
    "signature",
    "dedupe_on",
    "rules"
  ],
  "additionalProperties": false,
  "$defs": {
    "pick": {
      "type": "string",
      "pattern": "^(\\$(\\.[A-Za-z0-9_]+)+|header:[a-z0-9-]+|meta:[a-z0-9_]+|item:(sha256|filename|media_type|bytes|received_at|text_state|pages))$",
      "description": "$.a.b from a JSON body, header:<name>, meta:<multipart field>, or item:<what the Collector knows>."
    },
    "value_spec": {
      "oneOf": [
        {
          "$ref": "#/$defs/pick"
        },
        {
          "type": "object",
          "required": [
            "const"
          ],
          "additionalProperties": false,
          "properties": {
            "const": {}
          }
        },
        {
          "type": "object",
          "required": [
            "from",
            "map"
          ],
          "additionalProperties": false,
          "properties": {
            "from": {
              "$ref": "#/$defs/pick"
            },
            "map": {
              "type": "object"
            }
          }
        }
      ]
    }
  },
  "properties": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9-]*$"
    },
    "name": {
      "type": "string"
    },
    "kind": {
      "enum": [
        "system",
        "vendor_api",
        "file_drop",
        "video",
        "load"
      ]
    },
    "about": {
      "type": "string"
    },
    "shape_basis": {
      "type": "string",
      "description": "Where the payload shape comes from: a published spec, a point list, or illustrative (to confirm with the vendor)."
    },
    "signature": {
      "oneOf": [
        {
          "const": "none_declared"
        },
        {
          "type": "object",
          "required": [
            "alg",
            "header",
            "key_env"
          ],
          "additionalProperties": false,
          "properties": {
            "alg": {
              "const": "hmac-sha256"
            },
            "header": {
              "type": "string",
              "pattern": "^[a-z0-9-]+$"
            },
            "key_env": {
              "type": "string",
              "pattern": "^[A-Z][A-Z0-9_]*$",
              "description": "The environment variable holding the key. Never the key itself."
            }
          }
        }
      ]
    },
    "dedupe_on": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/pick"
      },
      "description": "The values that make two items the same. All must be present, or the item is rejected."
    },
    "rules": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": [
          "id",
          "when",
          "event_type",
          "fields",
          "occurred_at"
        ],
        "additionalProperties": false,
        "properties": {
          "id": {
            "type": "string"
          },
          "when": {
            "type": "object",
            "minProperties": 1,
            "description": "pick -> a value, a list of values, or {prefix}. All must match.",
            "additionalProperties": {
              "oneOf": [
                {
                  "type": [
                    "string",
                    "number",
                    "boolean"
                  ]
                },
                {
                  "type": "array",
                  "items": {
                    "type": [
                      "string",
                      "number",
                      "boolean"
                    ]
                  }
                },
                {
                  "type": "object",
                  "required": [
                    "prefix"
                  ],
                  "additionalProperties": false,
                  "properties": {
                    "prefix": {
                      "type": "string"
                    }
                  }
                },
                {
                  "type": "object",
                  "required": [
                    "suffix"
                  ],
                  "additionalProperties": false,
                  "properties": {
                    "suffix": {
                      "type": "string",
                      "description": "Matched without case."
                    }
                  }
                }
              ]
            }
          },
          "event_type": {
            "type": "string",
            "description": "A catalogue id or alias."
          },
          "device": {
            "$ref": "#/$defs/pick",
            "description": "The device name the item carries. The Collector finds the sensor asset bound to this source and device."
          },
          "fields": {
            "type": "object",
            "additionalProperties": {
              "$ref": "#/$defs/value_spec"
            }
          },
          "load": {
            "type": "object",
            "required": [
              "adapter"
            ],
            "additionalProperties": false,
            "description": "Set on a load source: the file is also run through an ETL adapter to a template. If the load stops, the item is rejected with the rows and columns.",
            "properties": {
              "adapter": {
                "enum": [
                  "template",
                  "i3",
                  "allen-bradley",
                  "bms"
                ]
              },
              "template": {
                "type": "string"
              },
              "template_from": {
                "$ref": "#/$defs/pick"
              },
              "mapping": {
                "type": "string",
                "description": "A file in adapters/etl/mappings."
              },
              "mapping_by": {
                "type": "object",
                "required": [
                  "pick",
                  "files"
                ],
                "additionalProperties": false,
                "properties": {
                  "pick": {
                    "$ref": "#/$defs/pick"
                  },
                  "files": {
                    "type": "object",
                    "additionalProperties": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "occurred_at": {
            "oneOf": [
              {
                "$ref": "#/$defs/pick"
              },
              {
                "const": "received"
              }
            ],
            "description": "Where the event time comes from; received means the sender gives none and the event says so."
          },
          "unit_from": {
            "$ref": "#/$defs/pick",
            "description": "Where the sender states the unit of fields.value. It must be the catalogue unit (id or UCUM) of the type's value field."
          }
        }
      }
    }
  }
}