{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://trulyfinal.github.io/barcode-grader/schema/report.schema.json",
  "title": "barcode-grader JSON report",
  "description": "What `barcode-grader --format json` prints and `formatJson` returns: one file's report as an object, or several as an array of the same objects. Fields are only added within a schemaVersion.",
  "oneOf": [
    { "$ref": "#/definitions/file" },
    { "type": "array", "items": { "$ref": "#/definitions/file" } }
  ],
  "definitions": {
    "file": {
      "description": "One file: its report, or the reason it couldn't be read.",
      "oneOf": [{ "$ref": "#/definitions/report" }, { "$ref": "#/definitions/unreadable" }]
    },
    "tool": {
      "type": "object",
      "required": ["name", "version"],
      "properties": {
        "name": { "const": "barcode-grader" },
        "version": { "type": "string", "description": "The version of barcode-grader that wrote the report." }
      }
    },
    "unreadable": {
      "type": "object",
      "required": ["schemaVersion", "tool", "file", "error"],
      "not": { "required": ["codes"] },
      "properties": {
        "schemaVersion": { "const": 1 },
        "tool": { "$ref": "#/definitions/tool" },
        "file": { "type": "string" },
        "error": { "type": "string", "description": "Why the file couldn't be read, as a sentence." }
      }
    },
    "report": {
      "type": "object",
      "required": ["schemaVersion", "tool", "file", "kind", "pages", "sized", "codes", "counts"],
      "not": { "required": ["error"] },
      "properties": {
        "schemaVersion": { "const": 1 },
        "tool": { "$ref": "#/definitions/tool" },
        "file": { "type": "string", "description": "The file as it was named on the command line." },
        "kind": { "enum": ["pdf", "image"] },
        "pages": { "type": "integer", "minimum": 1, "description": "Pages in a PDF; 1 for a picture." },
        "sized": { "type": "boolean", "description": "Whether sizes could be measured: a PDF always; a picture with a resolution or a given width." },
        "codes": { "type": "array", "items": { "$ref": "#/definitions/code" }, "description": "Every code found, in reading order: page, then top to bottom, then left to right." },
        "counts": {
          "type": "object",
          "description": "How many codes, and how many came out with each verdict.",
          "required": ["codes", "ok", "check", "fail"],
          "properties": {
            "codes": { "type": "integer", "minimum": 0 },
            "ok": { "type": "integer", "minimum": 0 },
            "check": { "type": "integer", "minimum": 0 },
            "fail": { "type": "integer", "minimum": 0 }
          }
        }
      }
    },
    "rgb": { "type": "array", "items": { "type": "number", "minimum": 0, "maximum": 255 }, "minItems": 3, "maxItems": 3 },
    "numberOrNull": { "type": ["number", "null"] },
    "issue": {
      "type": "object",
      "required": ["rule", "level", "text"],
      "properties": {
        "rule": { "enum": ["read", "checkDigit", "size", "barHeight", "quiet", "colour", "symbology", "content", "grade", "print"] },
        "level": { "enum": ["fail", "check"], "description": "fail: will likely fail to scan or breaks a GS1 rule. check: worth a look." },
        "text": { "type": "string", "description": "What is wrong and what to do, as a sentence." }
      }
    },
    "code": {
      "type": "object",
      "required": ["page", "format", "name", "text", "value", "gs1", "inverted", "box", "photo", "checkDigit", "size", "barHeight", "quiet", "colour", "grade", "issues", "verdict", "summary"],
      "properties": {
        "page": { "type": "integer", "minimum": 1 },
        "format": { "type": "string", "description": "ZXing's format name, e.g. EAN13, QRCode." },
        "name": { "type": "string", "description": "The name people use, e.g. EAN-13, QR code." },
        "text": { "type": "string", "description": "What it encodes, as decoded." },
        "value": { "type": "string", "description": "What it means: the GTIN, the link, the element string in brackets." },
        "gs1": { "type": "boolean" },
        "inverted": { "type": "boolean", "description": "Light on dark (reversed)." },
        "box": {
          "type": "object",
          "description": "Where it sits on the page, as fractions of the page (0–1) from the top left.",
          "required": ["x", "y", "w", "h"],
          "properties": { "x": { "type": "number" }, "y": { "type": "number" }, "w": { "type": "number" }, "h": { "type": "number" } }
        },
        "error": { "type": "string", "description": "Present when the code was found but not decoded, e.g. ChecksumError." },
        "addOn": { "type": "string", "pattern": "^([0-9]{2}|[0-9]{5})$", "description": "Present when a two- or five-digit add-on (EAN-2, EAN-5) was read beside an EAN/UPC. Reported, not checked." },
        "photo": { "type": "boolean", "description": "The close-up looks like a photo of a printed pack: tones, quiet zones and grade aren't judged." },
        "checkDigit": {
          "type": ["object", "null"],
          "required": ["ok", "expected"],
          "properties": { "ok": { "type": "boolean" }, "expected": { "type": "string" } }
        },
        "size": {
          "type": ["object", "null"],
          "description": "null without a print size.",
          "required": ["xMm", "magnification", "minPct", "maxPct", "minMm", "maxMm", "context", "widthMm", "heightMm"],
          "properties": {
            "xMm": { "type": "number", "description": "X-dimension (module width) in mm." },
            "magnification": { "$ref": "#/definitions/numberOrNull", "description": "Percent of the 0.330 mm target X (EAN/UPC only: magnification is an EAN/UPC term). null for every other symbology, whose size is xMm against minMm–maxMm." },
            "minPct": { "$ref": "#/definitions/numberOrNull" },
            "maxPct": { "$ref": "#/definitions/numberOrNull" },
            "minMm": { "$ref": "#/definitions/numberOrNull" },
            "maxMm": { "$ref": "#/definitions/numberOrNull" },
            "context": { "type": ["string", "null"], "description": "Where GS1's range applies, e.g. retail tills." },
            "widthMm": { "$ref": "#/definitions/numberOrNull" },
            "heightMm": { "$ref": "#/definitions/numberOrNull" }
          }
        },
        "barHeight": {
          "type": ["object", "null"],
          "required": ["mm", "neededMm"],
          "properties": { "mm": { "type": "number" }, "neededMm": { "$ref": "#/definitions/numberOrNull" } }
        },
        "quiet": {
          "type": ["object", "null"],
          "description": "Clear space each side in modules (X), and what the symbology needs.",
          "required": ["left", "right", "top", "bottom", "need"],
          "properties": {
            "left": { "$ref": "#/definitions/numberOrNull" },
            "right": { "$ref": "#/definitions/numberOrNull" },
            "top": { "$ref": "#/definitions/numberOrNull" },
            "bottom": { "$ref": "#/definitions/numberOrNull" },
            "need": { "type": "object", "required": ["left", "right"], "properties": { "left": { "type": "number" }, "right": { "type": "number" } } }
          }
        },
        "colour": {
          "type": ["object", "null"],
          "description": "Measured bar and space colours (sRGB) and the symbol contrast they give in red light, 0–1.",
          "required": ["bar", "space", "contrast"],
          "properties": { "bar": { "$ref": "#/definitions/rgb" }, "space": { "$ref": "#/definitions/rgb" }, "contrast": { "type": "number" } }
        },
        "grade": {
          "type": ["object", "null"],
          "description": "Predicted ISO/IEC 15416 (linear) or 15415 (2D) grade from the artwork.",
          "required": ["letter", "value", "notation", "limiting", "required"],
          "properties": {
            "letter": { "enum": ["A", "B", "C", "D", "F"] },
            "value": { "type": "number", "minimum": 0, "maximum": 4 },
            "notation": { "type": "string", "description": "grade/aperture/wavelength, e.g. 2.8/06/660." },
            "limiting": { "type": ["string", "null"], "description": "The parameter with the lowest grade." },
            "required": { "$ref": "#/definitions/numberOrNull", "description": "The grade GS1 asks for where the code is scanned." }
          }
        },
        "issues": { "type": "array", "items": { "$ref": "#/definitions/issue" } },
        "verdict": { "enum": ["ok", "check", "fail"] },
        "summary": { "type": "string", "description": "One paragraph in plain words: what's wrong first, then what it reads and what was checked." }
      }
    }
  }
}
