[Support] precognition requests via middleware #343
zdravstvuy1714
started this conversation in
Ideas
Replies: 1 comment 15 replies
-
The following workaround adds Precognition support to Laravel Data. It intercepts the pipeline if the payload is a precognitive request, then filters the rules based on the precognition headers and validates the payload. Otherwise, it will continue as usual.
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator;
use Spatie\LaravelData\DataPipes\ValidatePropertiesDataPipe;
use Spatie\LaravelData\Support\DataClass;
class PrecognitivelyValidatePropertiesDataPipe extends ValidatePropertiesDataPipe
{
public function handle(mixed $payload, DataClass $class, Collection $properties): Collection
{
if ($payload instanceof Request && $payload->isPrecognitive()) {
Validator::make(
$properties->toArray(),
$payload->filterPrecognitiveRules(($class->name)::getValidationRules($properties->toArray())),
)->validate();
return $properties;
}
return parent::handle($payload, $class, $properties);
}
}
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->app->bind(ValidatePropertiesDataPipe::class, PrecognitivelyValidatePropertiesDataPipe::class);
}
} |
Beta Was this translation helpful? Give feedback.
15 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
implement support for laravel/framework#44339 Laravel feature
Beta Was this translation helpful? Give feedback.
All reactions