{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://gmr.github.io/pglifecycle/schemata/view.json",
  "title": "View",
  "description": "Defines a view",
  "type": "object",
  "properties": {
    "schema": {
      "title": "Schema",
      "description": "The schema the view is created in",
      "type": "string"
    },
    "name": {
      "title": "Name",
      "description": "The view name",
      "type": "string"
    },
    "owner": {
      "title": "Owner",
      "description": "The role that owns the view",
      "type": "string"
    },
    "sql": {
      "title": "SQL Statement",
      "description": "User-provided raw SQL snippet",
      "type": "string"
    },
    "recursive": {
      "title": "Recursive View",
      "description": "Specifies the view is a recursive view",
      "type": "boolean"
    },
    "columns": {
      "title": "Column names",
      "description": "An optional list of names to be used for columns of the view. If not\ngiven, the column names are deduced from the query.\n",
      "type": "array",
      "items": {
        "anyOf": [
          {
            "type": "string"
          },
          {
            "type": "object",
            "properties": {
              "name": {
                "title": "Column Name",
                "type": "string"
              },
              "comment": {
                "title": "Comment",
                "type": "string"
              }
            },
            "required": [
              "name"
            ]
          }
        ]
      }
    },
    "check_option": {
      "title": "Check Option",
      "description": "This option controls the behavior of automatically updatable views.\nWhen this option is specified, INSERT and UPDATE commands on the\nview will be checked to ensure that new rows satisfy the view-defining\ncondition (that is, the new rows are checked to ensure that they are\nvisible through the view). If they are not, the update will be\nrejected. If the CHECK OPTION is not specified, INSERT and UPDATE\ncommands on the view are allowed to create rows that are not visible\nthrough the view.\n\nWhen it is set to LOCAL, new rows are only checked against the\nconditions defined directly in the view itself. Any conditions defined\non underlying base views are not checked (unless they also specify\nthe CHECK OPTION).\n\nWhen it is set to CASCADED, new rows are checked against the conditions\nof the view and all underlying base views. If the CHECK OPTION is\nspecified, and neither LOCAL nor CASCADED is specified, then CASCADED\nis assumed.\n",
      "enum": [
        "LOCAL",
        "CASCADED"
      ]
    },
    "security_barrier": {
      "title": "Security Barrier",
      "description": "This should be used if the view is intended to provide row-level security.\n",
      "type": "boolean"
    },
    "query": {
      "title": "Query",
      "description": "A SELECT or VALUES command which will provide the columns and rows of the view.\n",
      "type": "string"
    },
    "rules": {
      "title": "Rules",
      "description": "An array of rewrite rules on the view",
      "type": "array",
      "items": {
        "title": "Rule",
        "description": "Defines a rewrite rule on a table or view (CREATE RULE)",
        "type": "object",
        "properties": {
          "name": {
            "title": "Name",
            "description": "The rule name, unique within its table or view",
            "type": "string"
          },
          "event": {
            "title": "Event",
            "enum": [
              "SELECT",
              "INSERT",
              "UPDATE",
              "DELETE"
            ]
          },
          "condition": {
            "title": "Condition",
            "description": "The WHERE expression that decides whether the rule applies",
            "type": "string"
          },
          "instead": {
            "title": "Instead",
            "description": "Run the commands instead of the original one (DO INSTEAD); the default is DO ALSO",
            "type": "boolean"
          },
          "commands": {
            "title": "Commands",
            "description": "The commands the rule runs; absent for NOTHING",
            "type": "array",
            "items": {
              "type": "string"
            },
            "minItems": 1
          },
          "enabled": {
            "title": "Enabled",
            "description": "When the rule fires (ALTER TABLE ... RULE). ORIGIN, the default, fires it in a normal session; REPLICA only when session_replication_role is replica; ALWAYS in both; DISABLED never.\n",
            "enum": [
              "ORIGIN",
              "REPLICA",
              "ALWAYS",
              "DISABLED"
            ]
          },
          "comment": {
            "title": "Comment",
            "type": "string"
          }
        },
        "required": [
          "name",
          "event"
        ],
        "additionalProperties": false
      }
    },
    "comment": {
      "title": "Comment",
      "description": "An optional comment about the view.",
      "type": "string"
    },
    "dependencies": {
      "title": "Dependencies",
      "description": "Objects this object depends on, for the cases the dependency graph cannot infer: an ordering the topological sort would otherwise get wrong, such as a foreign key between tables or an inheritance parent.\nEach entry names an object as `schema.name`, or unqualified for a schemaless type. A function or aggregate carries its argument signature (`test.f(integer)`) and a cast its types (`(int4 AS timestamp)`), since the name alone is ambiguous.\n",
      "type": "object",
      "properties": {
        "aggregates": {
          "description": "The aggregates this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "collations": {
          "description": "The collations this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "domains": {
          "description": "The domains this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "event_triggers": {
          "description": "The event triggers this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "extensions": {
          "description": "The extensions this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "foreign_data_wrappers": {
          "description": "The foreign data wrappers this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "functions": {
          "description": "The functions this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "languages": {
          "description": "The languages this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "materialized_views": {
          "description": "The materialized views this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "publications": {
          "description": "The publications this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "sequences": {
          "description": "The sequences this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "servers": {
          "description": "The servers this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "subscriptions": {
          "description": "The subscriptions this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "tables": {
          "description": "The tables this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "types": {
          "description": "The types this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "user_mappings": {
          "description": "The user mappings this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "users": {
          "description": "The users this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "views": {
          "description": "The views this object depends on.",
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false,
  "requires": [
    "schema",
    "name"
  ],
  "oneOf": [
    {
      "required": [
        "query"
      ],
      "not": {
        "required": [
          "sql"
        ]
      }
    },
    {
      "required": [
        "sql"
      ],
      "not": {
        "required": [
          "recursive",
          "columns",
          "check_option",
          "security_barrier",
          "query"
        ]
      }
    }
  ]
}