{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://ledigt.org/schema/1.0/listing.schema.json",
  "title": "ledigt.org 1.0 listing",
  "description": "One available (or recently let) rental apartment.",
  "type": "object",
  "required": ["id", "status", "updated"],
  "properties": {
    "id": { "type": "string", "minLength": 1, "maxLength": 100, "description": "Stable identifier, unique within the publisher." },
    "status": { "enum": ["available", "reserved", "let"] },
    "published": { "$ref": "#/$defs/dateOrDateTime" },
    "updated": { "$ref": "#/$defs/dateOrDateTime" },
    "url": { "$ref": "#/$defs/url", "description": "The listing page on the publisher's website." },
    "applyUrl": { "$ref": "#/$defs/url" },
    "applicationMethod": { "enum": ["firstCome", "queue", "lottery", "selection", "contact"] },
    "applicationDeadline": { "$ref": "#/$defs/dateOrDateTime" },
    "title": { "type": "string", "minLength": 1, "maxLength": 200 },
    "description": { "type": "string", "maxLength": 20000, "description": "Plain text. Use \\n for line breaks. No HTML." },
    "address": { "$ref": "#/$defs/address" },
    "geo": { "$ref": "#/$defs/geo" },
    "area": { "type": "number", "exclusiveMinimum": 0, "description": "Square metres." },
    "rent": { "$ref": "#/$defs/rent" },
    "availableFrom": { "$ref": "#/$defs/date", "description": "Omitted = by agreement." },
    "building": { "$ref": "#/$defs/building" },
    "media": { "type": "array", "items": { "$ref": "#/$defs/media" } },
    "contact": { "$ref": "#/$defs/contact" },
    "viewing": { "$ref": "#/$defs/viewing" },
    "residential": { "$ref": "#/$defs/residential" }
  },
  "patternProperties": { "^x-": true },
  "additionalProperties": false,
  "if": { "properties": { "status": { "const": "let" } } },
  "else": {
    "required": ["url", "title", "address", "rent"],
    "$comment": "Exactly one category block. 1.0 has one category; a later version adds its block here.",
    "oneOf": [
      { "required": ["residential"] }
    ]
  },
  "$defs": {
    "date": {
      "type": "string",
      "format": "date",
      "pattern": "^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$"
    },
    "dateOrDateTime": {
      "type": "string",
      "description": "YYYY-MM-DD or ISO 8601 date-time with time zone.",
      "pattern": "^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])(T([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9](\\.[0-9]+)?)?(Z|[+-]([01][0-9]|2[0-3]):[0-5][0-9]))?$"
    },
    "url": { "type": "string", "format": "uri", "pattern": "^https?://[^\\s]+$", "maxLength": 2000 },
    "money": { "type": "number", "minimum": 0 },
    "currency": { "type": "string", "pattern": "^[A-Z]{3}$" },
    "featureNotes": {
      "type": "object",
      "description": "Free text per feature, e.g. {\"balcony\": \"Glazed, facing south\"}.",
      "additionalProperties": { "type": "string", "maxLength": 500 }
    },
    "address": {
      "type": "object",
      "required": ["street", "city"],
      "properties": {
        "street": { "type": "string", "minLength": 1, "maxLength": 200 },
        "postalCode": { "type": "string", "maxLength": 20 },
        "city": { "type": "string", "minLength": 1, "maxLength": 100 },
        "district": { "type": "string", "maxLength": 100 },
        "country": { "type": "string", "pattern": "^[A-Z]{2}$", "default": "SE" }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false
    },
    "geo": {
      "type": "object",
      "required": ["lat", "lon"],
      "properties": {
        "lat": { "type": "number", "minimum": -90, "maximum": 90 },
        "lon": { "type": "number", "minimum": -180, "maximum": 180 }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false
    },
    "utilityState": { "enum": ["included", "metered", "flatRate", "separate", "notAvailable"] },
    "rent": {
      "type": "object",
      "properties": {
        "amount": { "$ref": "#/$defs/money" },
        "onRequest": { "type": "boolean" },
        "currency": { "$ref": "#/$defs/currency", "default": "SEK" },
        "period": { "enum": ["month", "year"], "default": "month" },
        "vat": { "type": "boolean", "description": "true = VAT is added on top of amount." },
        "utilities": {
          "type": "object",
          "propertyNames": {
            "enum": ["heating", "hotWater", "coldWater", "electricity", "cooling", "internet", "tv", "waste", "laundry", "parking", "storage"]
          },
          "additionalProperties": { "$ref": "#/$defs/utilityState" }
        },
        "extraCosts": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["item", "amount"],
            "properties": {
              "item": { "type": "string", "minLength": 1, "maxLength": 100 },
              "amount": { "$ref": "#/$defs/money" },
              "period": { "enum": ["month", "year", "once"], "default": "month" },
              "mandatory": { "type": "boolean" },
              "estimate": { "type": "boolean" }
            },
            "patternProperties": { "^x-": true },
            "additionalProperties": false
          }
        },
        "deposit": { "$ref": "#/$defs/money" }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false,
      "anyOf": [
        { "required": ["amount"] },
        { "required": ["onRequest"], "properties": { "onRequest": { "const": true } } }
      ],
      "not": { "required": ["amount", "onRequest"], "properties": { "onRequest": { "const": true } } }
    },
    "building": {
      "type": "object",
      "properties": {
        "name": { "type": "string", "maxLength": 200 },
        "yearBuilt": { "type": "integer", "minimum": 1000, "maximum": 2200 },
        "energyClass": { "enum": ["A", "B", "C", "D", "E", "F", "G"] },
        "elevator": { "type": "boolean" }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false
    },
    "media": {
      "type": "object",
      "required": ["url"],
      "properties": {
        "url": { "$ref": "#/$defs/url" },
        "type": { "enum": ["photo", "floorplan", "tour360", "video"], "default": "photo" },
        "caption": { "type": "string", "maxLength": 500 },
        "width": { "type": "integer", "minimum": 1 },
        "height": { "type": "integer", "minimum": 1 }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false
    },
    "contact": {
      "type": "object",
      "properties": {
        "name": { "type": "string", "maxLength": 200 },
        "email": { "type": "string", "format": "email", "pattern": "^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$" },
        "phone": { "type": "string", "maxLength": 40 }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false
    },
    "viewing": {
      "type": "object",
      "properties": {
        "date": { "$ref": "#/$defs/dateOrDateTime" },
        "required": { "type": "boolean" },
        "virtual": { "type": "boolean" }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false
    },
    "residential": {
      "type": "object",
      "required": ["rooms"],
      "properties": {
        "rooms": { "type": "number", "exclusiveMinimum": 0, "multipleOf": 0.5 },
        "floor": { "type": "integer", "minimum": -10, "maximum": 200 },
        "floorLabel": { "type": "string", "maxLength": 50 },
        "floorsInBuilding": { "type": "integer", "minimum": 1, "maximum": 200 },
        "contractType": { "enum": ["firstHand", "secondHand", "shortTerm"] },
        "contractEnd": { "$ref": "#/$defs/date" },
        "housingType": { "enum": ["regular", "student", "senior", "youth", "serviced"] },
        "ageMin": { "type": "integer", "minimum": 0, "maximum": 120 },
        "ageMax": { "type": "integer", "minimum": 0, "maximum": 120 },
        "requirements": {
          "type": "object",
          "properties": {
            "incomeMultiple": { "type": "number", "exclusiveMinimum": 0 },
            "incomeTypes": {
              "type": "array",
              "uniqueItems": true,
              "items": { "enum": ["employment", "pension", "studentAid", "benefits"] }
            },
            "noPaymentRemarks": { "type": "boolean" },
            "references": { "type": "boolean" },
            "maxOccupants": { "type": "integer", "minimum": 1 },
            "text": { "type": "string", "maxLength": 2000 }
          },
          "patternProperties": { "^x-": true },
          "additionalProperties": false
        },
        "furnished": { "type": "boolean" },
        "petsAllowed": { "type": "boolean" },
        "smokingAllowed": { "type": "boolean" },
        "accessible": { "type": "boolean" },
        "features": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "enum": ["balcony", "patio", "elevator", "dishwasher", "washingMachine", "tumbleDryer", "laundryRoom", "storage", "securityDoor", "bathtub", "shower", "fireplace", "fibre", "parkingAvailable", "bikeStorage"]
          }
        },
        "featureNotes": { "$ref": "#/$defs/featureNotes" }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false
    }
  }
}
