Files
pricebuddy/app/Rules/ScrapeStrategyValue.php
Jeremy 482fe587ae fix: share one scrape strategy value rule across all three endpoints
A schema_org strategy could be tested but not saved. meta-extraction requires
`value` to be null for schema_org and non-empty for every other type, while the
write endpoints required it unconditionally (create) or whenever a type was
present (update). The extension's Tune tab defaults all three fields to
schema_org and omits `value`, so "Test all" succeeded and "Save to store" 422'd
on the identical strategy. No single payload satisfied both, so any store whose
fields are detected via schema.org could not be saved at all.

Extracts the meta-extraction closure into App\Rules\ScrapeStrategyValue and
points all three requests at it. The rule locates the sibling `type` relative to
the attribute under validation, so it works at any path without a configured
prefix, and is implicit so it still runs when `value` is absent - which is both
how a schema_org strategy is legitimately sent and how a selector with no
expression is caught. Which types need a value stays owned by
ScraperStrategyType::needsValue().

A saved schema_org strategy round-trips as `{"type": "schema_org"}` with no
value key at all - StandardStrategyDto drops null values rather than persisting
them - and that shape is valid input to all three endpoints. Covered by a test
that creates a store, reads it back and posts it to meta-extraction.

Also adds the stores_filter_domain capability to /api/client-config, so clients
stop inferring filter[domain] support from products_filter_url.
2026-08-08 12:29:02 +10:00

85 lines
2.6 KiB
PHP

<?php
namespace App\Rules;
use App\Enums\ScraperStrategyType;
use Closure;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Str;
/**
* Validates a scrape strategy's `value` against its sibling `type`.
*
* `schema_org` reads the page's embedded metadata and takes no value, so one must not be
* given; every other type needs a non-empty extraction expression. Shared by
* meta-extraction and the store write endpoints, which previously disagreed: the
* extension could test a schema_org strategy and then fail to save the identical payload.
*
* The sibling `type` is located relative to the attribute under validation, so the same
* rule works at any path (`scrape_strategy.title.value`,
* `store.scrape_strategy.title.value`).
*/
class ScrapeStrategyValue implements DataAwareRule, ValidationRule
{
/**
* Run even when `value` is absent — that is both how a schema_org strategy is
* legitimately sent and how a selector with no expression must be caught.
*/
public bool $implicit = true;
/**
* @var array<string, mixed>
*/
protected array $data = [];
/**
* @param array<string, mixed> $data
*/
public function setData(array $data): static
{
$this->data = $data;
return $this;
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$type = data_get($this->data, Str::beforeLast($attribute, '.').'.type');
// No type in this payload (an optional strategy the client simply did not send, or
// a partial update), so there is nothing to require a value for. A value sent on
// its own must still be usable if it is there at all.
if (blank($type)) {
if (! is_null($value) && ! $this->isNonEmptyString($value)) {
$fail("The {$attribute} field must be a non-empty string.");
}
return;
}
if (! ScraperStrategyType::needsValue((string) $type)) {
if (! is_null($value)) {
$fail("The {$attribute} field must be null when using {$type}.");
}
return;
}
if (is_null($value)) {
$fail("The {$attribute} field is required when using {$type}.");
return;
}
if (! $this->isNonEmptyString($value)) {
$fail("The {$attribute} field must be a non-empty string.");
}
}
protected function isNonEmptyString(mixed $value): bool
{
return is_string($value) && trim($value) !== '';
}
}