{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://mtproto.eu/mtproto-1.2.schema.json",
  "title": "MTPROTO record, version 1.2",
  "description": "Normative JSON Schema for the JSON serialisation of an MTPROTO record. See https://mtproto.eu/spec.html",
  "type": "object",
  "required": ["mtproto", "record_id", "issued", "issuer", "item", "results", "traceable_to"],
  "additionalProperties": true,
  "properties": {
    "mtproto": {
      "description": "Format version this record is written against.",
      "type": "string",
      "const": "1.2"
    },
    "record_id": {
      "description": "Globally unique identifier for this record.",
      "type": "string",
      "pattern": "^urn:mtproto:[0-9a-zA-Z._~%-]+$"
    },
    "issued": {
      "description": "Instant at which the record was issued, RFC 3339, UTC.",
      "type": "string",
      "format": "date-time"
    },
    "issuer": {
      "type": "object",
      "required": ["name", "key_id"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "key_id": { "$ref": "#/$defs/key_id" },
        "accreditation": {
          "description": "Free-text reference to the issuer's accreditation, if any. Not interpreted.",
          "type": "string"
        }
      },
      "additionalProperties": true
    },
    "item": {
      "type": "object",
      "required": ["description"],
      "properties": {
        "description": { "type": "string", "minLength": 1 },
        "identifier": { "type": "string" },
        "manufacturer": { "type": "string" },
        "model": { "type": "string" }
      },
      "additionalProperties": true
    },
    "conditions": {
      "description": "Environmental quantities during measurement. Uncertainty is optional here.",
      "type": "object",
      "additionalProperties": { "$ref": "#/$defs/quantity" }
    },
    "results": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/result" }
    },
    "traceable_to": {
      "description": "References to the certificates of the reference standards used. Empty only for an anchor record.",
      "type": "array",
      "items": { "$ref": "#/$defs/reference" }
    },
    "valid_until": {
      "type": "string",
      "format": "date"
    },
    "supersedes": {
      "description": "record_id of the record this one replaces.",
      "type": "string",
      "pattern": "^urn:mtproto:[0-9a-zA-Z._~%-]+$"
    },
    "notes": {
      "description": "Human-readable remarks. Consumers must not parse this member.",
      "type": "string"
    },
    "signature": { "$ref": "#/$defs/signature" }
  },

  "$defs": {
    "key_id": {
      "type": "string",
      "pattern": "^mtp:ed25519:[0-9A-Za-z_-]+$"
    },
    "unit": {
      "description": "UCUM case-sensitive code. Free-text units are not conforming.",
      "type": "string",
      "minLength": 1
    },
    "quantity": {
      "type": "object",
      "required": ["value", "unit"],
      "properties": {
        "value": { "type": "number" },
        "unit": { "$ref": "#/$defs/unit" },
        "u": {
          "description": "Standard uncertainty, in the same unit as value.",
          "type": "number",
          "minimum": 0
        },
        "k": {
          "description": "Coverage factor.",
          "type": "number",
          "exclusiveMinimum": 0
        },
        "distribution": {
          "type": "string",
          "enum": ["normal", "rectangular", "triangular", "u-shaped"],
          "default": "normal"
        }
      },
      "dependentRequired": { "k": ["u"] },
      "additionalProperties": true
    },
    "result": {
      "allOf": [{ "$ref": "#/$defs/quantity" }],
      "type": "object",
      "required": ["quantity", "value", "unit", "u", "k"],
      "properties": {
        "quantity": {
          "description": "Registered dotted name, or a private name prefixed with x-.",
          "type": "string",
          "pattern": "^(x-)?[a-z][a-z0-9]*(\\.[a-z][a-z0-9]*)*$"
        },
        "method": { "type": "string" },
        "nominal": { "type": "number" },
        "point": {
          "description": "Measurement point, for results reported over a range.",
          "type": "number"
        }
      }
    },
    "reference": {
      "type": "object",
      "required": ["standard", "certificate"],
      "properties": {
        "standard": { "type": "string", "minLength": 1 },
        "certificate": {
          "type": "string",
          "pattern": "^urn:mtproto:[0-9a-zA-Z._~%-]+$"
        },
        "resolver": {
          "description": "Advisory base URL for retrieving the referenced record.",
          "type": "string",
          "format": "uri"
        }
      },
      "additionalProperties": true
    },
    "signature": {
      "description": "Detached signature over the canonical form, with the signature member removed.",
      "type": "object",
      "required": ["alg", "key_id", "sig"],
      "properties": {
        "alg": { "type": "string", "const": "ed25519" },
        "key_id": { "$ref": "#/$defs/key_id" },
        "sig": {
          "description": "base64url, unpadded.",
          "type": "string",
          "pattern": "^[0-9A-Za-z_-]+$"
        }
      },
      "additionalProperties": false
    }
  }
}
