@blackcode_sa/metaestetics-api 1.13.20 → 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -63,7 +63,8 @@ export const contraindicationSchema = z.object({
63
63
  isActive: z.boolean(),
64
64
  });
65
65
 
66
- export const medicationSchema = z.object({
66
+ // Base medication schema without refinement (for update operations that need .partial())
67
+ const baseMedicationSchema = z.object({
67
68
  name: z.string().min(1),
68
69
  dosage: z.string().min(1),
69
70
  frequency: z.string().min(1),
@@ -72,6 +73,33 @@ export const medicationSchema = z.object({
72
73
  prescribedBy: z.string().optional().nullable(),
73
74
  });
74
75
 
76
+ // Medication schema with date validation refinement
77
+ export const medicationSchema = baseMedicationSchema.refine(
78
+ (data) => {
79
+ // If either date is not provided, skip validation (both are optional)
80
+ // However, if endDate is provided, startDate must also be provided
81
+ if (!data.endDate) {
82
+ return true;
83
+ }
84
+
85
+ // If endDate exists but startDate doesn't, this is invalid
86
+ if (!data.startDate) {
87
+ return false;
88
+ }
89
+
90
+ // Both dates must be Timestamp objects with toDate method
91
+ const startDate = data.startDate.toDate();
92
+ const endDate = data.endDate.toDate();
93
+
94
+ // End date must be >= start date
95
+ return endDate >= startDate;
96
+ },
97
+ {
98
+ message: "End date requires a start date and must be equal to or after start date",
99
+ path: ["endDate"], // This will attach the error to the endDate field
100
+ }
101
+ );
102
+
75
103
  export const patientMedicalInfoSchema = z.object({
76
104
  patientId: z.string(),
77
105
  vitalStats: vitalStatsSchema,
@@ -120,6 +148,30 @@ export const updateContraindicationSchema = contraindicationSchema
120
148
  });
121
149
 
122
150
  export const addMedicationSchema = medicationSchema;
123
- export const updateMedicationSchema = medicationSchema.partial().extend({
151
+ export const updateMedicationSchema = baseMedicationSchema.partial().extend({
124
152
  medicationIndex: z.number().min(0),
125
- });
153
+ }).refine(
154
+ (data) => {
155
+ // If either date is not provided, skip validation (both are optional)
156
+ // However, if endDate is provided, startDate must also be provided
157
+ if (!data.endDate) {
158
+ return true;
159
+ }
160
+
161
+ // If endDate exists but startDate doesn't, this is invalid
162
+ if (!data.startDate) {
163
+ return false;
164
+ }
165
+
166
+ // Both dates must be Timestamp objects with toDate method
167
+ const startDate = data.startDate.toDate();
168
+ const endDate = data.endDate.toDate();
169
+
170
+ // End date must be >= start date
171
+ return endDate >= startDate;
172
+ },
173
+ {
174
+ message: "End date requires a start date and must be equal to or after start date",
175
+ path: ["endDate"], // This will attach the error to the endDate field
176
+ }
177
+ );