{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://w3id.org/materica/schema/0.1/case.json",
  "$comment": "Hand-edited. The read request and the read result are read_request.json and read_result.json; the package's typed shapes are generated from them.",
  "title": "Case",
  "description": "A conformance case file, one per arm of a case (README.md, \"Case files\").",
  "type": "object",
  "required": ["case", "suite", "schema_version", "inputs", "expected", "compare"],
  "properties": {
    "case": {"type": "string", "pattern": "^[A-Z]{2}-[0-9]{3}(?![\\s\\S])"},
    "variant": {"type": "string", "pattern": "^[a-z0-9_]+(?![\\s\\S])"},
    "suite": {"$ref": "#/$defs/suite"},
    "schema_version": {"$ref": "record.writer.json#/$defs/version"},
    "claims": {
      "description": "Each suite the case's answer depends on, and whether the reader claims it.",
      "type": "object",
      "propertyNames": {"$ref": "#/$defs/suite"},
      "additionalProperties": {"type": "boolean"}
    },
    "inputs": {"$ref": "#/$defs/case_inputs"},
    "request": {
      "description": "A read request, an operation, a build-check case's build, or, where a case tests the request's own check, any JSON value.",
      "anyOf": [
        {"$ref": "read_request.json"},
        {"$ref": "#/$defs/case_operation"},
        {"$ref": "#/$defs/build_operation"},
        {"$ref": "record.writer.json#/$defs/json_value"}
      ]
    },
    "expected": {
      "description": "The members of the result compared: a read result's, a table evaluation case's evaluations, a record-check case's findings and listing, or a build-check case's members of the build's report.",
      "anyOf": [
        {"$ref": "#/$defs/compared_result"},
        {"$ref": "#/$defs/table_evaluations"},
        {"$ref": "#/$defs/record_check_expected"},
        {"$ref": "#/$defs/build_expected"}
      ]
    },
    "compare": {"$ref": "#/$defs/case_compare"}
  },
  "additionalProperties": false,
  "if": {"required": ["inputs"], "properties": {"inputs": {"required": ["request"]}}},
  "then": {"not": {"required": ["request"]}},
  "else": {
    "description": "A request, except in a record-check case, which has none unless it names an operation.",
    "anyOf": [{"required": ["request"]}, {"required": ["suite"], "properties": {"suite": {"const": "record_checks"}}}]
  },
  "allOf": [
    {
      "description": "A table evaluation case (README.md, \"Case files\"): the evaluate operation, no inputs, its evaluations, and a tolerance per precision; every other case, a read result and one tolerance.",
      "if": {"required": ["suite"], "properties": {"suite": {"const": "table_evaluation"}}},
      "then": {
        "required": ["request"],
        "properties": {
          "inputs": {"maxProperties": 0},
          "request": {"$ref": "#/$defs/evaluate_operation"},
          "expected": {"$ref": "#/$defs/table_evaluations"},
          "compare": {
            "required": ["relative_tolerance"],
            "properties": {
              "members": {"const": ["evaluations"]},
              "relative_tolerance": {"$ref": "#/$defs/precision_tolerances"}
            }
          }
        }
      },
      "else": {
        "properties": {"compare": {"properties": {"relative_tolerance": {"type": "number"}}}},
        "if": {"required": ["suite"], "properties": {"suite": {"const": "record_checks"}}, "not": {"required": ["request"]}},
        "then": {
          "description": "A record-check case (README.md, \"Case files\"): no request, the findings and the listing it expects, and the rules whose items it compares where it names any.",
          "properties": {
            "expected": {"$ref": "#/$defs/record_check_expected"},
            "compare": {"properties": {"members": {"items": {"enum": ["items", "listing"]}}}}
          }
        },
        "else": {
          "if": {"required": ["suite"], "properties": {"suite": {"const": "build_checks"}}},
          "then": {
            "description": "A build-check case (README.md, \"Case files\"): the check_build operation, the declaration among its inputs, the members of the build's report it expects, the rules whose refusals it compares where it names any, and, where it compares results it expects, the members of each result it compares.",
            "required": ["request"],
            "properties": {
              "inputs": {"required": ["declaration"]},
              "request": {"$ref": "#/$defs/build_operation"},
              "expected": {"$ref": "#/$defs/build_expected"},
              "compare": {"properties": {"members": {"items": {"enum": ["refusals", "results", "watches", "unused"]}}}}
            },
            "if": {
              "required": ["compare", "expected"],
              "properties": {
                "compare": {"required": ["members"], "properties": {"members": {"contains": {"const": "results"}}}},
                "expected": {"required": ["results"]}
              }
            },
            "then": {"properties": {"compare": {"required": ["result_members"]}}}
          },
          "else": {
            "properties": {
              "expected": {"$ref": "#/$defs/compared_result"},
              "compare": {"properties": {"rules_compared": false}}
            }
          }
        }
      }
    },
    {
      "description": "The check_build operation and the members of a result compared belong to a build-check case alone; a declaration among the inputs, to a build-check case and an engine-adapter case (README.md, \"Case files\").",
      "if": {"required": ["suite"], "properties": {"suite": {"const": "build_checks"}}},
      "else": {
        "properties": {
          "request": {"not": {"$ref": "#/$defs/build_operation"}},
          "compare": {"properties": {"result_members": false}}
        },
        "if": {"required": ["suite"], "properties": {"suite": {"const": "engine_adapter"}}},
        "else": {"properties": {"inputs": {"properties": {"declaration": false}}}}
      }
    },
    {
      "description": "A read result's status is held wherever compare names it.",
      "if": {
        "required": ["compare"],
        "properties": {"compare": {"required": ["members"], "properties": {"members": {"contains": {"const": "status"}}}}}
      },
      "then": {"properties": {"expected": {"required": ["status"]}}}
    },
    {
      "description": "A read result's reasons are held wherever compare names them.",
      "if": {
        "required": ["compare"],
        "properties": {"compare": {"required": ["members"], "properties": {"members": {"contains": {"const": "reasons"}}}}}
      },
      "then": {"properties": {"expected": {"required": ["reasons"]}}}
    }
  ],
  "$defs": {
    "compared_result": {
      "title": "ComparedResult",
      "description": "The members of a read result a case compares (README.md, \"Case files\": expected holds the members to compare), each as read_result.json gives it (tests/spec_sync/test_schemas.py holds the two lists equal). A case leaves out of compare, and so of expected, a member whose answer the specification leaves open, such as the status and reasons of a no_pick result whose alternatives' own reasons are not yet specified; status and reasons are held wherever compare names them.",
      "type": "object",
      "properties": {
        "status": {"$ref": "read_result.json#/$defs/read_status"},
        "reasons": {"type": "array", "items": {"$ref": "read_result.json#/$defs/reason"}},
        "value": {"$ref": "read_result.json#/$defs/result_value"},
        "alternative_values": {"type": "array", "items": {"$ref": "read_result.json#/$defs/alternative_value"}},
        "unit": {"$ref": "record.writer.json#/$defs/unit"},
        "conditions": {"type": "array", "items": {"$ref": "read_result.json#/$defs/condition_entry"}},
        "specimen": {"$ref": "read_result.json#/$defs/result_specimen"},
        "statistics": {"$ref": "record.writer.json#/$defs/field_statistics"},
        "provenance": {"$ref": "read_result.json#/$defs/result_provenance"},
        "labels": {"type": "array", "items": {"$ref": "read_result.json#/$defs/returned_label"}},
        "assumption": {"$ref": "read_result.json#/$defs/assumption"},
        "table": {"$ref": "record.writer.json#/$defs/table"}
      },
      "additionalProperties": false
    },
    "suite": {
      "enum": [
        "plain_read",
        "hash",
        "scene_documents",
        "fits_and_reference_data",
        "usd_read",
        "table_evaluation",
        "mixing",
        "family_evaluation",
        "sampling",
        "build_checks",
        "record_checks",
        "engine_adapter"
      ]
    },
    "case_inputs": {
      "description": "The files a case supplies, by name in its suite's inputs/.",
      "type": "object",
      "properties": {
        "records": {"type": "array", "items": {"type": "string"}},
        "value": {"type": "string"},
        "request": {"description": "A request that breaks the JSON rules, in place of request.", "type": "string"},
        "page_texts": {
          "description": "A record-check case's page-text documents, given to the checker beside the records (page_text.md section 3 item 1).",
          "type": "array",
          "items": {"type": "string"}
        },
        "declaration": {
          "description": "A build-check case's declaration, one file, read by record_format.md section 1 item 6, so that a declaration that breaks it can be given; an engine-adapter case names one too (README.md, \"Case files\").",
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    "case_compare": {
      "description": "The result's members compared, and how numbers are: relative_tolerance or exact, never both.",
      "type": "object",
      "required": ["members"],
      "properties": {
        "members": {"type": "array", "items": {"type": "string"}},
        "relative_tolerance": {
          "description": "One relative tolerance, or a table evaluation case's tolerance per precision.",
          "anyOf": [{"type": "number", "minimum": 0}, {"$ref": "#/$defs/precision_tolerances"}]
        },
        "exact": {"type": "boolean"},
        "rules_compared": {
          "description": "A record-check case's rules, by case id, whose findings and listing it compares, or a build-check case's, whose refusals it compares; the items and refusals of other rules are not compared.",
          "type": "array",
          "minItems": 1,
          "items": {"type": "string", "pattern": "^[A-Z]{2}-[0-9]{3}(?![\\s\\S])"}
        },
        "result_members": {
          "description": "A build-check case's members of each result compared, beside model, the location and field (or fields), which are always compared: compared_build_result's members (tests/spec_sync/test_schemas.py holds the two lists equal).",
          "type": "array",
          "items": {
            "enum": [
              "status",
              "reasons",
              "value",
              "alternative_values",
              "unit",
              "conditions",
              "specimen",
              "statistics",
              "provenance",
              "labels",
              "assumption",
              "table",
              "model",
              "record",
              "field",
              "fields",
              "specimen_differences",
              "accepted",
              "accepted_by",
              "trainable_by",
              "not_accepted"
            ]
          }
        }
      },
      "additionalProperties": false,
      "not": {"required": ["relative_tolerance", "exact"]}
    },
    "case_operation": {
      "description": "A case that is not a read (README.md, \"Operations\"), or a sampling case's draws mapped through a record's structure (README.md, \"Case files\"): the shape of each normal and uniform drawn, and of the result, are not yet specified.",
      "type": "object",
      "required": ["operation"],
      "properties": {
        "operation": {"enum": ["content_hash", "list_records", "quote_sha256", "transform_draws", "evaluate"]},
        "text": {"description": "A quote_sha256 operation's text, whose quote hash it gives.", "type": "string"},
        "normals": {"type": "array", "items": {"$ref": "record.writer.json#/$defs/json_value"}},
        "uniforms": {"type": "array", "items": {"$ref": "record.writer.json#/$defs/json_value"}}
      }
    },
    "record_check_expected": {
      "title": "RecordCheckExpected",
      "description": "A record-check case's expected answer (README.md, \"Case files\"): the findings, which fail the check, and the facts it lists without failing, each compared as a set.",
      "type": "object",
      "properties": {
        "items": {"type": "array", "items": {"$ref": "#/$defs/check_item"}},
        "listing": {"type": "array", "items": {"$ref": "#/$defs/check_item"}}
      },
      "additionalProperties": false
    },
    "check_item": {
      "title": "CheckItem",
      "description": "A finding or a listed fact: the case id of the rule that gives it, the input it is in, by file name, and a JSON pointer into that input.",
      "type": "object",
      "required": ["item", "document", "pointer"],
      "properties": {
        "item": {"type": "string", "pattern": "^[A-Z]{2}-[0-9]{3}(?![\\s\\S])"},
        "document": {"type": "string"},
        "pointer": {"type": "string"}
      },
      "additionalProperties": false
    },
    "build_operation": {
      "title": "BuildOperation",
      "description": "A build-check case's request (README.md, \"Case files\"): the build makes its own reads.",
      "type": "object",
      "required": ["operation"],
      "properties": {"operation": {"const": "check_build"}},
      "additionalProperties": false
    },
    "build_expected": {
      "title": "BuildExpected",
      "description": "The members of the build's report a build-check case compares (declarations.md section 2; README.md, \"Case files\"): the declaration's own refusals, compared as a set, or those of the rules compare.rules_compared names; the results, a set keyed by model, location and field (or fields); the watches, a set; the acceptances unused, a set. A member compare names and expected omits must be absent or empty. Written here, as compared_result is, until the build's report has its own schema.",
      "type": "object",
      "properties": {
        "refusals": {"type": "array", "items": {"$ref": "#/$defs/compared_refusal"}},
        "results": {"type": "array", "items": {"$ref": "#/$defs/compared_build_result"}},
        "watches": {"type": "array", "items": {"$ref": "#/$defs/compared_watch"}},
        "unused": {"type": "array", "items": {"$ref": "#/$defs/compared_unused"}}
      },
      "additionalProperties": false
    },
    "compared_refusal": {
      "title": "ComparedRefusal",
      "description": "A refusal of the declaration on its own (declarations.md section 1): the case id of the rule broken and a JSON pointer into the declaration; detail is never compared.",
      "type": "object",
      "required": ["item", "pointer"],
      "properties": {
        "item": {"type": "string", "pattern": "^[A-Z]{2}-[0-9]{3}(?![\\s\\S])"},
        "pointer": {"type": "string"},
        "detail": {"type": "string"}
      },
      "additionalProperties": false
    },
    "compared_build_result": {
      "title": "ComparedBuildResult",
      "description": "A result of the build a case compares (declarations.md sections 2 and 3 item 6): a field result's read members, as compared_result gives them (tests/spec_sync/test_schemas.py holds the two equal), and the build's members, model, the record it was read for, field (or a combination's fields), specimen_differences, accepted, accepted_by, trainable_by and not_accepted.",
      "type": "object",
      "properties": {
        "status": {"$ref": "read_result.json#/$defs/read_status"},
        "reasons": {"type": "array", "items": {"$ref": "read_result.json#/$defs/reason"}},
        "value": {"$ref": "read_result.json#/$defs/result_value"},
        "alternative_values": {"type": "array", "items": {"$ref": "read_result.json#/$defs/alternative_value"}},
        "unit": {"$ref": "record.writer.json#/$defs/unit"},
        "conditions": {"type": "array", "items": {"$ref": "read_result.json#/$defs/condition_entry"}},
        "specimen": {"$ref": "read_result.json#/$defs/result_specimen"},
        "statistics": {"$ref": "record.writer.json#/$defs/field_statistics"},
        "provenance": {"$ref": "read_result.json#/$defs/result_provenance"},
        "labels": {"type": "array", "items": {"$ref": "read_result.json#/$defs/returned_label"}},
        "assumption": {"$ref": "read_result.json#/$defs/assumption"},
        "table": {"$ref": "record.writer.json#/$defs/table"},
        "model": {"type": "string"},
        "record": {"$ref": "read_request.json#/$defs/material_ref"},
        "field": {"$ref": "read_request.json#/$defs/field_name"},
        "fields": {"type": "array", "minItems": 2, "items": {"$ref": "read_request.json#/$defs/field_name"}},
        "specimen_differences": {"type": "array", "items": {"$ref": "#/$defs/compared_specimen_difference"}},
        "accepted": {"type": "boolean"},
        "accepted_by": {"description": "0-based indexes into the model's acceptances, compared as a set.", "type": "array", "items": {"type": "integer", "minimum": 0}},
        "trainable_by": {"description": "The 0-based index of the result's entry in the model's trainable.", "type": "integer", "minimum": 0},
        "not_accepted": {
          "description": "Each reason and each specimen difference nothing covers, compared as a multiset.",
          "type": "array",
          "items": {"anyOf": [{"$ref": "read_result.json#/$defs/reason"}, {"$ref": "#/$defs/compared_specimen_difference"}]}
        }
      },
      "additionalProperties": false,
      "not": {"required": ["field", "fields"]}
    },
    "compared_specimen_difference": {
      "title": "ComparedSpecimenDifference",
      "description": "A specimen difference (declarations.md section 3): the section and fact, compared, and the record's fact (absent where the record does not hold it) and the target's.",
      "type": "object",
      "required": ["section", "fact"],
      "properties": {
        "section": {"type": "string"},
        "fact": {"type": "string"},
        "record": {"$ref": "record.writer.json#/$defs/json_value"},
        "target": {"$ref": "record.writer.json#/$defs/json_value"}
      },
      "additionalProperties": false
    },
    "compared_watch": {
      "title": "ComparedWatch",
      "description": "A run-time watch (declarations.md section 8): its model, location, field and condition, its source, and either the interval the condition may take, its ends compared as compare says, or the accept list.",
      "type": "object",
      "required": ["model", "field", "condition", "source"],
      "properties": {
        "model": {"type": "string"},
        "record": {"$ref": "read_request.json#/$defs/material_ref"},
        "field": {"$ref": "read_request.json#/$defs/field_name"},
        "condition": {"type": "string"},
        "unit": {"type": "string"},
        "source": {"enum": ["tolerance", "form_range", "declared"]},
        "interval": {"type": "array", "minItems": 2, "maxItems": 2, "items": {"type": "number"}},
        "accept": {"type": "array", "minItems": 1, "items": {"type": "string"}}
      },
      "additionalProperties": false,
      "oneOf": [{"required": ["interval"]}, {"required": ["accept"]}]
    },
    "compared_unused": {
      "title": "ComparedUnused",
      "description": "An acceptance no result used (declarations.md section 2 item 9): its model, by name, and its 0-based index in the model's acceptances.",
      "type": "object",
      "required": ["model", "acceptance"],
      "properties": {"model": {"type": "string"}, "acceptance": {"type": "integer", "minimum": 0}},
      "additionalProperties": false
    },
    "evaluate_operation": {
      "description": "A table evaluation case's request (README.md, \"Case files\"): one table as record_format.md section 6 gives it, the axis condition aside, and the axis values it is evaluated at.",
      "type": "object",
      "required": ["operation", "form", "table", "at"],
      "properties": {
        "operation": {"const": "evaluate"},
        "form": {"const": "table"},
        "table": {"$ref": "record.writer.json#/$defs/json_object", "required": ["axis", "axis_unit", "x", "y", "scheme", "edges"]},
        "at": {"type": "array", "items": {"type": "number"}, "minItems": 1}
      },
      "additionalProperties": false
    },
    "table_evaluations": {
      "description": "A table evaluation case's expected answer: one entry per axis value, in order.",
      "type": "object",
      "required": ["evaluations"],
      "properties": {"evaluations": {"type": "array", "items": {"$ref": "#/$defs/table_evaluation"}}},
      "additionalProperties": false
    },
    "table_evaluation": {
      "oneOf": [
        {
          "title": "TableEvaluationInside",
          "description": "Inside the readable range, off every edge: the value and the derivative, both sides at an interior knot strictly inside the range, and none where the range is one point.",
          "type": "object",
          "required": ["in_range", "value", "derivative"],
          "properties": {
            "in_range": {"const": true},
            "value": {"type": "number"},
            "derivative": {
              "anyOf": [
                {"type": "number"},
                {"const": "none"},
                {
                  "title": "DerivativeSides",
                  "type": "object",
                  "required": ["left", "right"],
                  "properties": {"left": {"type": "number"}, "right": {"type": "number"}},
                  "additionalProperties": false
                }
              ]
            }
          },
          "additionalProperties": false
        },
        {
          "title": "TableEvaluationRefused",
          "description": "At a declared edge, or across one from the readable range: the refusal, with no number.",
          "type": "object",
          "required": ["refused"],
          "properties": {"refused": {"const": true}},
          "additionalProperties": false
        },
        {
          "title": "TableEvaluationOutside",
          "description": "Elsewhere outside the readable range: the value at its nearest end, taken from inside it, and a derivative of 0.",
          "type": "object",
          "required": ["in_range", "value", "derivative"],
          "properties": {"in_range": {"const": false}, "value": {"type": "number"}, "derivative": {"const": 0}},
          "additionalProperties": false
        }
      ]
    },
    "precision_tolerances": {
      "description": "A table evaluation case's relative tolerance per precision: 1e-12 for binary64, and the file's for binary32.",
      "type": "object",
      "required": ["binary64", "binary32"],
      "properties": {"binary64": {"const": 1e-12}, "binary32": {"type": "number", "exclusiveMinimum": 0}},
      "additionalProperties": false
    }
  }
}
