Skip to content

Commit eb23493

Browse files
allow precognitive requests to use wildcards (#57437)
1 parent 500c837 commit eb23493

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

src/Illuminate/Http/Concerns/CanBePrecognitive.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,20 @@ public function filterPrecognitiveRules($rules)
1818
return $rules;
1919
}
2020

21+
$validateOnly = explode(',', $this->header('Precognition-Validate-Only'));
22+
$validateOnlyPatterns = (new Collection($validateOnly))
23+
->map(fn ($pattern) => '/^'.str_replace('\*', '\d+', preg_quote($pattern, '/')).'$/');
24+
2125
return (new Collection($rules))
22-
->only(explode(',', $this->header('Precognition-Validate-Only')))
23-
->all();
26+
->filter(function ($rule, $ruleKey) use ($validateOnlyPatterns) {
27+
foreach ($validateOnlyPatterns as $pattern) {
28+
if (preg_match($pattern, $ruleKey)) {
29+
return true;
30+
}
31+
}
32+
33+
return false;
34+
})->all();
2435
}
2536

2637
/**

tests/Integration/Routing/PrecognitionTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,51 @@ public function testClientCanSpecifyInputsToValidateWhenUsingControllerValidateW
456456
]);
457457
}
458458

459+
public function testClientCanSpecifySimpleArrayWildcardToValidateWhenUsingControllerValidateWithPassingArrayOfRules()
460+
{
461+
Route::post('test-route', [PrecognitionTestController::class, 'methodWhereArrayRulesAreValidateViaControllerValidate'])
462+
->middleware(PrecognitionInvokingController::class);
463+
464+
$response = $this->postJson('test-route', [
465+
'raw_array' => [123, 'someString'],
466+
], [
467+
'Precognition' => 'true',
468+
'Precognition-Validate-Only' => 'raw_array.*',
469+
]);
470+
471+
$response->assertUnprocessable();
472+
$response->assertHeaderMissing('Precognition-Success');
473+
$response->assertJsonPath('errors', [
474+
'raw_array.0' => [
475+
'The raw_array.0 field must be a string.',
476+
],
477+
]);
478+
}
479+
480+
public function testClientCanSpecifyComplexArrayWildcardToValidateWhenUsingControllerValidateWithPassingArrayOfRules()
481+
{
482+
Route::post('test-route', [PrecognitionTestController::class, 'methodWhereArrayRulesAreValidateViaControllerValidate'])
483+
->middleware(PrecognitionInvokingController::class);
484+
485+
$response = $this->postJson('test-route', [
486+
'nested_array' => [
487+
['name' => 123],
488+
['name' => 'someString'],
489+
],
490+
], [
491+
'Precognition' => 'true',
492+
'Precognition-Validate-Only' => 'nested_array.*.name',
493+
]);
494+
495+
$response->assertUnprocessable();
496+
$response->assertHeaderMissing('Precognition-Success');
497+
$response->assertJsonPath('errors', [
498+
'nested_array.0.name' => [
499+
'The nested_array.0.name field must be a string.',
500+
],
501+
]);
502+
}
503+
459504
public function testItAppendsAnAdditionalVaryHeaderInsteadOfReplacingAnyExistingVaryHeaders()
460505
{
461506
Route::get('test-route', function () {
@@ -1117,6 +1162,22 @@ public function methodWhereNestedRulesAreValidatedViaControllerValidate(Request
11171162
fail();
11181163
}
11191164

1165+
public function methodWhereArrayRulesAreValidateViaControllerValidate(Request $request)
1166+
{
1167+
precognitive(function () use ($request) {
1168+
$this->validate($request, [
1169+
'nested_array' => ['required', 'array', 'min:1'],
1170+
'nested_array.*.name' => ['required', 'string'],
1171+
'raw_array' => ['required', 'array', 'min:1'],
1172+
'raw_array.*' => ['required', 'string'],
1173+
]);
1174+
1175+
fail();
1176+
});
1177+
1178+
fail();
1179+
}
1180+
11201181
public function methodWhereNestedRulesAreValidatedViaControllerValidateWith(Request $request)
11211182
{
11221183
precognitive(function () {

0 commit comments

Comments
 (0)