Skip to content

Commit 5231036

Browse files
committed
Improve request validator
1 parent bd0176b commit 5231036

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/Lodash/Http/Requests/RestrictsExtraAttributes.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function array_diff;
1313
use function array_keys;
1414
use function config;
15+
use function in_array;
1516
use function method_exists;
1617
use function preg_replace;
1718

@@ -20,15 +21,38 @@
2021
*/
2122
trait RestrictsExtraAttributes
2223
{
24+
protected bool $checkForExtraProperties = true;
25+
protected bool $checkForEmptyPayload = true;
26+
protected array $methodsForEmptyPayload = ['PATCH'];
27+
2328
protected function prepareForValidation(): void
2429
{
2530
$this->checkForNotAllowedProperties();
2631

32+
$this->checkForEmptyPayload();
33+
2734
parent::prepareForValidation();
2835
}
2936

37+
private function checkForEmptyPayload(): void
38+
{
39+
if (! $this->checkForEmptyPayload) {
40+
return;
41+
}
42+
43+
if (in_array($this->method(), $this->methodsForEmptyPayload, true)) {
44+
if (empty($this->input())) {
45+
throw ValidationException::withMessages(['general' => [__('validation.payload_is_empty')]]);
46+
}
47+
}
48+
}
49+
3050
private function checkForNotAllowedProperties(): void
3151
{
52+
if (! $this->checkForExtraProperties) {
53+
return;
54+
}
55+
3256
$extraAttributes = array_diff(
3357
$this->getValidationData(),
3458
$this->getAllowedAttributes(),

0 commit comments

Comments
 (0)