{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://schemas.apps.smartmate.io/tables/tablePermissions/tablePermissions.schema.json",
  "$$target": "https://schemas.apps.smartmate.io/tables/tablePermissions/tablePermissions.schema.json",
  "type": "object",
  "title": "permissions",
  "description": "Table permissions to create, delete, view or edit records. See :ref:`Table permissions <table-permissions>` for more information.`",
  "propertyNames": {
    "allOf": [{
      "description": "The key must correspond to a valid :ref:`app role <app-roles>`.",
      "type": "string",
      "pattern": "^([a-zA-Z][a-zA-Z_0-9.]*)$",
      "minLength": 2,
      "maxLength": 81,
      "examples": [
        "manager"
      ]
    }]
  },
  "patternProperties": {
    "": {
      "type": "object",
      "properties": {
        "create": {
          "description": "Determines if the user can create an item in this table.",
          "$ref": "#/definitions/create"
        },
        "delete": {
          "description": "Determines if the user can delete an item.",
          "$ref": "#/definitions/delete"
        },
        "view": {
          "description": "Determines if the user can view items.",
          "$ref": "#/definitions/viewOrEdit"
        },
        "edit": {
          "description": "Determines which fields are editable by the user.",
          "$ref": "#/definitions/viewOrEdit"
        }
      },
      "additionalProperties": false
    }
  },
  "definitions": {
    "create": {
      "oneOf": [
        { "type": "boolean" },
        {
          "$ref": "#/definitions/validArrayOptions"
        }
      ]
    },
    "delete": {
      "oneOf": [
        { "type": "boolean" },
        {
          "type": "object",
          "propertyNames": {
            "type": "string",
            "enum": ["own", "any", "assigned"]
          },
          "patternProperties": {
              "": {
                "type": "boolean"
              }
          },
          "additionalProperties": false
        }
      ]
    },
    "viewOrEdit": {
        "oneOf": [
            { "type": "boolean" },
            {
              "$ref": "#/definitions/validArrayOptions"
            },
            {
              "type": "object",
              "propertyNames": {
                "type": "string",
                "enum": ["own", "any", "assigned"]
              },
              "patternProperties": {
                "": {
                  "anyOf": [
                    {
                      "type": "boolean"
                    },
                    {
                      "$ref": "#/definitions/validArrayOptions"
                    }
                  ]
                }
              },
              "additionalProperties": false
            }
        ]
    },
    "validArrayOptions": {
        "type": "array",
        "items": {
         "oneOf": [
             {
               "type": "string",
               "enum" : ["*"],
               "description": "It can set all the fields."
             },
             {
               "$ref": "https://schemas.apps.smartmate.io/patterns/identification.schema.json",
               "description": "It can set the specified field"
             },
             {
                "type": "string",
               "description": "It cannot set the field that is preceded by a ``!``",
               "pattern": "^([!][a-zA-Z_0-9]*)$",
                "minLength": 2,
                "maxLength": 41,
                "examples": [
                  "!firstName"
                ]
             }
         ]
        }
    }
  },
  "additionalProperties": false
}
