{}FireSchema

Firestore 向け JSON Schema:完全ガイド

Firestore コレクションをドキュメント化するためのスキーマの書き方を学ぶ

JSON Schema は JSON データの構造を記述するためのオープンスタンダードです。Google、Microsoft、AWS などの企業が設定ファイル、API、データモデルの検証に使用しています。FireSchema は JSON Schema を使用して Firestore コレクションを記述し、データベースをドキュメント化するための強力で標準化された方法を提供します。 なぜ最適な選択なのかを学ぶ.

JSON Schema とは?

JSON Schema は JSON ドキュメントの構造を定義します:どのフィールドが存在するか、その型は何か、どれが必須か、どの値が有効か。すべての主要言語のバリデータでサポートされている標準(draft 2020-12)です。

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer", "minimum": 0 }
  },
  "required": ["name"]
}

このスキーマは次のことを意味します:データは必須の name(文字列)とオプションの age(非負の整数)を持つオブジェクトです。

FireSchema の基本構造

FireSchema は JSON Schema をコレクションメタデータのエンベロープでラップします。各 .schema.json ファイルは次の構造を持ちます:

schemas/users.schema.jsonJSON
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "collection": "users",
  "description": "Registered app users",
  "schema": {
    "type": "object",
    "properties": {
      "displayName": {
        "type": "string",
        "description": "User's full name"
      }
    }
  }
}

トップレベルフィールド:

  • - $schema — JSON Schema のバージョン(常に 2020-12 を使用)
  • - collection — Firestore コレクション名
  • - description — コレクションの人間が読める説明
  • - documentCount — オプションの推定ドキュメント数
  • - schema — このコレクション内の各ドキュメントを記述する JSON Schema

一般的なフィールド型

JSON Schema は Firestore ドキュメントに必要なすべての型をサポートしています:

{
  "schema": {
    "type": "object",
    "properties": {
      "title": {
        "type": "string",
        "description": "Product title"
      },
      "price": {
        "type": "number",
        "description": "Price in USD",
        "minimum": 0
      },
      "inStock": {
        "type": "boolean",
        "description": "Whether the product is available"
      },
      "tags": {
        "type": "array",
        "items": { "type": "string" },
        "description": "Product tags for search"
      },
      "metadata": {
        "type": "object",
        "description": "Additional product metadata",
        "properties": {
          "createdBy": { "type": "string" },
          "version": { "type": "integer" }
        }
      }
    }
  }
}

利用可能な型:

  • - string — テキスト値(名前、メール、ID)
  • - number — 小数値(価格、スコア)
  • - integer — 整数値(カウント、バージョン)
  • - boolean — 真偽値(フラグ、トグル)
  • - array — 値のリスト(タグ、アイテム)
  • - object — ネストされたオブジェクト(住所、メタデータ)

Firestore 固有のパターン

Firestore には JSON Schema のフォーマットにうまくマッピングできる固有のデータ型があります:

{
  "schema": {
    "type": "object",
    "properties": {
      "createdAt": {
        "type": "string",
        "format": "date-time",
        "description": "Firestore Timestamp"
      },
      "location": {
        "type": "object",
        "description": "Firestore GeoPoint",
        "properties": {
          "latitude": { "type": "number", "minimum": -90, "maximum": 90 },
          "longitude": { "type": "number", "minimum": -180, "maximum": 180 }
        },
        "required": ["latitude", "longitude"]
      },
      "authorRef": {
        "type": "string",
        "description": "Firestore DocumentReference to users collection",
        "pattern": "^users/.+$"
      }
    }
  }
}

バリデーションルール

制約を追加して、スキーマをより記述的で有用なものにしましょう:

{
  "schema": {
    "type": "object",
    "properties": {
      "email": {
        "type": "string",
        "format": "email",
        "description": "Must be a valid email"
      },
      "age": {
        "type": "integer",
        "minimum": 13,
        "maximum": 120,
        "description": "User age (13+ required)"
      },
      "role": {
        "type": "string",
        "enum": ["admin", "editor", "viewer"],
        "description": "One of the allowed roles"
      },
      "username": {
        "type": "string",
        "minLength": 3,
        "maxLength": 20,
        "pattern": "^[a-z0-9_]+$",
        "description": "Lowercase alphanumeric, 3-20 chars"
      }
    }
  }
}

一般的な制約:

  • - format — email、date-time、uri、uuid
  • - enum — 許可される値のリスト
  • - minimum / maximum — 数値の範囲
  • - minLength / maxLength — 文字列の長さ制限
  • - pattern — 正規表現によるバリデーション

完全な例:Orders コレクション

users 内の orders サブコレクションの完全なスキーマです。これを schemas/users/orders.schema.json として保存してください:

schemas/users/orders.schema.jsonJSON
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "collection": "orders",
  "description": "User purchase orders",
  "documentCount": 15000,
  "schema": {
    "type": "object",
    "properties": {
      "orderNumber": {
        "type": "string",
        "pattern": "^ORD-[0-9]{6}$",
        "description": "Order ID in format ORD-000000"
      },
      "status": {
        "type": "string",
        "enum": ["pending", "confirmed", "shipped", "delivered", "cancelled"],
        "description": "Current order status"
      },
      "items": {
        "type": "array",
        "description": "Ordered items",
        "items": {
          "type": "object",
          "properties": {
            "productId": { "type": "string" },
            "name": { "type": "string" },
            "quantity": { "type": "integer", "minimum": 1 },
            "price": { "type": "number", "minimum": 0 }
          },
          "required": ["productId", "name", "quantity", "price"]
        }
      },
      "total": {
        "type": "number",
        "minimum": 0,
        "description": "Order total in USD"
      },
      "createdAt": {
        "type": "string",
        "format": "date-time",
        "description": "When the order was placed"
      }
    },
    "required": ["orderNumber", "status", "items", "total", "createdAt"]
  }
}

このスキーマは、コレクション名、推定ドキュメント数、すべてのフィールドとその型・説明、enum、パターン、最小値などのバリデーションルールを文書化しています。FireSchema でレンダリングすると、インタラクティブで閲覧可能なドキュメントになります。

ツールとリソース

これらのツールは JSON Schema ファイルの作成と検証に役立ちます:

次のステップ

FireSchema を試す

クイックスタートガイド