{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://geodesix-publishers.pages.dev/schemas/affiliate.schema.json",
  "title": "Affiliate file",
  "description": "Served at /.well-known/affiliate.json. Says who the organization is, which affiliate networks its links run on, how an agent gets access, and where its affiliate resources are.",
  "type": "object",
  "required": [
    "protocol_version",
    "organization",
    "resources"
  ],
  "properties": {
    "protocol_version": {
      "type": "string",
      "pattern": "^\\d+\\.\\d+$",
      "description": "The version of this format the file follows, as major.minor. Readers refuse a major version they do not know and accept any minor version."
    },
    "organization": {
      "$ref": "#/$defs/organization"
    },
    "updated": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}$",
      "description": "When this file last changed."
    },
    "networks": {
      "type": "array",
      "maxItems": 20,
      "items": {
        "$ref": "#/$defs/network"
      },
      "description": "The affiliate networks this organization's links run on."
    },
    "access": {
      "$ref": "#/$defs/access"
    },
    "resources": {
      "type": "array",
      "maxItems": 50,
      "items": {
        "$ref": "#/$defs/resource"
      },
      "description": "The affiliate files this organization publishes, one entry per file."
    }
  },
  "$defs": {
    "organization": {
      "type": "object",
      "description": "The organization the file speaks for.",
      "required": [
        "name",
        "domain"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 120,
          "description": "Display name."
        },
        "domain": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{1,80}\\.[a-z]{2,}$",
          "description": "The organization's own domain. For a file at /.well-known/affiliate.json this must be the host that serves it, or the host it stands in for."
        },
        "role": {
          "type": "string",
          "enum": [
            "publisher",
            "creator",
            "retailer",
            "network",
            "other"
          ],
          "description": "What the organization does in an affiliate relationship."
        },
        "group": {
          "type": "string",
          "maxLength": 120,
          "description": "The parent company or media group, when there is one."
        },
        "logo_uri": {
          "type": "string",
          "pattern": "^https://",
          "maxLength": 2048
        }
      }
    },
    "network": {
      "type": "object",
      "description": "An affiliate network the organization's links run on.",
      "required": [
        "name",
        "click_id_parameter"
      ],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9-]{1,80}$",
          "description": "A short name for the network, unique in this file."
        },
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 120,
          "description": "The network's name, such as impact.com."
        },
        "domain": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{1,80}\\.[a-z]{2,}$",
          "description": "A registrable host name in lower case, without scheme or path."
        },
        "click_id_parameter": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_]{1,40}$",
          "description": "The URL query parameter the network puts its click id in on the landing URL, such as irclickid."
        }
      }
    },
    "access": {
      "type": "object",
      "description": "How a reader gets the key for resources marked access: key.",
      "required": [
        "type"
      ],
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "bearer"
          ],
          "description": "bearer: send the key as Authorization: Bearer <key>."
        },
        "terms": {
          "type": "string",
          "maxLength": 500,
          "description": "What a reader agrees to in exchange for a key, in plain words."
        },
        "terms_url": {
          "type": "string",
          "pattern": "^https://",
          "maxLength": 2048
        },
        "request_url": {
          "type": "string",
          "pattern": "^https://",
          "maxLength": 2048,
          "description": "Where to ask for a key."
        }
      }
    },
    "resource": {
      "type": "object",
      "description": "One affiliate file.",
      "required": [
        "type",
        "url"
      ],
      "properties": {
        "type": {
          "type": "string",
          "pattern": "^([a-z][a-z0-9_]{1,60}|[a-z0-9-]+(\\.[a-z0-9_-]+){2,})$",
          "description": "What kind of file this is. Types this format defines are plain names (product_references). Anyone may add a type under a reverse domain name they control (com.example.deals)."
        },
        "url": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2048,
          "description": "An absolute https URL, or a URL relative to the file that holds it."
        },
        "access": {
          "type": "string",
          "enum": [
            "public",
            "key"
          ],
          "default": "public",
          "description": "Whether the file at url needs the key."
        },
        "description": {
          "type": "string",
          "maxLength": 300
        },
        "version": {
          "type": "string",
          "pattern": "^\\d+\\.\\d+$",
          "description": "The version of this format the file follows, as major.minor. Readers refuse a major version they do not know and accept any minor version."
        },
        "schema": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2048,
          "description": "The JSON Schema the file follows."
        },
        "spec": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2048,
          "description": "The written specification for this type."
        },
        "hosted_by": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{1,80}\\.[a-z]{2,}$",
          "description": "The domain that serves the file, when it is not the organization's own (a service hosting it on the organization's behalf)."
        },
        "updated": {
          "type": "string",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$",
          "description": "A calendar date, YYYY-MM-DD."
        }
      }
    }
  }
}
