{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://carbonbridge.nl/protocol/farm-registry-record-v1.schema.json",
  "title": "Farm Registry Record",
  "description": "Farm Registry Protocol, version 1.0. One record per farm polygon, written on the day the farm is onboarded. Published by Carbon Bridge Services B.V. under CC BY 4.0.",
  "type": "object",
  "required": [
    "version",
    "country",
    "geometry",
    "onboardedAt",
    "attributes"
  ],
  "additionalProperties": false,
  "properties": {
    "version": {
      "const": "1.0"
    },
    "country": {
      "type": "string",
      "pattern": "^[A-Z]{2}$",
      "description": "ISO 3166-1 alpha-2 code of the country the polygon lies in. Supplied by the registrant, never derived from coordinates."
    },
    "registrantReference": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "description": "The registrant's own reference for the farm."
    },
    "geometry": {
      "type": "object",
      "description": "GeoJSON Polygon (RFC 7946, WGS84) in canonical form: see the protocol's Canonical geometry section.",
      "required": [
        "type",
        "coordinates"
      ],
      "additionalProperties": false,
      "properties": {
        "type": {
          "const": "Polygon"
        },
        "coordinates": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "array",
            "minItems": 4,
            "items": {
              "type": "array",
              "minItems": 2,
              "maxItems": 2,
              "items": {
                "type": "number"
              }
            }
          }
        }
      }
    },
    "onboardedAt": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{1,9})?Z$",
      "description": "ISO 8601 instant in UTC (trailing Z) at which the farm was onboarded."
    },
    "attributes": {
      "type": "object",
      "required": [
        "tenureReference",
        "landUseHistory",
        "imageryReferences",
        "aboveGroundBiomass"
      ],
      "additionalProperties": true,
      "properties": {
        "tenureReference": {
          "type": "string",
          "minLength": 1
        },
        "landUseHistory": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "from",
              "use"
            ],
            "additionalProperties": false,
            "properties": {
              "from": {
                "type": "string",
                "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
              },
              "to": {
                "type": "string",
                "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
              },
              "use": {
                "type": "string",
                "minLength": 1
              }
            }
          }
        },
        "imageryReferences": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^0x[0-9a-f]{64}$"
          },
          "description": "SHA-256 content hashes of the imagery kept with the record."
        },
        "aboveGroundBiomass": {
          "type": "object",
          "required": [
            "value",
            "unit"
          ],
          "additionalProperties": false,
          "properties": {
            "value": {
              "type": "number",
              "minimum": 0
            },
            "unit": {
              "const": "t/ha"
            }
          },
          "description": "The recorded above-ground biomass at onboarding, tonnes per hectare."
        }
      }
    },
    "enrolments": {
      "type": "array",
      "description": "Programmes, projects or registries the polygon is enrolled in. Not covered by the record hash.",
      "items": {
        "type": "object",
        "required": [
          "programmeId",
          "since"
        ],
        "additionalProperties": false,
        "properties": {
          "programmeId": {
            "type": "string",
            "minLength": 1
          },
          "since": {
            "type": "string",
            "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
          }
        }
      }
    },
    "anchor": {
      "type": "object",
      "description": "Where the anchor digest was written. Not covered by the record hash.",
      "required": [
        "ledger",
        "reference",
        "anchoredAt"
      ],
      "additionalProperties": false,
      "properties": {
        "ledger": {
          "type": "string",
          "minLength": 1
        },
        "reference": {
          "type": "string",
          "minLength": 1
        },
        "anchoredAt": {
          "type": "string",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{1,9})?Z$"
        }
      }
    }
  }
}
