From ed4502425a9e8fec4bd4f1a2fd9e0acaf7bdb316 Mon Sep 17 00:00:00 2001 From: ncoronel Date: Fri, 7 Aug 2026 12:07:40 -0300 Subject: [PATCH] refactor(catalog): replace hardcoded validation messages with translatable strings --- .../Catalog/Services/CatalogService.php | 26 +++++++++---------- lang/en/api.php | 13 ++++++++++ lang/es/api.php | 13 ++++++++++ 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/app/Domains/Catalog/Services/CatalogService.php b/app/Domains/Catalog/Services/CatalogService.php index 6e7b137..da1e1e8 100644 --- a/app/Domains/Catalog/Services/CatalogService.php +++ b/app/Domains/Catalog/Services/CatalogService.php @@ -50,7 +50,7 @@ class CatalogService if ($hasEventDateVariants && ! in_array('event_date', $attributeCodes, true)) { throw ValidationException::withMessages([ 'attribute_codes' => [ - 'The event_date attribute is required for event date variants.', + __('api.catalog.event_date_attribute_required'), ], ]); } @@ -60,7 +60,7 @@ class CatalogService if (array_diff($multiSelectAttributeCodes, $attributeCodes) !== []) { throw ValidationException::withMessages([ 'multi_select_attribute_codes' => [ - 'Multi-select attributes must also be present in attribute_codes.', + __('api.catalog.multi_select_attribute_not_on_item'), ], ]); } @@ -532,8 +532,8 @@ class CatalogService throw ValidationException::withMessages([ "variants.{$index}.event_date_ids" => [ $eventDateItemAttribute->allow_multi_select - ? 'At least one event date must be selected.' - : 'Exactly one event date must be selected.', + ? __('api.catalog.event_date_selection_required') + : __('api.catalog.single_event_date_required'), ], ]); } @@ -546,7 +546,7 @@ class CatalogService if ($validEventDateCount !== $eventDateIds->count()) { throw ValidationException::withMessages([ "variants.{$index}.event_date_ids" => [ - 'Every event date must belong to the catalog item tenant.', + __('api.catalog.event_date_wrong_tenant'), ], ]); } @@ -595,7 +595,7 @@ class CatalogService if (! in_array($eventProductType, EventProductType::values(), true)) { throw ValidationException::withMessages([ - 'event_product_type' => ['The event product type is invalid.'], + 'event_product_type' => [__('api.catalog.invalid_event_product_type')], ]); } } @@ -637,7 +637,7 @@ class CatalogService $key = implode('|', $combination); if (isset($seen[$key])) { throw ValidationException::withMessages([ - "variants.{$index}" => ['The variant combination must be unique.'], + "variants.{$index}" => [__('api.catalog.duplicate_variant_combination')], ]); } @@ -657,15 +657,15 @@ class CatalogService throw ValidationException::withMessages([ $validationKey => [ $itemAttribute->allow_multi_select - ? 'At least one value must be selected.' - : 'Exactly one value must be selected.', + ? __('api.catalog.multi_value_required') + : __('api.catalog.single_value_required'), ], ]); } if (collect($values)->contains(fn ($item): bool => ! is_string($item) || trim($item) === '')) { throw ValidationException::withMessages([ - $validationKey => ['Every selected value must be a non-empty string.'], + $validationKey => [__('api.catalog.selected_values_non_empty')], ]); } @@ -674,7 +674,7 @@ class CatalogService if ($normalizedValues->unique()->count() !== count($values)) { throw ValidationException::withMessages([ - $validationKey => ['Selected values must be distinct.'], + $validationKey => [__('api.catalog.selected_values_distinct')], ]); } @@ -686,7 +686,7 @@ class CatalogService $resolvedOptions = $normalizedValues->map(fn (string $normalizedValue) => $optionsByNormalizedValue->get($normalizedValue)); if ($resolvedOptions->contains(null)) { throw ValidationException::withMessages([ - $validationKey => ['One or more selected values are not valid attribute options.'], + $validationKey => [__('api.catalog.invalid_attribute_options')], ]); } @@ -696,7 +696,7 @@ class CatalogService ->unique(); if ($validityTimeIds->count() > 1) { throw ValidationException::withMessages([ - $validationKey => ['Selected values cannot have different validity windows.'], + $validationKey => [__('api.catalog.incompatible_validity_windows')], ]); } diff --git a/lang/en/api.php b/lang/en/api.php index c3910da..8651e67 100644 --- a/lang/en/api.php +++ b/lang/en/api.php @@ -92,6 +92,19 @@ return [ 'variants_required' => 'An item with attribute_codes must have variants.', 'variants_forbidden' => 'An item without attribute_codes cannot have variants.', 'direct_inventory_forbidden' => 'An item with variants cannot have direct inventory.', + 'event_date_attribute_required' => 'The event_date attribute is required for event date variants.', + 'multi_select_attribute_not_on_item' => 'Multi-select attributes must also be present in attribute_codes.', + 'event_date_selection_required' => 'At least one event date must be selected.', + 'single_event_date_required' => 'Exactly one event date must be selected.', + 'event_date_wrong_tenant' => 'Every event date must belong to the catalog item tenant.', + 'invalid_event_product_type' => 'The event product type is invalid.', + 'duplicate_variant_combination' => 'The variant combination must be unique.', + 'multi_value_required' => 'At least one value must be selected.', + 'single_value_required' => 'Exactly one value must be selected.', + 'selected_values_non_empty' => 'Every selected value must be a non-empty string.', + 'selected_values_distinct' => 'Selected values must be distinct.', + 'invalid_attribute_options' => 'One or more selected values are not valid attribute options.', + 'incompatible_validity_windows' => 'Selected values cannot have different validity windows.', ], 'menu' => [ 'schema_required' => 'The schema is required for static menus.', diff --git a/lang/es/api.php b/lang/es/api.php index 79bda55..1b9c6f8 100644 --- a/lang/es/api.php +++ b/lang/es/api.php @@ -92,6 +92,19 @@ return [ 'variants_required' => 'Un ítem con attribute_codes debe tener variantes.', 'variants_forbidden' => 'Un ítem sin attribute_codes no puede tener variantes.', 'direct_inventory_forbidden' => 'Un ítem con variantes no puede tener inventario directo.', + 'event_date_attribute_required' => 'El atributo event_date es obligatorio para las variantes con fecha de evento.', + 'multi_select_attribute_not_on_item' => 'Los atributos multiselección también deben estar incluidos en attribute_codes.', + 'event_date_selection_required' => 'Debe seleccionar al menos una fecha de evento.', + 'single_event_date_required' => 'Debe seleccionar exactamente una fecha de evento.', + 'event_date_wrong_tenant' => 'Todas las fechas del evento deben pertenecer al tenant del ítem de catálogo.', + 'invalid_event_product_type' => 'El tipo de producto de evento no es válido.', + 'duplicate_variant_combination' => 'La combinación de la variante debe ser única.', + 'multi_value_required' => 'Debe seleccionar al menos un valor.', + 'single_value_required' => 'Debe seleccionar exactamente un valor.', + 'selected_values_non_empty' => 'Cada valor seleccionado debe ser un texto no vacío.', + 'selected_values_distinct' => 'Los valores seleccionados no pueden repetirse.', + 'invalid_attribute_options' => 'Uno o más valores seleccionados no son opciones válidas del atributo.', + 'incompatible_validity_windows' => 'Los valores seleccionados no pueden tener ventanas de validez diferentes.', ], 'menu' => [ 'schema_required' => 'El schema es obligatorio para los menús estáticos.',