status: done
Task: JSON-safe timestamps + Zod schemas
End Goal
Timestamp wrapper format { "__time__": "<ISO>" } works bi-directionally, and Zod schemas validate payloads.
Currently
Datamodel registry loads entity definitions.
Should
- Convert Firestore
Timestamp/Date to {__time__} on output
- Convert
{__time__} to Firestore Timestamp on input
- Zod schemas for create/update validation and schema introspection
Constraints
- [ ] Timestamp format exactly:
{ "__time__": "<ISO8601>" }
- [ ] Recursive conversion for nested objects/arrays
- [ ] Strict Zod objects (no extra fields)
Acceptance Criteria
- [ ] All unit tests pass
- [ ] Timestamp wrapper format matches exactly
- [ ] Round-trip serialization preserves data
Implementation Checklist
- [x] 4.1 Create
src/firestore/serialization.ts with TimeWrapper type
- [x] 4.2 Implement
toJsonSafe(value) for Firestore → JSON conversion
- [x] 4.3 Handle
Timestamp, Date, arrays, objects recursively
- [x] 4.4 Implement
fromJsonSafeToFirestore(value) for JSON → Firestore conversion
- [x] 4.5 Validate ISO date string is parseable
- [x] 4.6 Create
src/datamodels/zodSchemas.ts with timeWrapperSchema
- [x] 4.7 Implement
buildEntityDataSchema(entity) from field specs
- [x] 4.8 Handle required/optional based on
spec.required
- [x] 4.9 Handle
nullable fields
- [x] 4.10 Handle
enums → z.enum
- [x] 4.11 Handle
timestamp → timeWrapperSchema
- [x] 4.12 Handle
map → z.record(z.unknown())
- [x] 4.13 Handle
array with referenced primitive or unknown
- [x] 4.14 Implement
buildCreateOrUpsertDataSchema(entity) allowing omitted id/timestamps
- [x] 4.15 Implement
buildUpdateDataSchema(entity) as fully partial
- [x] 4.16 Add unit tests for timestamp conversion
- [x] 4.17 Add unit tests for nested conversion
- [x] 4.18 Add unit tests for required/optional/nullable rules
Notes
- TimeWrapper:
{ __time__: string }
- Timestamp validation: Must be valid ISO 8601 date string
Reacties