mirror of
https://github.com/R0m1k3/LogiFlow.git
synced 2026-10-12 09:47:54 +02:00
Address Zod validation errors by ensuring correct data types are sent from the frontend and transformed on the backend, specifically for amounts and dates, to resolve 500 server errors during delivery updates. Replit-Commit-Author: Agent Replit-Commit-Session-Id: b163d4c0-de5e-4f4e-a9c0-aed4c7049718 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/b163d4c0-de5e-4f4e-a9c0-aed4c7049718/kMVZi4q
3.5 KiB
3.5 KiB
Fix Urgent - Erreurs de Validation Zod
❌ Problème Identifié
Erreur 500 : ZodError lors de PUT /api/deliveries/:id
Erreurs Specifiques
- blAmount : Expected string, received number
- validatedAt : Expected date, received string
🔍 Analyse du Problème
Schema Drizzle (shared/schema.ts)
// Dans deliveries table:
blAmount: decimal("bl_amount", { precision: 10, scale: 2 }) // → Zod attend string
invoiceAmount: decimal("invoice_amount", { precision: 10, scale: 2 }) // → Zod attend string
validatedAt: timestamp("validated_at") // → Zod attend Date object
Données Envoyées depuis Frontend
// ❌ Problématique
{
blAmount: 3397.86, // number → should be string
invoiceAmount: null, // OK
validatedAt: '2025-08-12T11:42:39.679Z' // string → should be Date
}
✅ Solutions Appliquées
1. Transformation côté Serveur (server/routes.ts)
// Avant validation Zod, transformer les types
const transformedData = { ...req.body };
// Convertir montants number → string
if (transformedData.blAmount !== undefined && transformedData.blAmount !== null) {
transformedData.blAmount = transformedData.blAmount.toString();
}
if (transformedData.invoiceAmount !== undefined && transformedData.invoiceAmount !== null) {
transformedData.invoiceAmount = transformedData.invoiceAmount.toString();
}
// Convertir validatedAt string → Date
if (transformedData.validatedAt && typeof transformedData.validatedAt === 'string') {
transformedData.validatedAt = new Date(transformedData.validatedAt);
}
const data = insertDeliverySchema.partial().parse(transformedData);
2. Correction côté Frontend (ReconciliationModal.tsx)
// Avant
validatedAt: new Date() // ❌ Date object → string lors JSON.stringify
// Après
validatedAt: new Date().toISOString() // ✅ string → convertie en Date côté serveur
3. Montants toujours en string (ReconciliationModal.tsx)
// Déjà corrigé précédemment
blAmount: formData.blAmount ? formData.blAmount.toString() : null,
invoiceAmount: formData.invoiceAmount ? formData.invoiceAmount.toString() : null,
🧪 Test de Validation
Payload Attendu Maintenant
// ✅ Correct après transformation
{
blNumber: 'LD2250800571',
blAmount: '3397.86', // string ✅
invoiceReference: null,
invoiceAmount: null,
reconciled: true,
validatedAt: new Date('2025-08-12T11:42:39.679Z') // Date object ✅
}
Tests à Effectuer
- Enregistrer données BL : blAmount doit passer en string
- Valider rapprochement : validatedAt doit passer en Date
- Enregistrer montant facture : invoiceAmount doit passer en string
- Vérifier console : Plus d'erreurs ZodError 500
🔄 Flux de Données Complet
Frontend Form → JSON.stringify → HTTP Request → Server Transform → Zod Validation → Database
↓ ↓ ↓ ↓ ↓ ↓
{number} → "3397.86" → "3397.86" → "3397.86" → ✅ → DECIMAL
{Date} → "2025-..." → "2025-..." → Date(...) → ✅ → TIMESTAMP
📊 Status
- ✅ Transformation automatique côté serveur
- ✅ Frontend envoie validatedAt en ISO string
- ✅ Montants forcés en string côté client
- 🔄 À tester en production
L'erreur 500 ZodError devrait être résolue !