Skip to main content

📋 Schema Definition

Firetype uses Zod schemas to define your Firestore collection and document structures. Organize your schemas in a directory structure that mirrors your Firestore database hierarchy.

Directory Structure

Basic Collection Schema

Each schema file must export a schema constant using Zod:

Subcollection Schema

Define subcollections by creating nested directories:

Advanced Schema Patterns

Union Types (Polymorphism)

Firestore References

Firetype supports strongly-typed Firestore document references using the firestoreRef helper:
You can use dynamic path segments for documentation purposes:
References automatically resolve to properly typed DocumentReference objects:
The generated types also include a union type of all valid collection paths for better development experience:

Collection Path Types

For better type safety, you can use the CollectionPath type and collectionPath helper:

Complex Validation

Schema Best Practices

  • Use descriptive names: Choose clear, descriptive names for your collections and fields
  • Add validation: Leverage Zod’s validation features to ensure data integrity
  • Use enums: For fields with a fixed set of values, use z.enum() instead of strings
  • Default values: Provide sensible defaults for optional fields
  • Firestore references: Use firestoreRef() for strongly-typed document references
  • Collection paths: Use collectionPath() helper for better type safety with references
  • Dynamic path segments: Use :param syntax in paths for documentation (e.g., "users/:userId/posts")
  • Type references: Use TypeScript types when referencing other document IDs
  • Custom validation: Add business logic validation using Zod’s .refine() method
  • Keep it DRY: Extract common schema parts into reusable constants
💡 Pro Tip: Your Zod schemas serve as both runtime validators and TypeScript type generators. Well-crafted schemas provide excellent developer experience and data integrity.