{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://www.arlandaexpress.com/schemas/ticket-prices.v1.json",
  "title": "Arlanda Express ticket prices",
  "description": "Public Arlanda Express ticket prices and the conditions they apply under. Prices are in SEK and include VAT.",
  "type": "object",
  "required": [
    "schemaVersion",
    "operator",
    "route",
    "currency",
    "vatIncluded",
    "source",
    "pricesFetchedAt",
    "fareTypes",
    "passengerCategories",
    "prices",
    "rules",
    "examples"
  ],
  "properties": {
    "$schema": { "type": "string" },
    "schemaVersion": { "const": 1 },
    "operator": { "type": "string" },
    "route": {
      "type": "object",
      "required": ["between", "bothDirections"],
      "properties": {
        "between": { "type": "array", "items": { "type": "string" }, "minItems": 2, "maxItems": 2 },
        "bothDirections": { "type": "boolean" }
      },
      "additionalProperties": false
    },
    "currency": { "const": "SEK" },
    "vatIncluded": { "const": true },
    "source": {
      "description": "shopping-api: fetched from the shopping API during the build. unavailable: the shopping API could not be reached, so there are no prices.",
      "enum": ["shopping-api", "unavailable"]
    },
    "pricesFetchedAt": {
      "description": "When the prices were fetched from the shopping API during the build. Null when source is unavailable.",
      "oneOf": [{ "$ref": "#/definitions/timestamp" }, { "type": "null" }]
    },
    "generatedAt": { "$ref": "#/definitions/timestamp" },
    "fareTypes": { "type": "array", "items": { "$ref": "#/definitions/fareType" } },
    "passengerCategories": { "type": "array", "items": { "$ref": "#/definitions/passengerCategory" } },
    "prices": { "type": "array", "items": { "$ref": "#/definitions/price" } },
    "rules": { "type": "array", "items": { "$ref": "#/definitions/rule" } },
    "examples": { "type": "array", "items": { "$ref": "#/definitions/example" } }
  },
  "additionalProperties": false,
  "definitions": {
    "timestamp": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?Z$"
    },
    "duration": {
      "description": "ISO 8601 duration, e.g. P90D or P24M.",
      "type": "string",
      "pattern": "^P(\\d+[YMWD])+$"
    },
    "localized": {
      "type": "object",
      "required": ["en", "sv"],
      "properties": {
        "en": { "type": "string", "minLength": 1 },
        "sv": { "type": "string", "minLength": 1 },
        "de": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },
    "fareTypeCode": { "enum": ["full-flex", "early-bird", "travel-pass"] },
    "passengerCategoryCode": { "enum": ["adult", "youth", "pensioner", "child"] },
    "tripType": { "enum": ["one-way", "round-trip"] },
    "fareType": {
      "type": "object",
      "required": ["code", "name", "description", "validity", "refundable", "advancePurchaseDays"],
      "properties": {
        "code": { "$ref": "#/definitions/fareTypeCode" },
        "name": { "$ref": "#/definitions/localized" },
        "description": { "$ref": "#/definitions/localized" },
        "validity": { "$ref": "#/definitions/duration" },
        "refundable": { "type": ["boolean", "null"] },
        "advancePurchaseDays": { "type": "integer", "minimum": 0 }
      },
      "additionalProperties": false
    },
    "passengerCategory": {
      "type": "object",
      "required": ["code", "name", "minAge", "maxAge"],
      "properties": {
        "code": { "$ref": "#/definitions/passengerCategoryCode" },
        "name": { "$ref": "#/definitions/localized" },
        "minAge": { "type": "integer", "minimum": 0 },
        "maxAge": { "type": ["integer", "null"], "minimum": 0 }
      },
      "additionalProperties": false
    },
    "price": {
      "type": "object",
      "required": ["id", "fareType", "passengerCategory", "tripType", "travelers", "total"],
      "properties": {
        "id": { "type": "string", "pattern": "^[a-z0-9.-]+$" },
        "fareType": { "$ref": "#/definitions/fareTypeCode" },
        "passengerCategory": {
          "description": "Null for products that are not tied to a passenger category, such as the Travel Pass.",
          "oneOf": [{ "$ref": "#/definitions/passengerCategoryCode" }, { "type": "null" }]
        },
        "tripType": { "oneOf": [{ "$ref": "#/definitions/tripType" }, { "type": "null" }] },
        "travelers": { "type": "integer", "minimum": 1 },
        "trips": { "type": "integer", "minimum": 1 },
        "total": { "type": "number", "minimum": 0 },
        "perPerson": {
          "description": "Total divided by the number of travelers. Left out for products priced per trip, such as the Travel Pass.",
          "type": "number",
          "minimum": 0
        },
        "perTrip": {
          "description": "Total divided by the number of trips. Only present for the Travel Pass.",
          "type": "number",
          "minimum": 0
        },
        "condition": { "type": "string", "pattern": "^[a-z0-9-]+$" },
        "productId": { "type": "string" }
      },
      "additionalProperties": false
    },
    "rule": {
      "type": "object",
      "required": ["code", "text"],
      "properties": {
        "code": { "type": "string", "pattern": "^[a-z0-9-]+$" },
        "text": { "$ref": "#/definitions/localized" }
      },
      "additionalProperties": false
    },
    "example": {
      "type": "object",
      "required": ["id", "fareType", "tripType", "travelers", "total"],
      "properties": {
        "id": { "type": "string", "pattern": "^[a-z0-9.-]+$" },
        "fareType": { "$ref": "#/definitions/fareTypeCode" },
        "tripType": { "$ref": "#/definitions/tripType" },
        "travelers": {
          "type": "object",
          "required": ["adult", "youth", "pensioner", "child"],
          "properties": {
            "adult": { "type": "integer", "minimum": 0 },
            "youth": { "type": "integer", "minimum": 0 },
            "pensioner": { "type": "integer", "minimum": 0 },
            "child": { "type": "integer", "minimum": 0 }
          },
          "additionalProperties": false
        },
        "total": { "type": "number", "minimum": 0 }
      },
      "additionalProperties": false
    }
  }
}
