feat(validation): implement comprehensive validation for item creation and updates

This commit is contained in:
Galindo97
2026-01-20 12:53:08 -06:00
parent ba353493d3
commit dd6c832674
6 changed files with 611 additions and 16 deletions

View File

@@ -277,6 +277,27 @@
lines: editingItem.lines || []
});
// Verificar si hay errores de validación
if ('error' in response) {
// Manejar errores de validación (422)
if (response.validationErrors && Array.isArray(response.validationErrors)) {
const validationErrors = response.validationErrors
.map((err: any) => `• ${err.message}`)
.join('\n');
toast.error('Errores de validación', {
description: validationErrors,
duration: 10000
});
} else {
toast.error('Error al crear item', {
description: response.error || 'No se pudo crear el item. Intenta de nuevo.'
});
}
isSaving = false;
return;
}
// Recargar items
await loadItems();
@@ -286,10 +307,22 @@
});
} catch (error: any) {
console.error('Error creating item:', error);
const errorMessage = error?.response?.data?.detail || 'No se pudo crear el item. Intenta de nuevo.';
toast.error('Error al crear item', {
description: errorMessage
});
// Manejar errores de validación (422)
if (error?.response?.data?.errors && Array.isArray(error.response.data.errors)) {
const validationErrors = error.response.data.errors
.map((err: any) => `• ${err.message}`)
.join('\n');
toast.error('Errores de validación', {
description: validationErrors
});
} else {
const errorMessage = error?.response?.data?.detail || 'No se pudo crear el item. Intenta de nuevo.';
toast.error('Error al crear item', {
description: errorMessage
});
}
} finally {
isSaving = false;
}
@@ -300,7 +333,7 @@
isSaving = true;
try {
await itemsApi.update(selectedItem.id, activeCompanyId, {
const response = await itemsApi.update(selectedItem.id, activeCompanyId, {
reference_number: editingItem.reference_number,
order: editingItem.order,
warehouse: editingItem.warehouse,
@@ -308,6 +341,27 @@
lines: editingItem.lines || []
});
// Verificar si hay errores de validación
if ('error' in response) {
// Manejar errores de validación (422)
if (response.validationErrors && Array.isArray(response.validationErrors)) {
const validationErrors = response.validationErrors
.map((err: any) => `• ${err.message}`)
.join('\n');
toast.error('Errores de validación', {
description: validationErrors,
duration: 10000
});
} else {
toast.error('Error al actualizar item', {
description: response.error || 'No se pudo actualizar el item. Intenta de nuevo.'
});
}
isSaving = false;
return;
}
// Recargar items
await loadItems();
@@ -317,10 +371,22 @@
});
} catch (error: any) {
console.error('Error updating item:', error);
const errorMessage = error?.response?.data?.detail || 'No se pudo actualizar el item. Intenta de nuevo.';
toast.error('Error al actualizar item', {
description: errorMessage
});
// Manejar errores de validación (422)
if (error?.response?.data?.errors && Array.isArray(error.response.data.errors)) {
const validationErrors = error.response.data.errors
.map((err: any) => `• ${err.message}`)
.join('\n');
toast.error('Errores de validación', {
description: validationErrors
});
} else {
const errorMessage = error?.response?.data?.detail || 'No se pudo actualizar el item. Intenta de nuevo.';
toast.error('Error al actualizar item', {
description: errorMessage
});
}
} finally {
isSaving = false;
}