Skip to content

Commit bf70d26

Browse files
committed
fix
1 parent 8cc6b11 commit bf70d26

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

src/Validators/DataValidator.php

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ public function validateProduct(array $product): void
4040
$errors = array_merge($errors, $fieldErrors);
4141
}
4242

43+
// Validate localized variant fields (e.g., variants_lt-LT, variants_lv-LV)
44+
if (isset($this->fieldConfiguration['variants'])) {
45+
$variantsConfig = $this->fieldConfiguration['variants'];
46+
foreach ($product as $fieldName => $value) {
47+
// Check if this is a localized variants field that wasn't already validated
48+
if (str_starts_with($fieldName, 'variants_') && !isset($this->fieldConfiguration[$fieldName])) {
49+
$fieldErrors = $this->validateField($fieldName, $value, $variantsConfig);
50+
$errors = array_merge($errors, $fieldErrors);
51+
}
52+
}
53+
}
54+
4355
if (!empty($errors)) {
4456
throw new ValidationException('Product validation failed', $errors);
4557
}
@@ -205,8 +217,8 @@ private function validateVariants(string $fieldName, array $variants, FieldConfi
205217
continue;
206218
}
207219

208-
// Validate required variant fields (based on Go ProductVariant struct)
209-
$requiredFields = ['id', 'sku', 'url', 'attributes'];
220+
// Validate required variant fields
221+
$requiredFields = ['id', 'sku', 'productUrl', 'attributes'];
210222
foreach ($requiredFields as $requiredField) {
211223
if (!isset($variant[$requiredField])) {
212224
$errors[] = "Field '{$fieldName}' variant at index {$index} must have '{$requiredField}' field";
@@ -219,44 +231,41 @@ private function validateVariants(string $fieldName, array $variants, FieldConfi
219231
$errors[] = "Field '{$fieldName}' variant at index {$index} 'id' must be a non-empty string";
220232
}
221233

222-
if (!is_string($variant['url']) || !filter_var($variant['url'], FILTER_VALIDATE_URL)) {
223-
$errors[] = "Field '{$fieldName}' variant at index {$index} 'url' must be a valid URL";
234+
if (!is_string($variant['productUrl']) || !filter_var($variant['productUrl'], FILTER_VALIDATE_URL)) {
235+
$errors[] = "Field '{$fieldName}' variant at index {$index} 'productUrl' must be a valid URL";
224236
}
225237

226238
if (!is_array($variant['attributes'])) {
227239
$errors[] = "Field '{$fieldName}' variant at index {$index} 'attributes' must be an array";
228240
continue;
229241
}
230242

231-
// Validate variant attributes against field config attributes
232-
if ($fieldConfig->attributes !== null) {
233-
foreach ($fieldConfig->attributes as $attrName => $attrConfig) {
234-
if (isset($variant['attributes'][$attrName])) {
235-
$attribute = $variant['attributes'][$attrName];
236-
237-
// Validate the attribute structure (must have 'name' and 'value')
238-
if (!is_array($attribute)) {
239-
$errors[] = "Field '{$fieldName}' variant at index {$index} attribute '{$attrName}' must be an object with 'name' and 'value' fields";
240-
continue;
241-
}
243+
// Validate variant attributes structure - expecting array of objects with 'name' and 'value'
244+
foreach ($variant['attributes'] as $attrIndex => $attribute) {
245+
if (!is_array($attribute)) {
246+
$errors[] = "Field '{$fieldName}' variant at index {$index} attribute at index {$attrIndex} must be an object with 'name' and 'value' fields";
247+
continue;
248+
}
242249

243-
if (!isset($attribute['name']) || !isset($attribute['value'])) {
244-
$errors[] = "Field '{$fieldName}' variant at index {$index} attribute '{$attrName}' must have 'name' and 'value' fields";
245-
continue;
246-
}
250+
if (!isset($attribute['name']) || !isset($attribute['value'])) {
251+
$errors[] = "Field '{$fieldName}' variant at index {$index} attribute at index {$attrIndex} must have 'name' and 'value' fields";
252+
continue;
253+
}
247254

248-
if (!is_string($attribute['name']) || $attribute['name'] !== $attrName) {
249-
$errors[] = "Field '{$fieldName}' variant at index {$index} attribute '{$attrName}' name field must match the attribute key";
250-
}
255+
if (!is_string($attribute['name']) || empty($attribute['name'])) {
256+
$errors[] = "Field '{$fieldName}' variant at index {$index} attribute at index {$attrIndex} 'name' must be a non-empty string";
257+
continue;
258+
}
251259

252-
// Validate the attribute value against the field config
253-
$attrErrors = $this->validateField(
254-
"{$fieldName}.variants[{$index}].attributes.{$attrName}.value",
255-
$attribute['value'],
256-
$attrConfig
257-
);
258-
$errors = array_merge($errors, $attrErrors);
259-
}
260+
// Validate the attribute value against the field config if available
261+
if ($fieldConfig->attributes !== null && isset($fieldConfig->attributes[$attribute['name']])) {
262+
$attrConfig = $fieldConfig->attributes[$attribute['name']];
263+
$attrErrors = $this->validateField(
264+
"{$fieldName}.variants[{$index}].attributes[{$attrIndex}].value",
265+
$attribute['value'],
266+
$attrConfig
267+
);
268+
$errors = array_merge($errors, $attrErrors);
260269
}
261270
}
262271
}

0 commit comments

Comments
 (0)