📋 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 aschema 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 thefirestoreRef helper:
DocumentReference objects:
Collection Path Types
For better type safety, you can use theCollectionPath 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
:paramsyntax 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.