{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://duellab.org/corrections.schema.json",
  "title": "DuelLab corrections log v2",
  "type": "object",
  "additionalProperties": false,
  "required": ["schema_version", "corrections"],
  "properties": {
    "schema_version": { "const": 2 },
    "corrections": {
      "type": "array",
      "items": { "$ref": "#/definitions/correction" }
    }
  },
  "definitions": {
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["results_cut_id", "canonical_results_sha256"],
      "properties": {
        "results_cut_id": { "type": "string", "pattern": "^[0-9]{8}_[0-9]{6}$" },
        "canonical_results_sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "value": { "type": "integer", "minimum": 0 }
      }
    },
    "base": {
      "type": "object",
      "required": [
        "correction_id",
        "correction_type",
        "effective_at",
        "source_cut_generated_at",
        "field",
        "before",
        "after",
        "reason"
      ],
      "properties": {
        "correction_id": { "type": "string", "pattern": "^[a-z0-9][a-z0-9._-]+$" },
        "correction_type": { "enum": ["metric", "release_coherence"] },
        "effective_at": { "type": "string", "format": "date-time" },
        "source_cut_generated_at": { "type": "string", "format": "date-time" },
        "field": { "type": "string", "minLength": 1 },
        "before": { "$ref": "#/definitions/evidence" },
        "after": { "$ref": "#/definitions/evidence" },
        "reason": { "type": "string", "minLength": 1 }
      }
    },
    "correction": {
      "allOf": [
        { "$ref": "#/definitions/base" },
        {
          "oneOf": [
            {
              "properties": {
                "correction_type": { "const": "metric" },
                "before": { "required": ["value"] },
                "after": { "required": ["value"] }
              }
            },
            {
              "required": [
                "affected_public_release_ids",
                "confirmed_findings",
                "unreproduced_allegations",
                "preservation_statement"
              ],
              "properties": {
                "correction_type": { "const": "release_coherence" },
                "affected_public_release_ids": {
                  "type": "array",
                  "minItems": 1,
                  "uniqueItems": true,
                  "items": { "type": "string", "pattern": "^gb2-[0-9]{8}-[a-f0-9]{12}$" }
                },
                "confirmed_findings": {
                  "type": "array",
                  "minItems": 1,
                  "items": { "type": "string", "minLength": 1 }
                },
                "unreproduced_allegations": {
                  "type": "array",
                  "minItems": 1,
                  "items": { "type": "string", "minLength": 1 }
                },
                "preservation_statement": { "type": "string", "minLength": 1 }
              }
            }
          ]
        }
      ],
      "unevaluatedProperties": false
    }
  }
}
