{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kabinethq.com/protocol/1/resource.schema.json",
  "title": "Resource",
  "description": "`GET /panel/resources/{r}/schema`: the resource as this user may see it.",
  "$defs": {
    "ResourceSchema": {
      "title": "ResourceSchema",
      "type": "object",
      "required": ["version", "resource", "label", "can", "form", "table", "actions", "form_mode", "pages"],
      "properties": {
        "version": { "const": 1 },
        "resource": { "type": "string", "minLength": 1 },
        "label": { "type": "string" },
        "can": { "$ref": "#/$defs/ResourceAbilities" },
        "form": { "type": "array", "items": { "$ref": "fields.schema.json#/$defs/FormField" } },
        "table": { "$ref": "#/$defs/TableSchema" },
        "actions": { "type": "array", "items": { "$ref": "#/$defs/Action" } },
        "form_mode": { "$ref": "#/$defs/FormMode" },
        "infolist": { "type": "array", "items": { "$ref": "fields.schema.json#/$defs/Entry" } },
        "pages": { "$ref": "#/$defs/ResourcePages" }
      },
      "additionalProperties": false,
      "$comment": "A resource has a view page exactly when it has an infolist.",
      "if": { "required": ["infolist"] },
      "then": { "properties": { "pages": { "type": "object", "required": ["view"] } } },
      "else": { "properties": { "pages": { "type": "object", "not": { "required": ["view"] } } } }
    },
    "ResourceAbilities": {
      "title": "ResourceAbilities",
      "description": "What the user may do to the resource as a whole.",
      "type": "object",
      "required": ["create"],
      "properties": { "create": { "type": "boolean" } },
      "additionalProperties": false
    },
    "TableSchema": {
      "title": "TableSchema",
      "type": "object",
      "required": ["columns", "filters", "searchable", "per_page"],
      "properties": {
        "columns": { "type": "array", "items": { "$ref": "fields.schema.json#/$defs/Column" } },
        "filters": { "type": "array", "items": { "$ref": "fields.schema.json#/$defs/Filter" } },
        "searchable": { "type": "boolean" },
        "per_page": { "type": "integer", "minimum": 1 }
      },
      "additionalProperties": false
    },
    "Action": {
      "title": "Action",
      "description": "`standalone` runs without records; actions of one `group` share a dropdown; `destructive` gets a red button.",
      "type": "object",
      "required": ["name", "label", "target"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "label": { "type": "string" },
        "target": { "enum": ["record", "bulk", "standalone"] },
        "group": { "type": "string", "minLength": 1 },
        "confirm": { "type": "string", "minLength": 1 },
        "destructive": { "const": true },
        "queued": { "const": true },
        "form": { "type": "array", "minItems": 1, "items": { "$ref": "fields.schema.json#/$defs/FormField" } }
      },
      "additionalProperties": false
    },
    "FormMode": {
      "title": "FormMode",
      "description": "How the create / edit form opens; the resource decides, not the user.",
      "enum": ["page", "modal", "drawer"]
    },
    "ResourcePages": {
      "title": "ResourcePages",
      "description": "The resource's default block trees, filtered for this user like any page.",
      "type": "object",
      "required": ["index", "create", "edit"],
      "properties": {
        "index": { "$ref": "page.schema.json#/$defs/Layout" },
        "create": { "$ref": "page.schema.json#/$defs/Layout" },
        "edit": { "$ref": "page.schema.json#/$defs/Layout" },
        "view": { "$ref": "page.schema.json#/$defs/Layout" }
      },
      "additionalProperties": false
    }
  }
}
