{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://evidenza.ru/docs/jsonl-v2/schema.json",
  "title": "evidenza.kr-status-events/v2: structural input event schema",
  "description": "One event per JSONL line; all 15 fields required. Schema validation alone is not sufficient: use the local Evidenza viewer for UTF-8, duplicate keys, JSONL byte/line/UTF-16 limits, unique IDs, exact code_version equality, real UTC dates and chronological ordering. This schema does not verify provenance, source authority, freshness, completeness or hashes.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "event_id",
    "event_type",
    "code_version",
    "code",
    "version",
    "status",
    "previous_status",
    "revision_fingerprint",
    "previous_revision_fingerprint",
    "source_date",
    "source_date_basis",
    "observed_at",
    "previous_observed_at",
    "source",
    "snapshot_at"
  ],
  "properties": {
    "event_id": {
      "$ref": "#/$defs/text",
      "pattern": "^ev-[0-9a-f]{24}$"
    },
    "event_type": {
      "enum": [
        "new_edition",
        "status_change",
        "metadata_change",
        "record_missing"
      ]
    },
    "code_version": {
      "$ref": "#/$defs/text",
      "pattern": "^(0|[1-9][0-9]*)_(0|[1-9][0-9]*)$",
      "description": "Must additionally equal the JavaScript string code + \"_\" + version."
    },
    "code": {
      "$ref": "#/$defs/safeInteger"
    },
    "version": {
      "$ref": "#/$defs/safeInteger"
    },
    "status": {
      "enum": [
        "current",
        "withdrawn",
        null
      ]
    },
    "previous_status": {
      "enum": [
        "current",
        "withdrawn",
        null
      ]
    },
    "revision_fingerprint": {
      "anyOf": [
        {
          "$ref": "#/$defs/fingerprint"
        },
        {
          "type": "null"
        }
      ]
    },
    "previous_revision_fingerprint": {
      "anyOf": [
        {
          "$ref": "#/$defs/fingerprint"
        },
        {
          "type": "null"
        }
      ]
    },
    "source_date": {
      "anyOf": [
        {
          "$ref": "#/$defs/text"
        },
        {
          "type": "null"
        }
      ]
    },
    "source_date_basis": {
      "$ref": "#/$defs/text"
    },
    "observed_at": {
      "$ref": "#/$defs/utc"
    },
    "previous_observed_at": {
      "$ref": "#/$defs/utc"
    },
    "source": {
      "const": "по данным рубрикатора клинических рекомендаций Минздрава России"
    },
    "snapshot_at": {
      "$ref": "#/$defs/utc"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "event_type": {
            "const": "record_missing"
          }
        }
      },
      "then": {
        "properties": {
          "status": {
            "type": "null"
          },
          "revision_fingerprint": {
            "type": "null"
          }
        }
      },
      "else": {
        "properties": {
          "status": {
            "enum": [
              "current",
              "withdrawn"
            ]
          },
          "revision_fingerprint": {
            "$ref": "#/$defs/fingerprint"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "event_type": {
            "const": "new_edition"
          }
        }
      },
      "then": {
        "properties": {
          "previous_status": {
            "type": "null"
          },
          "previous_revision_fingerprint": {
            "type": "null"
          }
        }
      },
      "else": {
        "properties": {
          "previous_revision_fingerprint": {
            "$ref": "#/$defs/fingerprint"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "event_type": {
            "const": "status_change"
          }
        }
      },
      "then": {
        "oneOf": [
          {
            "properties": {
              "status": {
                "const": "current"
              },
              "previous_status": {
                "const": "withdrawn"
              }
            }
          },
          {
            "properties": {
              "status": {
                "const": "withdrawn"
              },
              "previous_status": {
                "const": "current"
              }
            }
          }
        ]
      }
    },
    {
      "if": {
        "properties": {
          "event_type": {
            "const": "metadata_change"
          }
        }
      },
      "then": {
        "oneOf": [
          {
            "properties": {
              "status": {
                "const": "current"
              },
              "previous_status": {
                "const": "current"
              }
            }
          },
          {
            "properties": {
              "status": {
                "const": "withdrawn"
              },
              "previous_status": {
                "const": "withdrawn"
              }
            }
          }
        ]
      }
    }
  ],
  "$defs": {
    "text": {
      "type": "string",
      "maxLength": 512,
      "pattern": "^[^\\u0000-\\u0008\\u000B\\u000C\\u000E-\\u001F\\u007F]*$",
      "$comment": "JSON Schema counts Unicode code points; viewer additionally enforces at most 512 UTF-16 code units."
    },
    "safeInteger": {
      "type": "integer",
      "minimum": 0,
      "maximum": 9007199254740991
    },
    "fingerprint": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "utc": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$",
      "format": "date-time",
      "description": "Real UTC date with second precision; previous_observed_at <= observed_at <= snapshot_at, enforced additionally by viewer."
    }
  }
}
