{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://northstarintel.ai/spec/receipt-v1.schema.json",
  "title": "North Star Verified signed payload v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "issued_at",
    "key_id",
    "verified_on",
    "verify_on",
    "standard",
    "v",
    "id",
    "prev_sha256",
    "verdict",
    "publish_level",
    "owner_signoff",
    "reviewer",
    "evidence"
  ],
  "properties": {
    "issued_at": {
      "type": "string"
    },
    "key_id": {
      "type": "string"
    },
    "verified_on": {
      "type": "string"
    },
    "verify_on": {
      "type": "string"
    },
    "standard": {
      "const": "North Star Verified"
    },
    "v": {
      "const": 1
    },
    "id": {
      "type": "string",
      "pattern": "^NSV-[0-9]{4}-[0-9A-HJKMNP-TV-Z]{6}$"
    },
    "prev_sha256": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        {
          "type": "null"
        }
      ]
    },
    "verdict": {
      "enum": [
        "verified_gain",
        "verified_no_gain",
        "inconclusive",
        "disputed"
      ]
    },
    "publish_level": {
      "enum": [
        "none",
        "numbers_only",
        "anonymized",
        "named"
      ]
    },
    "owner_signoff": {
      "const": true
    },
    "reviewer": {
      "const": "independent"
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "count",
        "sha256"
      ],
      "properties": {
        "count": {
          "type": "integer",
          "minimum": 2
        },
        "sha256": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[0-9a-f]{64}$"
          },
          "minItems": 2,
          "uniqueItems": true
        }
      }
    },
    "measurement": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "metric",
        "baseline",
        "result"
      ],
      "properties": {
        "metric": {
          "type": "string"
        },
        "baseline": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "value",
            "window"
          ],
          "properties": {
            "value": {
              "type": "number"
            },
            "window": {
              "type": "string"
            }
          }
        },
        "result": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "value",
            "window"
          ],
          "properties": {
            "value": {
              "type": "number"
            },
            "window": {
              "type": "string"
            }
          }
        }
      }
    },
    "context": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "system",
        "other_changes"
      ],
      "properties": {
        "system": {
          "type": "string"
        },
        "other_changes": {
          "type": "string"
        }
      }
    },
    "decision": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "question",
        "owner_role"
      ],
      "properties": {
        "question": {
          "type": "string"
        },
        "owner_role": {
          "type": "string"
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "publish_level": {
            "const": "none"
          }
        }
      },
      "then": {
        "not": {
          "anyOf": [
            {
              "required": [
                "measurement"
              ]
            },
            {
              "required": [
                "context"
              ]
            },
            {
              "required": [
                "decision"
              ]
            }
          ]
        }
      }
    },
    {
      "if": {
        "properties": {
          "publish_level": {
            "const": "numbers_only"
          }
        }
      },
      "then": {
        "required": [
          "measurement"
        ],
        "not": {
          "anyOf": [
            {
              "required": [
                "context"
              ]
            },
            {
              "required": [
                "decision"
              ]
            }
          ]
        }
      }
    },
    {
      "if": {
        "properties": {
          "publish_level": {
            "const": "anonymized"
          }
        }
      },
      "then": {
        "required": [
          "measurement",
          "context"
        ],
        "not": {
          "anyOf": [
            {
              "required": [
                "decision"
              ]
            }
          ]
        }
      }
    },
    {
      "if": {
        "properties": {
          "publish_level": {
            "const": "named"
          }
        }
      },
      "then": {
        "required": [
          "measurement",
          "context",
          "decision"
        ]
      }
    }
  ]
}
