{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://law.arxo.io/schema/diagnostic.schema.json",
  "title": "Arxo diagnostic 0.1",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "code": {
      "type": "string",
      "pattern": "^LDC-[EWIH][0-9]{4}$"
    },
    "severity": {
      "enum": [
        "error",
        "warning",
        "information",
        "hint"
      ]
    },
    "message": {
      "type": "string",
      "minLength": 1
    },
    "primarySpan": {
      "$ref": "#/$defs/SourceSpan"
    },
    "primaryLocation": {
      "$ref": "#/$defs/DiagnosticLocation"
    },
    "relatedSpans": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/RelatedSpan"
      }
    },
    "relatedLocations": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/RelatedLocation"
      }
    },
    "legalSourceAnchors": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "uniqueItems": true
    },
    "explanation": {
      "type": "string"
    },
    "suggestedFixes": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/SuggestedFix"
      }
    },
    "machineData": {
      "type": "object"
    }
  },
  "required": [
    "code",
    "severity",
    "message"
  ],
  "allOf": [
    {
      "if": {
        "not": {
          "required": [
            "primarySpan"
          ]
        }
      },
      "then": {
        "required": [
          "primaryLocation"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "code": {
            "const": "LDC-E5301"
          }
        },
        "required": [
          "code"
        ]
      },
      "then": {
        "properties": {
          "severity": {
            "const": "error"
          }
        },
        "required": [
          "severity"
        ]
      }
    }
  ],
  "$defs": {
    "Position": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "line": {
          "type": "integer",
          "minimum": 0
        },
        "character": {
          "type": "integer",
          "minimum": 0
        },
        "offset": {
          "type": "integer",
          "minimum": 0
        }
      },
      "required": [
        "line",
        "character"
      ]
    },
    "SourceSpan": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "uri": {
          "type": "string"
        },
        "start": {
          "$ref": "#/$defs/Position"
        },
        "end": {
          "$ref": "#/$defs/Position"
        }
      },
      "required": [
        "uri",
        "start",
        "end"
      ]
    },
    "RelatedSpan": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "span": {
          "$ref": "#/$defs/SourceSpan"
        },
        "message": {
          "type": "string"
        }
      },
      "required": [
        "span",
        "message"
      ]
    },
    "TableCellLocation": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": {
          "const": "table_cell"
        },
        "uri": {
          "type": "string"
        },
        "table": {
          "type": "string"
        },
        "row": {
          "type": "string"
        },
        "column": {
          "type": "string"
        },
        "jsonPointer": {
          "type": "string"
        },
        "sheet": {
          "type": "string"
        },
        "cell": {
          "type": "string"
        },
        "xpath": {
          "type": "string"
        }
      },
      "required": [
        "kind",
        "uri",
        "table",
        "row",
        "column"
      ]
    },
    "ParameterRowLocation": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": {
          "const": "parameter_row"
        },
        "uri": {
          "type": "string"
        },
        "dataset": {
          "type": "string"
        },
        "row": {
          "type": "string"
        },
        "jsonPointer": {
          "type": "string"
        }
      },
      "required": [
        "kind",
        "uri",
        "dataset",
        "row",
        "jsonPointer"
      ]
    },
    "JsonPointerLocation": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": {
          "const": "json_pointer"
        },
        "uri": {
          "type": "string"
        },
        "jsonPointer": {
          "type": "string",
          "pattern": "^(|/.*)$"
        }
      },
      "required": [
        "kind",
        "uri",
        "jsonPointer"
      ]
    },
    "DiagnosticLocation": {
      "oneOf": [
        {
          "$ref": "#/$defs/TableCellLocation"
        },
        {
          "$ref": "#/$defs/ParameterRowLocation"
        },
        {
          "$ref": "#/$defs/JsonPointerLocation"
        }
      ]
    },
    "RelatedLocation": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "location": {
          "$ref": "#/$defs/DiagnosticLocation"
        },
        "message": {
          "type": "string"
        }
      },
      "required": [
        "location",
        "message"
      ]
    },
    "TextEdit": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "span": {
          "$ref": "#/$defs/SourceSpan"
        },
        "replacement": {
          "type": "string"
        }
      },
      "required": [
        "span",
        "replacement"
      ]
    },
    "SuggestedFix": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "title": {
          "type": "string"
        },
        "applicability": {
          "enum": [
            "always",
            "maybe",
            "manual_review"
          ]
        },
        "edits": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/TextEdit"
          },
          "minItems": 1
        }
      },
      "required": [
        "title",
        "applicability",
        "edits"
      ]
    }
  }
}
