{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://caspian.md/docs/database.schema.json",
  "title": "Caspian project database",
  "description": "Schema for database.json, a project's optional local user accounts, next to its canvas.json. Only used when the project has \"User registration\" on and its canvas has the \"users\" capability. See https://caspian.md/docs/#database.",
  "type": "object",
  "required": [
    "caspian_database",
    "users"
  ],
  "properties": {
    "caspian_database": {
      "const": 1,
      "description": "Format version."
    },
    "users": {
      "type": "object",
      "required": [
        "fields",
        "records"
      ],
      "properties": {
        "fields": {
          "type": "array",
          "description": "Extra things the sign-up form asks for, beyond email, password and name.",
          "maxItems": 30,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "id",
              "label",
              "type"
            ],
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[a-z][a-z0-9_-]{0,39}$",
                "not": {
                  "enum": [
                    "id",
                    "email",
                    "name",
                    "password",
                    "passwordhash",
                    "createdat"
                  ]
                },
                "description": "Key in each record's fields and in auth.register's fields."
              },
              "label": {
                "type": "string",
                "minLength": 1,
                "maxLength": 80
              },
              "type": {
                "enum": [
                  "string",
                  "number",
                  "boolean",
                  "email",
                  "phone",
                  "url",
                  "date"
                ]
              },
              "required": {
                "type": "boolean"
              }
            }
          }
        },
        "records": {
          "type": "array",
          "description": "Registered users. Written by Caspian only.",
          "items": {
            "type": "object",
            "required": [
              "id",
              "email",
              "passwordHash",
              "createdAt"
            ],
            "properties": {
              "id": {
                "type": "string"
              },
              "email": {
                "type": "string",
                "description": "Lower-cased; unique."
              },
              "name": {
                "type": "string"
              },
              "passwordHash": {
                "type": "string",
                "pattern": "^(scrypt|pbkdf2_sha256)\\$",
                "description": "scrypt$<N>$<r>$<p>$<salt base64>$<hash base64> for accounts made in Caspian, or pbkdf2_sha256$<iterations>$<salt>$<hash base64> for ones synced down from the live site. Never a plain password."
              },
              "createdAt": {
                "type": "string",
                "format": "date-time"
              },
              "updatedAt": {
                "type": "string",
                "description": "Bumped on every change; two-way sync with the live site compares it."
              },
              "emailVerified": {
                "type": "boolean",
                "description": "Whether they confirmed their email on the live site."
              },
              "fields": {
                "type": "object",
                "additionalProperties": {
                  "type": [
                    "string",
                    "number",
                    "boolean",
                    "null"
                  ]
                }
              }
            }
          }
        }
      }
    }
  }
}
