Skip to content

Commit c7c175a

Browse files
committed
Use field name as the error array key
1 parent c8516eb commit c7c175a

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

src/Filterer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ private static function assertIfAliasExists($alias, bool $overwrite)
346346
private static function checkForUnknowns(array $leftOverInput, array $errors) : array
347347
{
348348
foreach ($leftOverInput as $field => $value) {
349-
$errors[] = "Field '{$field}' with value '" . trim(var_export($value, true), "'") . "' is unknown";
349+
$errors[$field] = "Field '{$field}' with value '" . trim(var_export($value, true), "'") . "' is unknown";
350350
}
351351

352352
return $errors;
@@ -364,7 +364,7 @@ private static function handleAllowUnknowns(bool $allowUnknowns, array $leftOver
364364
private static function handleRequiredFields(bool $required, string $field, array $errors) : array
365365
{
366366
if ($required) {
367-
$errors[] = "Field '{$field}' was required and not present";
367+
$errors[$field] = "Field '{$field}' was required and not present";
368368
}
369369
return $errors;
370370
}
@@ -402,7 +402,7 @@ private static function handleCustomError(
402402
);
403403
}
404404

405-
$errors[] = str_replace('{value}', trim(var_export($value, true), "'"), $error);
405+
$errors[$field] = str_replace('{value}', trim(var_export($value, true), "'"), $error);
406406
return $errors;
407407
}
408408

tests/FiltererTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,26 @@ public function filterReturnsResponseType()
245245
$this->assertSame([], $result->unknowns);
246246
}
247247

248+
/**
249+
* @test
250+
* @covers ::filter
251+
*/
252+
public function filterReturnsResponseTypeWithErrors()
253+
{
254+
$specification = ['name' => [['string']]];
255+
$input = ['name' => 'foo', 'id' => 1];
256+
$options = ['responseType' => Filterer::RESPONSE_TYPE_FILTER];
257+
258+
$result = Filterer::filter($specification, $input, $options);
259+
260+
$this->assertInstanceOf(FilterResponse::class, $result);
261+
$this->assertSame(false, $result->success);
262+
$this->assertSame(['name' => 'foo'], $result->filteredValue);
263+
$this->assertSame(['id' => "Field 'id' with value '1' is unknown"], $result->errors);
264+
$this->assertSame("Field 'id' with value '1' is unknown", $result->errorMessage);
265+
$this->assertSame(['id' => 1], $result->unknowns);
266+
}
267+
248268
/**
249269
* @test
250270
* @covers ::filter

0 commit comments

Comments
 (0)