{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://pvjohnston.com/research/metrics.schema.json",
  "title": "pvjohnston.com traceable experiment metrics",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "experiment",
    "provenance",
    "metrics"
  ],
  "properties": {
    "schema_version": {
      "const": 1
    },
    "experiment": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$"
    },
    "provenance": {
      "$ref": "#/$defs/provenance"
    },
    "metrics": {
      "type": "object",
      "minProperties": 1,
      "propertyNames": {
        "pattern": "^[a-z][a-z0-9_]*$"
      },
      "additionalProperties": {
        "$ref": "#/$defs/metric"
      }
    }
  },
  "$defs": {
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": ["generated_at", "generator", "inputs"],
      "properties": {
        "generated_at": {
          "type": "string",
          "format": "date-time",
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
        },
        "generator": {
          "$ref": "#/$defs/repositoryPath"
        },
        "inputs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["path", "sha256"],
            "properties": {
              "path": {
                "$ref": "#/$defs/repositoryPath"
              },
              "sha256": {
                "type": "string",
                "pattern": "^[0-9a-fA-F]{64}$"
              }
            }
          }
        }
      }
    },
    "repositoryPath": {
      "type": "string",
      "minLength": 1,
      "pattern": "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$))(?!.*\\s)(?!.*\\\\).+$"
    },
    "metric": {
      "oneOf": [
        {
          "$ref": "#/$defs/integerMetric"
        },
        {
          "$ref": "#/$defs/numberMetric"
        },
        {
          "$ref": "#/$defs/booleanMetric"
        }
      ]
    },
    "metricCommon": {
      "type": "object",
      "required": ["type", "value", "description"],
      "properties": {
        "type": {
          "enum": ["integer", "number", "boolean"]
        },
        "description": {
          "type": "string",
          "minLength": 1,
          "pattern": "\\S"
        },
        "unit": {
          "type": "string",
          "minLength": 1,
          "pattern": "^(?=.*\\S)[^\\r\\n\\t]+$"
        }
      }
    },
    "integerMetric": {
      "allOf": [
        {
          "$ref": "#/$defs/metricCommon"
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "value", "description"],
          "properties": {
            "type": {"const": "integer"},
            "value": {"type": "integer"},
            "description": {"type": "string"},
            "unit": {"type": "string"}
          }
        }
      ]
    },
    "numberMetric": {
      "allOf": [
        {
          "$ref": "#/$defs/metricCommon"
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "value", "format", "description"],
          "properties": {
            "type": {"const": "number"},
            "value": {"type": "number"},
            "format": {"$ref": "#/$defs/numberFormat"},
            "description": {"type": "string"},
            "unit": {"type": "string"}
          }
        }
      ]
    },
    "booleanMetric": {
      "allOf": [
        {
          "$ref": "#/$defs/metricCommon"
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "value", "description"],
          "properties": {
            "type": {"const": "boolean"},
            "value": {"type": "boolean"},
            "description": {"type": "string"},
            "unit": {"type": "string"}
          }
        }
      ]
    },
    "numberFormat": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["style", "digits"],
          "properties": {
            "style": {"enum": ["fixed", "scientific", "percent"]},
            "digits": {"type": "integer", "minimum": 0, "maximum": 12}
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["style"],
          "properties": {
            "style": {"const": "raw"}
          }
        }
      ]
    }
  }
}
