This package contains type definitions for the Hubby eSIM project with Zod schema validation.
This package has been upgraded to use Zod schemas for improved runtime validation and type safety. The migration provides:
-
Dual schema approach:
- Firestore schemas using native Firestore types (Timestamp, DocumentReference)
- App schemas using JavaScript-friendly types (Date, string IDs)
-
Automatic conversion between Firestore and app data
-
Backward compatibility with existing type definitions
You can continue to import types as before for backward compatibility:
import { Booking } from '@hubbyesim/types';Or import the new Zod-based types directly:
import { BookingApp, BookingFirestore } from '@hubbyesim/types/schemas/booking';import { bookingAppSchema, bookingFirestoreSchema } from '@hubbyesim/types/schemas/booking';
// Validate app data
try {
const validBooking = bookingAppSchema.parse(bookingData);
// Data is valid, proceed
} catch (error) {
if (error instanceof z.ZodError) {
// Handle validation errors
console.error('Validation failed:', error.errors);
}
}import {
bookingToFirestore,
bookingFromFirestore
} from '@hubbyesim/types/schemas/booking';
// Convert app data to Firestore format
const firestoreData = bookingToFirestore(appData);
// Convert Firestore data to app format
const appData = bookingFromFirestore(firestoreData);import { z } from 'zod';
import {
userAppSchema,
userFirestoreSchema,
userToFirestore,
userFromFirestore
} from '@hubbyesim/types/schemas/user';
// Create a new user with type safety
const user = {
id: 'user_123',
name: 'John Doe',
email: '[email protected]',
profileId: 'profile_456',
createdAt: new Date(),
created_at: new Date(),
updated_at: new Date(),
created_by: 'system',
updated_by: null
};
// Validate
const validUser = userAppSchema.parse(user);
// Convert to Firestore
const firestoreUser = userToFirestore(validUser);
// Save to Firestore (in your code)
// await userCollection.doc(firestoreUser.id).set(firestoreUser);
// Convert back to app format
const retrievedUser = userFromFirestore(firestoreUser);The following models have been migrated to use Zod schemas:
UserBookingPartnerCountryPackagePromoCodeESIMPaymentMessage(includingSentMessages)CurrencyApiLogAPI(including request/response types)
Each model has:
- Firestore schema (
xxxFirestoreSchema) - App schema (
xxxAppSchema) - Conversion functions (
xxxToFirestore,xxxFromFirestore) - Type definitions (
XxxApp,XxxFirestore)
- ✅ Dual schema approach (Firestore/App)
- ✅ Automatic conversion between formats
- ✅ DocumentReference handling (as string IDs in app schema)
- ✅ Collection path constants for references
- ✅ Backward compatibility
- ✅ Comprehensive schema validation
- ✅ Type-safe conversion functions
- Runtime Validation: Validate data shape at runtime, not just during compilation
- Developer Experience: Strong type inference and autocomplete
- Error Handling: Detailed and structured error messages
- Simplified Integration: Seamless conversion between Firestore and application data
- Maintainability: Single source of truth for types, with schemas that generate TypeScript types
This package uses a dual-type system for each schema:
*Firestoretypes - Represent data as stored in Firestore*Apptypes - Represent data as used in application code
For convenience and backward compatibility:
- Firestore types are exported with both their full name and the base name (e.g.,
BookingFirestoreandBooking) - App types are exported with an H-prefix (e.g.,
BookingAppasHBooking)
All types are now automatically exported from a single index.ts file that is generated from the schema files.
To regenerate the exports after making changes to schema files, run:
npm run generate-exportsNote: The individual .d.ts files in the src/ directory (like booking.d.ts, etc.) are deprecated and will be removed in a future version. All types are now exported directly from the schema files through the generated index.ts.
include like this:
"dependencies": { "@hubbyesim/types": "git+https://github.com/hubbyesim/types.git#v1.0.5" }