{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://grids365.dev/corpus/function-cases.schema.json",
  "title": "Grid structured upstream function case",
  "type": "object",
  "required": [
    "schemaVersion",
    "id",
    "recordType",
    "language",
    "languageVersion",
    "split",
    "case",
    "metadata"
  ],
  "properties": {
    "schemaVersion": { "const": 1 },
    "id": {
      "type": "string",
      "pattern": "^grid-[0-9]+\\.[0-9]+\\.[0-9]+:function-case:[A-Za-z0-9_.-]+$"
    },
    "recordType": { "const": "function-case" },
    "language": { "const": "Grid" },
    "languageVersion": {
      "type": "string",
      "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"
    },
    "split": { "const": "train" },
    "case": {
      "oneOf": [
        { "$ref": "#/$defs/functionCase" },
        { "$ref": "#/$defs/formulaCase" },
        { "$ref": "#/$defs/sourceCase" }
      ]
    },
    "metadata": { "$ref": "#/$defs/metadata" }
  },
  "additionalProperties": false,
  "$defs": {
    "hex64": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "gitOid": {
      "type": "string",
      "pattern": "^[0-9a-f]{40}$"
    },
    "caseName": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_.-]+$",
      "minLength": 1,
      "maxLength": 128
    },
    "rawValue": {
      "oneOf": [
        { "type": "null" },
        { "type": "boolean" },
        { "type": "number" },
        { "type": "string" },
        {
          "type": "array",
          "items": { "$ref": "#/$defs/rawValue" }
        },
        {
          "type": "object",
          "additionalProperties": { "$ref": "#/$defs/rawValue" }
        }
      ]
    },
    "typedValue": {
      "oneOf": [
        {
          "type": "object",
          "required": ["kind"],
          "properties": { "kind": { "const": "blank" } },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["kind", "value"],
          "properties": {
            "kind": { "const": "boolean" },
            "value": { "type": "boolean" }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["kind", "value"],
          "properties": {
            "kind": { "const": "number" },
            "value": { "type": "number" }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["kind", "value"],
          "properties": {
            "kind": { "const": "string" },
            "value": { "type": "string" }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["kind", "value"],
          "properties": {
            "kind": { "const": "date" },
            "value": { "type": "string", "minLength": 1 }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["kind", "displayCode", "normalizedCode"],
          "properties": {
            "kind": { "const": "error" },
            "displayCode": { "type": "string", "minLength": 1 },
            "normalizedCode": { "type": "string", "minLength": 1 }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["kind", "rows"],
          "properties": {
            "kind": { "const": "array" },
            "rows": {
              "type": "array",
              "items": {
                "type": "array",
                "items": { "$ref": "#/$defs/typedValue" }
              }
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["kind", "value"],
          "properties": {
            "kind": { "const": "object" },
            "value": {
              "type": "object",
              "additionalProperties": { "$ref": "#/$defs/rawValue" }
            }
          },
          "additionalProperties": false
        }
      ]
    },
    "coordinateExpectation": {
      "oneOf": [
        {
          "type": "object",
          "required": ["matcher", "value"],
          "properties": {
            "matcher": { "const": "exact" },
            "value": { "type": "number" }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["matcher", "value", "tolerance", "toleranceExplicit"],
          "properties": {
            "matcher": { "const": "approx" },
            "value": { "type": "number" },
            "tolerance": { "type": "number", "minimum": 0 },
            "toleranceExplicit": { "type": "boolean" }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["matcher", "values"],
          "properties": {
            "matcher": { "const": "sequence" },
            "values": {
              "type": "array",
              "items": { "$ref": "#/$defs/coordinateExpectation" }
            }
          },
          "additionalProperties": false
        }
      ]
    },
    "expected": {
      "oneOf": [
        {
          "type": "object",
          "required": ["matcher", "value"],
          "properties": {
            "matcher": { "const": "exact" },
            "value": { "$ref": "#/$defs/typedValue" }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": [
            "matcher",
            "displayCode",
            "normalizedCode",
            "acceptedNormalizedCodes"
          ],
          "properties": {
            "matcher": { "const": "error" },
            "displayCode": { "type": "string", "minLength": 1 },
            "normalizedCode": { "type": "string", "minLength": 1 },
            "acceptedNormalizedCodes": {
              "type": "array",
              "minItems": 1,
              "uniqueItems": true,
              "items": { "type": "string", "minLength": 1 }
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["matcher", "value", "tolerance", "toleranceExplicit"],
          "properties": {
            "matcher": { "const": "approx" },
            "value": { "type": "number" },
            "tolerance": { "type": "number", "minimum": 0 },
            "toleranceExplicit": { "type": "boolean" }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["matcher", "rows"],
          "properties": {
            "matcher": { "const": "array" },
            "rows": {
              "type": "array",
              "items": {
                "type": "array",
                "items": { "$ref": "#/$defs/expected" }
              }
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["matcher", "geometryType", "epsg", "coordinates"],
          "properties": {
            "matcher": { "const": "geometry" },
            "geometryType": { "type": "string", "minLength": 1 },
            "epsg": { "type": "integer", "minimum": 1, "maximum": 4294967295 },
            "coordinates": { "$ref": "#/$defs/coordinateExpectation" }
          },
          "additionalProperties": false
        }
      ]
    },
    "invocation": {
      "type": "object",
      "required": [
        "authoredSpelling",
        "canonicalName",
        "isAlias",
        "aliases",
        "category",
        "returnType",
        "executionClass",
        "args"
      ],
      "properties": {
        "authoredSpelling": { "type": "string", "pattern": "^[A-Z][A-Z0-9_.]*$" },
        "canonicalName": { "type": "string", "pattern": "^[A-Z][A-Z0-9_.]*$" },
        "isAlias": { "type": "boolean" },
        "aliases": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string", "pattern": "^[A-Z][A-Z0-9_.]*$" }
        },
        "category": { "type": "string", "minLength": 1 },
        "returnType": { "type": "string", "minLength": 1 },
        "executionClass": { "const": "sync_builtin" },
        "args": {
          "type": "array",
          "items": { "$ref": "#/$defs/typedValue" }
        }
      },
      "additionalProperties": false
    },
    "functionCase": {
      "type": "object",
      "required": ["name", "mode", "expected", "invocation"],
      "properties": {
        "name": { "$ref": "#/$defs/caseName" },
        "mode": { "const": "function" },
        "expected": { "$ref": "#/$defs/expected" },
        "invocation": { "$ref": "#/$defs/invocation" },
        "notes": { "type": "string" }
      },
      "additionalProperties": false
    },
    "formulaCase": {
      "type": "object",
      "required": ["name", "mode", "expected", "formula"],
      "properties": {
        "name": { "$ref": "#/$defs/caseName" },
        "mode": { "const": "formula" },
        "expected": { "$ref": "#/$defs/expected" },
        "formula": { "type": "string", "minLength": 1 },
        "notes": { "type": "string" }
      },
      "additionalProperties": false
    },
    "sourceCase": {
      "type": "object",
      "required": ["name", "mode", "expected", "source"],
      "properties": {
        "name": { "$ref": "#/$defs/caseName" },
        "mode": { "const": "source" },
        "expected": { "$ref": "#/$defs/expected" },
        "source": { "type": "string", "minLength": 1 },
        "notes": { "type": "string" }
      },
      "additionalProperties": false
    },
    "license": {
      "type": "object",
      "required": ["name", "path", "sha256", "bytes", "sourceCommit", "changeLicense"],
      "properties": {
        "name": { "const": "Business Source License 1.1" },
        "path": { "const": "content/download-sources/grid-core/LICENSE" },
        "sha256": { "$ref": "#/$defs/hex64" },
        "bytes": { "type": "integer", "minimum": 1 },
        "sourceCommit": { "$ref": "#/$defs/gitOid" },
        "changeLicense": { "const": "Apache-2.0" }
      },
      "additionalProperties": false
    },
    "metadata": {
      "type": "object",
      "required": [
        "taskType",
        "lineageId",
        "semanticSha256",
        "source",
        "validation",
        "license"
      ],
      "properties": {
        "taskType": { "const": "function-semantics" },
        "lineageId": {
          "type": "string",
          "pattern": "^grid-core:function-corpus:[A-Za-z0-9_.-]+$"
        },
        "semanticSha256": { "$ref": "#/$defs/hex64" },
        "source": {
          "type": "object",
          "required": [
            "sourceCommit",
            "path",
            "fileGitOid",
            "fileSha256",
            "fileBytes",
            "caseIndex"
          ],
          "properties": {
            "sourceCommit": { "$ref": "#/$defs/gitOid" },
            "path": {
              "type": "string",
              "pattern": "^rust/grid-runtime-core/tests/fixtures/function-corpus/[A-Za-z0-9_-]+\\.yaml$"
            },
            "fileGitOid": { "$ref": "#/$defs/gitOid" },
            "fileSha256": { "$ref": "#/$defs/hex64" },
            "fileBytes": { "type": "integer", "minimum": 1 },
            "caseIndex": { "type": "integer", "minimum": 1 }
          },
          "additionalProperties": false
        },
        "validation": {
          "type": "object",
          "required": [
            "authority",
            "fixtureTreeGitOid",
            "runnerSha256",
            "runnerContractSha256",
            "runtimeCatalogSha256",
            "grammarCatalogSha256"
          ],
          "properties": {
            "authority": { "const": "grid-runtime-core::tests/function_corpus.rs" },
            "fixtureTreeGitOid": { "$ref": "#/$defs/gitOid" },
            "runnerSha256": { "$ref": "#/$defs/hex64" },
            "runnerContractSha256": { "$ref": "#/$defs/hex64" },
            "runtimeCatalogSha256": { "$ref": "#/$defs/hex64" },
            "grammarCatalogSha256": { "$ref": "#/$defs/hex64" }
          },
          "additionalProperties": false
        },
        "license": { "$ref": "#/$defs/license" }
      },
      "additionalProperties": false
    }
  }
}
