Skip to content

Schemas & Types

All Zod schemas live in packages/shared/src/schemas/. TypeScript types are inferred from the schemas and re-exported from packages/shared/src/types/index.ts and packages/shared/src/index.ts.


Current Schemas

UserSchema

File: packages/shared/src/schemas/user.schema.ts

ts
import { z } from 'zod'

export const UserSchema = z.object({
  _id:       z.string(),
  name:      z.string().min(1),
  email:     z.string().email(),
  role:      z.enum(['admin', 'user']),
  createdAt: z.string().datetime(),
})

export type User = z.infer<typeof UserSchema>

Exported type: User

Schema vs. Model Alignment

The UserSchema in @my-app/shared is the cross-boundary API contract type. The Mongoose userModel has additional fields (full_name, phone, preferences, features, has_preferences_setup, etc.) that are internal to the backend. The shared schema represents only what the frontend needs to know about a user.


Public Entry Point

File: packages/shared/src/index.ts

ts
export * from './schemas/user.schema'

Import from @my-app/shared:

ts
import { UserSchema, type User } from '@my-app/shared'

Types Re-Export

File: packages/shared/src/types/index.ts

ts
export type { User } from '../schemas/user.schema'

This file allows consumers to import just the type without the Zod schema:

ts
import type { User } from '@my-app/shared'

Extending the Schema Set

The shared package currently only exposes UserSchema. As the codebase matures, additional schemas should be added here to:

  • Validate API request bodies on the backend (replacing ad-hoc validation in controllers)
  • Type-check API response shapes in the frontend

Suggested schemas to add:

SchemaFields
UnitSchema_id, name, min/max_temperature, min/max_humidity, live_alerts
DeviceSchema_id, code, name, unit_id, recent_sensor_data
RecipientSchema_id, name, email, phone, is_default
SensorDataSchemadevice_id, date, temperature, humidity, volt
AlertSchemaerror_code, start_date, value, count

Intecog Logistech IoT Monitoring Platform