{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://usecalculator.app/specs/rulepack.schema.json",
  "title": "UseCalculator rule pack v1",
  "description": "Shape contract for a trusted, versioned rule registry. Runtime validation additionally enforces real dates, ordering, exact scope, review status and ambiguity. Schema validation does not establish legal authority.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "id",
    "version",
    "family",
    "title",
    "synthetic",
    "status",
    "scope",
    "effectiveFrom",
    "effectiveTo",
    "review",
    "sources",
    "assumptions",
    "exclusions",
    "calculation"
  ],
  "properties": {
    "schemaVersion": {
      "const": "1.0.0"
    },
    "id": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9.-]{2,119}$"
    },
    "version": {
      "type": "string",
      "pattern": "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$"
    },
    "family": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9-]{0,79}$"
    },
    "title": {
      "type": "string",
      "minLength": 1,
      "maxLength": 180
    },
    "synthetic": {
      "type": "boolean"
    },
    "status": {
      "enum": [
        "draft",
        "verified",
        "retired"
      ]
    },
    "scope": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "country",
        "subdivision",
        "industry",
        "product",
        "event",
        "placeBasis",
        "person",
        "taxYear"
      ],
      "properties": {
        "country": {
          "type": "string",
          "pattern": "^[A-Z]{2}$"
        },
        "subdivision": {
          "anyOf": [
            {
              "type": "null"
            },
            {
              "type": "string",
              "pattern": "^[A-Z]{2}-[A-Z0-9]{1,8}$"
            }
          ]
        },
        "industry": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9-]{0,79}$"
        },
        "product": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9-]{0,79}$"
        },
        "event": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9-]{0,79}$"
        },
        "placeBasis": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9-]{0,79}$"
        },
        "person": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9-]{0,79}$"
        },
        "taxYear": {
          "anyOf": [
            {
              "type": "null"
            },
            {
              "type": "integer",
              "minimum": 1900,
              "maximum": 2200
            }
          ]
        }
      }
    },
    "effectiveFrom": {
      "type": "string",
      "format": "date",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
    },
    "effectiveTo": {
      "anyOf": [
        {
          "type": "null"
        },
        {
          "type": "string",
          "format": "date",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
        }
      ]
    },
    "review": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "reviewedAt",
        "reviewDueAt",
        "reviewer",
        "reviewerRole",
        "testReportHash"
      ],
      "properties": {
        "reviewedAt": {
          "type": "string",
          "format": "date",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
        },
        "reviewDueAt": {
          "type": "string",
          "format": "date",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
        },
        "reviewer": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "reviewerRole": {
          "enum": [
            "fixture-author",
            "domain-specialist"
          ]
        },
        "testReportHash": {
          "type": "string",
          "pattern": "^sha256:[a-f0-9]{64}$"
        }
      }
    },
    "sources": {
      "type": "array",
      "minItems": 1,
      "maxItems": 20,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "kind",
          "title",
          "url",
          "retrievedAt",
          "locator",
          "contentHash"
        ],
        "properties": {
          "kind": {
            "enum": [
              "fixture",
              "official"
            ]
          },
          "title": {
            "type": "string",
            "minLength": 1,
            "maxLength": 180
          },
          "url": {
            "type": "string",
            "format": "uri",
            "pattern": "^https://",
            "maxLength": 2000
          },
          "retrievedAt": {
            "type": "string",
            "format": "date",
            "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
          },
          "locator": {
            "type": "string",
            "minLength": 1,
            "maxLength": 400
          },
          "contentHash": {
            "type": "string",
            "pattern": "^sha256:[a-f0-9]{64}$"
          }
        }
      }
    },
    "assumptions": {
      "type": "array",
      "minItems": 1,
      "maxItems": 30,
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 1000
      }
    },
    "exclusions": {
      "type": "array",
      "minItems": 1,
      "maxItems": 30,
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 1000
      }
    },
    "calculation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "engine",
        "rateBasisPoints",
        "outputScale",
        "rounding",
        "unit"
      ],
      "properties": {
        "engine": {
          "const": "percentage-of-basis-points"
        },
        "rateBasisPoints": {
          "type": "integer",
          "minimum": 0,
          "maximum": 100000
        },
        "outputScale": {
          "const": 2
        },
        "rounding": {
          "const": "half-up"
        },
        "unit": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9-]{0,79}$"
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "synthetic": {
            "const": false
          }
        },
        "required": [
          "synthetic"
        ]
      },
      "then": {
        "properties": {
          "sources": {
            "items": {
              "properties": {
                "kind": {
                  "const": "official"
                }
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "synthetic": {
            "const": false
          },
          "status": {
            "const": "verified"
          }
        },
        "required": [
          "synthetic",
          "status"
        ]
      },
      "then": {
        "properties": {
          "review": {
            "properties": {
              "reviewerRole": {
                "const": "domain-specialist"
              }
            }
          }
        }
      }
    }
  ]
}
