{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.alliedchb.com/schemas/postal-items.v1.json",
  "title": "Allied CHB postal item feed, version 1",
  "description": "Item-level data for the U.S. entry of international mail: entry type 13 (informal mail entry), the postal informal entry and formal entry. The fields follow the CN 22 and CN 23 customs declaration, which the UPU ITMATT message carries in electronic form.",
  "type": "object",
  "required": [
    "batch_ref",
    "sent_at",
    "items"
  ],
  "additionalProperties": false,
  "properties": {
    "batch_ref": {
      "type": "string",
      "maxLength": 50,
      "description": "Sender's reference for this batch. Unique per batch."
    },
    "sent_at": {
      "type": "string",
      "format": "date-time"
    },
    "dispatch": {
      "$ref": "#/$defs/dispatch"
    },
    "items": {
      "type": "array",
      "minItems": 1,
      "maxItems": 5000,
      "items": {
        "$ref": "#/$defs/item"
      }
    }
  },
  "$defs": {
    "money": {
      "type": "string",
      "pattern": "^[0-9]{1,9}\\.[0-9]{2}$"
    },
    "country": {
      "type": "string",
      "pattern": "^[A-Z]{2}$",
      "description": "ISO 3166-1 alpha-2."
    },
    "s10": {
      "type": "string",
      "pattern": "^[A-Z]{2}[0-9]{9}[A-Z]{2}$",
      "description": "UPU S10 item identifier: 2 letters, 8-digit serial number, 1 check digit, 2-letter country code of the issuing post. Allied checks the check digit (weights 8 6 4 2 3 5 9 7)."
    },
    "foreign_address": {
      "type": "object",
      "required": [
        "line1",
        "city",
        "country"
      ],
      "additionalProperties": false,
      "properties": {
        "line1": {
          "type": "string",
          "maxLength": 70
        },
        "line2": {
          "type": "string",
          "maxLength": 70
        },
        "city": {
          "type": "string",
          "maxLength": 35
        },
        "region": {
          "type": "string",
          "maxLength": 35
        },
        "postal_code": {
          "type": "string",
          "maxLength": 17
        },
        "country": {
          "$ref": "#/$defs/country"
        }
      }
    },
    "us_address": {
      "type": "object",
      "required": [
        "line1",
        "city",
        "state",
        "zip5"
      ],
      "additionalProperties": false,
      "properties": {
        "line1": {
          "type": "string",
          "maxLength": 70
        },
        "line2": {
          "type": "string",
          "maxLength": 70
        },
        "city": {
          "type": "string",
          "maxLength": 35
        },
        "state": {
          "type": "string",
          "pattern": "^[A-Z]{2}$"
        },
        "zip5": {
          "type": "string",
          "pattern": "^[0-9]{5}$"
        },
        "zip4": {
          "type": "string",
          "pattern": "^[0-9]{4}$"
        }
      }
    },
    "party": {
      "type": "object",
      "required": [
        "name",
        "address"
      ],
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string",
          "maxLength": 70
        },
        "address": {
          "$ref": "#/$defs/foreign_address"
        },
        "phone": {
          "type": "string",
          "maxLength": 25
        },
        "email": {
          "type": "string",
          "format": "email"
        }
      }
    },
    "addressee": {
      "type": "object",
      "required": [
        "name",
        "address"
      ],
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string",
          "maxLength": 70
        },
        "address": {
          "$ref": "#/$defs/us_address"
        },
        "phone": {
          "type": "string",
          "maxLength": 25
        },
        "email": {
          "type": "string",
          "format": "email"
        }
      }
    },
    "content": {
      "type": "object",
      "required": [
        "description",
        "quantity",
        "net_weight_kg",
        "value",
        "hs_code",
        "origin_country"
      ],
      "additionalProperties": false,
      "properties": {
        "description": {
          "type": "string",
          "minLength": 3,
          "maxLength": 200,
          "description": "Detailed description of the article (CN 23 box: quantity and detailed description of contents)."
        },
        "quantity": {
          "type": "integer",
          "minimum": 1
        },
        "net_weight_kg": {
          "type": "number",
          "exclusiveMinimum": 0
        },
        "value": {
          "$ref": "#/$defs/money"
        },
        "hs_code": {
          "type": "string",
          "pattern": "^[0-9]{6,10}$",
          "description": "HS tariff number of the article, 6 to 10 digits, digits only (CN 23: HS tariff number). Mandatory. Allied checks it and assigns the 10-digit HTSUS number and the Chapter 99 numbers."
        },
        "origin_country": {
          "$ref": "#/$defs/country",
          "description": "Country where the goods originated."
        },
        "sku": {
          "type": "string",
          "maxLength": 40
        }
      }
    },
    "item": {
      "type": "object",
      "required": [
        "item_id",
        "nature_code",
        "sender",
        "addressee",
        "contents",
        "currency",
        "gross_weight_kg"
      ],
      "additionalProperties": false,
      "properties": {
        "item_id": {
          "$ref": "#/$defs/s10"
        },
        "nature_code": {
          "enum": [
            11,
            111,
            21,
            31,
            32,
            91,
            999
          ],
          "description": "UPU code list 136 (item nature): 11 sale of goods, 111 commercial sale of goods (B2B), 21 returned goods, 31 gift, 32 commercial sample, 91 documents, 999 other."
        },
        "sender": {
          "$ref": "#/$defs/party"
        },
        "addressee": {
          "$ref": "#/$defs/addressee"
        },
        "contents": {
          "type": "array",
          "minItems": 1,
          "maxItems": 99,
          "items": {
            "$ref": "#/$defs/content"
          }
        },
        "currency": {
          "type": "string",
          "pattern": "^[A-Z]{3}$",
          "description": "ISO 4217 currency of the values."
        },
        "gross_weight_kg": {
          "type": "number",
          "exclusiveMinimum": 0
        },
        "postage_paid": {
          "$ref": "#/$defs/money"
        },
        "insurance": {
          "$ref": "#/$defs/money"
        },
        "invoice_number": {
          "type": "string",
          "maxLength": 35
        },
        "licence_numbers": {
          "type": "array",
          "items": {
            "type": "string",
            "maxLength": 35
          }
        },
        "certificate_numbers": {
          "type": "array",
          "items": {
            "type": "string",
            "maxLength": 35
          }
        },
        "receptacle_id": {
          "type": "string",
          "maxLength": 29,
          "description": "UPU S9 receptacle identifier of the bag that holds the item."
        }
      }
    },
    "dispatch": {
      "type": "object",
      "additionalProperties": false,
      "description": "Optional dispatch data. It tells Allied the carrier, the U.S. port and the arrival date for the whole batch.",
      "properties": {
        "dispatch_id": {
          "type": "string",
          "maxLength": 20
        },
        "mode": {
          "enum": [
            "AIR",
            "TRUCK",
            "OCEAN",
            "RAIL"
          ]
        },
        "carrier": {
          "type": "string",
          "maxLength": 35
        },
        "flight_or_trip": {
          "type": "string",
          "maxLength": 20
        },
        "us_port_code": {
          "type": "string",
          "pattern": "^[0-9]{4}$",
          "description": "CBP port code of arrival, for example 4701 (JFK)."
        },
        "expected_arrival_date": {
          "type": "string",
          "format": "date"
        }
      }
    }
  }
}
