{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://w3id.org/restep/1/schema/discovery.json",
  "title": "Restep 1 discovery document",
  "description": "The document a GET on the API root returns (section 4.2), as of draft 6. Members a server adds outside Restep start with x- (4.2); clients ignore what they do not know. Rules that relate one member to another (a collation on a declared string field, a named filter that shares no name with a filter, a subcollection path that starts with its parent's) are checked by restep-check, not by this schema.",
  "type": "object",
  "required": ["restep"],
  "properties": {
    "restep": {
      "type": "string",
      "pattern": "^1\\.(0|[1-9][0-9]*)(-(draft|rc)\\.(0|[1-9][0-9]*))?$",
      "description": "Full Restep version, without leading zeros: 1.0 for a published version, 1.0-draft.6 or 1.0-rc.1 for a draft or a candidate of it (1.1).",
      "examples": ["1.0", "1.0-draft.6", "1.0-rc.1", "1.1-draft.2"]
    },
    "version": { "type": "string", "pattern": "^[1-9][0-9]*$", "description": "Major version of this API, the n in /vn/ (5.5)." },
    "profiles": {
      "type": "array", "uniqueItems": true,
      "items": { "type": "string", "examples": ["core", "query", "writes", "cache", "relations", "sync"] }
    },
    "extensions": {
      "type": "array", "uniqueItems": true,
      "items": { "type": "string", "examples": ["actions", "batch", "filter-expr", "query-method", "oauth", "inherit"] }
    },
    "batch": {
      "type": "object", "required": ["path"],
      "properties": {
        "path": { "$ref": "#/$defs/path" },
        "max_operations": { "type": "integer", "minimum": 1 }
      }
    },
    "idempotency": {
      "type": "object",
      "properties": {
        "ttl": { "type": "integer", "minimum": 86400, "description": "Seconds a key is kept (7.1)." },
        "anonymous": { "type": "boolean", "description": "false: requests without credentials that carry Idempotency-Key get 400 bad_request; missing means true (7.1)." }
      }
    },
    "events": { "$ref": "#/$defs/path", "description": "Server-Sent Events path, relative to the API root (10)." },
    "status": { "$ref": "#/$defs/link", "description": "Link to the service status resource (4.4)." },
    "collections": {
      "type": "object",
      "propertyNames": { "$ref": "#/$defs/name" },
      "additionalProperties": { "$ref": "#/$defs/collection" }
    },
    "singletons": {
      "type": "object",
      "description": "Resources that are neither a collection nor an item of one, keyed by a name of the API's choosing (5.9).",
      "propertyNames": { "$ref": "#/$defs/name" },
      "additionalProperties": { "$ref": "#/$defs/singleton" }
    },
    "deprecations": {
      "type": "array",
      "items": { "$ref": "#/$defs/deprecation", "properties": { "kind": { "enum": ["version", "profile", "extension", "collection", "singleton", "reason"] } } },
      "description": "Retirement notices of the version, a profile, an extension, a collection, a singleton or a reason (5.5)."
    },
    "reasons": {
      "type": "object",
      "propertyNames": { "$ref": "#/$defs/reason_name" },
      "additionalProperties": { "$ref": "#/$defs/reason" },
      "description": "The domain reasons the API can give, keyed by reason; if declared, complete (5.4)."
    }
  },
  "patternProperties": { "^x-": true },
  "additionalProperties": false,
  "allOf": [
    {
      "if": { "properties": { "profiles": { "contains": { "const": "sync" } } }, "required": ["profiles"] },
      "then": { "properties": { "profiles": { "contains": { "const": "query" } } } }
    },
    {
      "if": { "properties": { "extensions": { "contains": { "enum": ["filter-expr", "query-method"] } } }, "required": ["extensions"] },
      "then": { "required": ["profiles"], "properties": { "profiles": { "contains": { "const": "query" } } } }
    },
    {
      "if": { "properties": { "extensions": { "contains": { "const": "batch" } } }, "required": ["extensions"] },
      "then": { "required": ["batch"] }
    },
    {
      "description": "A collection declares inherit only if extensions lists it (11.6).",
      "anyOf": [
        { "properties": { "collections": { "additionalProperties": { "not": { "required": ["inherit"] } } } } },
        { "required": ["extensions"], "properties": { "extensions": { "contains": { "const": "inherit" } } } }
      ]
    }
  ],
  "$defs": {
    "name": {
      "type": "string", "pattern": "^[A-Za-z][A-Za-z0-9_-]{0,63}$",
      "description": "A name the API gives: name = ALPHA *63( ALPHA / DIGIT / \"_\" / \"-\" ) (3). It never starts with _, which Restep reserves."
    },
    "field_path": {
      "type": "string", "pattern": "^[A-Za-z][A-Za-z0-9_-]{0,63}(\\.[A-Za-z][A-Za-z0-9_-]{0,63})*$",
      "description": "A field or a dotted path: names joined by ., whose last segment can be a lowercase language tag (name.es-mx) (3, 6.1)."
    },
    "reason_name": {
      "type": "string", "pattern": "^[a-z][a-z0-9_]{0,63}$",
      "description": "A reason: snake_case (5.4) and a name (3)."
    },
    "reason": {
      "type": "object", "required": ["code"],
      "properties": {
        "code": { "type": "string", "pattern": "^[a-z][a-z0-9_]*$", "description": "The code this reason comes with (5.4)." },
        "description": { "$ref": "#/$defs/description" }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false
    },
    "path": {
      "type": "string", "pattern": "^/",
      "description": "A path relative to the API root, starting with / (2). A collection path MAY be a URI template (RFC 6570)."
    },
    "template_path": {
      "allOf": [{ "$ref": "#/$defs/path" }],
      "pattern": "^/(?:[^{}]|\\{(?:[A-Za-z0-9_]|%[0-9A-Fa-f]{2})+(?:\\.(?:[A-Za-z0-9_]|%[0-9A-Fa-f]{2})+)*\\})*$",
      "not": { "pattern": "\\{([^}]+)\\}.*\\{\\1\\}" },
      "description": "A collection path: a path whose template variables, if any, are simple strings ({name}) with distinct names (4.2)."
    },
    "link": {
      "type": "string", "pattern": "^(/(?!/)|https?:)",
      "description": "A link in a body: an absolute path that includes the API root's path, or an absolute URI whose scheme is https or the API root's (3)."
    },
    "type": { "enum": ["string", "integer", "number", "decimal", "boolean", "date", "datetime", "duration", "enum"] },
    "op": { "enum": ["eq", "ne", "lt", "le", "gt", "ge", "in", "null"] },
    "description": { "type": "string", "minLength": 1, "description": "A short plain-text explanation, one sentence; data, never instructions (4.3)." },
    "schema": {
      "oneOf": [
        { "type": "object", "description": "A JSON Schema 2020-12 object." },
        { "$ref": "#/$defs/link", "description": "A link to one (3)." }
      ]
    },
    "method": { "enum": ["GET", "POST", "PATCH", "DELETE"] },
    "methods": { "type": "array", "items": { "$ref": "#/$defs/method" }, "uniqueItems": true },
    "filter": {
      "type": "object", "required": ["type", "ops"],
      "properties": {
        "type": { "$ref": "#/$defs/type" },
        "values": { "type": "array", "items": { "type": "string" }, "minItems": 1 },
        "description": { "$ref": "#/$defs/description" },
        "ops": { "type": "array", "items": { "$ref": "#/$defs/op" }, "minItems": 1, "uniqueItems": true }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false,
      "allOf": [
        { "if": { "properties": { "type": { "const": "enum" } } }, "then": { "required": ["values"] } },
        { "if": { "properties": { "type": { "const": "boolean" } } },
          "then": { "properties": { "ops": { "items": { "enum": ["eq", "ne", "null"] } } } } }
      ]
    },
    "named_filter": {
      "type": "object", "required": ["description"],
      "description": "A filter by name that does not compare a field (6.1): a switch without type, or one that takes a value of its type.",
      "properties": {
        "type": { "$ref": "#/$defs/type" },
        "values": { "type": "array", "items": { "type": "string" }, "minItems": 1 },
        "description": { "$ref": "#/$defs/description", "description": "Which items it returns; it is what gives the filter its meaning." }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false,
      "if": { "properties": { "type": { "const": "enum" } }, "required": ["type"] }, "then": { "required": ["values"] }
    },
    "relation": {
      "type": "object", "required": ["to"],
      "properties": {
        "to": { "oneOf": [{ "$ref": "#/$defs/name" }, { "type": "array", "items": { "$ref": "#/$defs/name" }, "minItems": 2, "uniqueItems": true }] },
        "many": { "type": "boolean" },
        "include": { "type": "boolean" },
        "path": { "$ref": "#/$defs/template_path", "description": "The template of the subcollection it leads to (9)." },
        "description": { "$ref": "#/$defs/description" }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false
    },
    "action": {
      "type": "object", "required": ["on", "safe", "idempotent"],
      "properties": {
        "on": { "enum": ["item", "collection"] },
        "safe": { "type": "boolean" },
        "idempotent": { "type": "boolean" },
        "method": { "enum": ["GET", "POST"], "description": "POST unless declared; only safe actions may use GET (11.1)." },
        "description": { "$ref": "#/$defs/description" },
        "input": { "$ref": "#/$defs/schema", "description": "Schema of the action's body (4.3)." }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false,
      "if": { "required": ["method"], "properties": { "method": { "const": "GET" } } },
      "then": { "properties": { "safe": { "const": true } }, "not": { "required": ["input"] } }
    },
    "inherit": {
      "type": "object", "required": ["own"],
      "properties": {
        "own": { "$ref": "#/$defs/name", "description": "The own field, written; null means it inherits." },
        "from": {
          "type": "array", "uniqueItems": true,
          "items": { "$ref": "#/$defs/name" },
          "description": "To-one relationships this step inherits from, in order."
        },
        "default": { "type": "boolean", "description": "There is a default at the end of the chain." },
        "description": { "$ref": "#/$defs/description" }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false
    },
    "language_map": {
      "type": "object",
      "description": "Left for minor versions (4.2): today only x- members.",
      "patternProperties": { "^x-": true },
      "additionalProperties": false
    },
    "deprecation": {
      "type": "object", "required": ["kind", "announced"],
      "description": "A retirement notice (5.5). Where it goes decides which kinds it can have: the root, a collection or a singleton.",
      "properties": {
        "kind": { "enum": ["version", "profile", "extension", "collection", "singleton", "reason",
          "field", "filter", "operator", "value", "sort", "relation", "include", "count", "max_limit", "action", "method", "item_method"] },
        "name": { "type": "string", "minLength": 1, "description": "What is retired: a name, a field, a filter field or a method, as the kind says." },
        "op": { "$ref": "#/$defs/op", "description": "The operator, for kind operator." },
        "value": { "description": "The enum value (value), the lower limit (max_limit) or \"estimated\" (count)." },
        "announced": { "type": "string", "format": "date-time", "description": "When the notice was first published; it never changes." },
        "sunset": { "type": "string", "format": "date-time", "description": "When it is removed, once there is a date." }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false,
      "allOf": [
        { "if": { "properties": { "kind": { "enum": ["version", "count", "max_limit"] } } },
          "else": { "required": ["name"] } },
        { "if": { "properties": { "kind": { "const": "operator" } } }, "then": { "required": ["op"] } },
        { "if": { "properties": { "kind": { "const": "value" } } }, "then": { "required": ["value"], "properties": { "value": { "type": "string" } } } },
        { "if": { "properties": { "kind": { "const": "max_limit" } } }, "then": { "required": ["value"], "properties": { "value": { "type": "integer", "minimum": 1 } } } },
        { "if": { "properties": { "kind": { "const": "count" } } }, "then": { "properties": { "value": { "const": "estimated" } } } },
        { "if": { "properties": { "kind": { "enum": ["method", "item_method"] } } }, "then": { "properties": { "name": { "$ref": "#/$defs/method" } } } },
        { "if": { "properties": { "kind": { "enum": ["collection", "singleton", "relation", "action", "reason"] } } }, "then": { "properties": { "name": { "$ref": "#/$defs/name" } } } },
        { "if": { "properties": { "kind": { "enum": ["field", "filter", "operator", "value", "sort", "include"] } } }, "then": { "properties": { "name": { "$ref": "#/$defs/field_path" } } } }
      ]
    },
    "singleton": {
      "type": "object", "required": ["path"],
      "properties": {
        "path": { "$ref": "#/$defs/path", "not": { "pattern": "[{}]" }, "description": "Never a template, nor a path that a collection or an item of one has or could have (5.9)." },
        "description": { "$ref": "#/$defs/description" },
        "methods": {
          "type": "array", "uniqueItems": true, "items": { "enum": ["GET", "PATCH"] }, "contains": { "const": "GET" },
          "description": "Exact, as in 4.3; a singleton always supports GET (5.9)."
        },
        "schemas": {
          "type": "object",
          "properties": {
            "item": { "$ref": "#/$defs/schema", "description": "Its full representation." },
            "update": { "$ref": "#/$defs/schema", "description": "The body of a PATCH, a merge patch." }
          },
          "patternProperties": { "^x-": true },
          "additionalProperties": false
        },
        "deprecations": {
          "type": "array",
          "items": { "$ref": "#/$defs/deprecation", "properties": { "kind": { "enum": ["field", "method"] }, "name": { "type": "string" } },
            "if": { "properties": { "kind": { "const": "method" } } }, "then": { "properties": { "name": { "enum": ["GET", "PATCH"] } } } }
        }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false
    },
    "collection": {
      "type": "object", "required": ["path"],
      "properties": {
        "path": { "$ref": "#/$defs/template_path" },
        "parent": { "$ref": "#/$defs/name", "description": "The collection whose items a subcollection hangs from; required when path is a template (4.2)." },
        "description": { "$ref": "#/$defs/description" },
        "methods": { "$ref": "#/$defs/methods", "description": "Methods the collection supports (4.3); absent means unknown." },
        "item_methods": { "$ref": "#/$defs/methods", "description": "Methods its items support (4.3); absent means unknown." },
        "schemas": {
          "type": "object",
          "properties": {
            "item": { "$ref": "#/$defs/schema", "description": "The full representation of an item." },
            "create": { "$ref": "#/$defs/schema", "description": "The body of a POST." },
            "update": { "$ref": "#/$defs/schema", "description": "The body of a PATCH, a merge patch." }
          },
          "patternProperties": { "^x-": true },
          "additionalProperties": false
        },
        "id_from": { "enum": ["server", "client", "either"], "description": "Who chooses an item's id; missing means server (5.1)." },
        "filter": {
          "type": "object",
          "propertyNames": { "$ref": "#/$defs/field_path" },
          "additionalProperties": { "$ref": "#/$defs/filter" },
          "properties": {
            "id": { "$ref": "#/$defs/filter", "properties": { "ops": { "items": { "enum": ["eq", "ne", "in", "null"] } } }, "description": "The order of ids is the server's: no lt, le, gt or ge (6.1)." }
          }
        },
        "named_filters": {
          "type": "object",
          "description": "Filters by name (6.1); none shares its name with a key of filter.",
          "propertyNames": { "$ref": "#/$defs/name" },
          "additionalProperties": { "$ref": "#/$defs/named_filter" }
        },
        "sort": { "type": "array", "items": { "$ref": "#/$defs/field_path" }, "uniqueItems": true },
        "collations": {
          "type": "object",
          "description": "How a string field or a translation field.tag is compared and sorted (6.1); a field not listed is codepoint.",
          "propertyNames": { "$ref": "#/$defs/field_path", "not": { "const": "id" } },
          "additionalProperties": { "enum": ["codepoint", "server"] }
        },
        "max_limit": { "type": "integer", "minimum": 1 },
        "count": { "enum": ["exact", "estimated"] },
        "relations": {
          "type": "object",
          "propertyNames": { "$ref": "#/$defs/name" },
          "additionalProperties": { "$ref": "#/$defs/relation" }
        },
        "language_maps": {
          "type": "object",
          "description": "Fields of the collection that are language maps (3), all of them; absent means none.",
          "propertyNames": { "$ref": "#/$defs/name" },
          "additionalProperties": { "$ref": "#/$defs/language_map" }
        },
        "inherit": {
          "type": "object",
          "description": "Inherited fields (11.6): each key is an effective, read-only field.",
          "propertyNames": { "$ref": "#/$defs/name" },
          "additionalProperties": { "$ref": "#/$defs/inherit" }
        },
        "include_depth": { "type": "integer", "minimum": 1 },
        "sync": {
          "type": "object",
          "description": "Declares the collection synced, {} if it has nothing to say; only synced collections answer :changes (10).",
          "properties": { "retention": { "type": "integer", "minimum": 0, "description": "Seconds changes are kept (10)." } }
        },
        "actions": {
          "type": "object",
          "propertyNames": { "$ref": "#/$defs/name", "not": { "const": "changes" } },
          "additionalProperties": { "$ref": "#/$defs/action" }
        },
        "deprecations": {
          "type": "array",
          "items": { "$ref": "#/$defs/deprecation", "properties": { "kind": { "enum": ["field", "filter", "operator", "value", "sort", "relation", "include", "count", "max_limit", "action", "method", "item_method"] } } }
        }
      },
      "patternProperties": { "^x-": true },
      "additionalProperties": false,
      "allOf": [
        {
          "if": { "required": ["sync"] },
          "then": {
            "required": ["filter"],
            "properties": { "filter": { "required": ["id"], "properties": { "id": { "properties": { "ops": { "contains": { "const": "in" } } } } } } }
          }
        },
        {
          "if": { "required": ["path"], "properties": { "path": { "pattern": "\\{" } } },
          "then": { "required": ["parent"] }
        }
      ]
    }
  }
}
