Skip to content

Commit b52da8e

Browse files
feat: Update validation error to include original value
Prior, the validation error may contain the cast-value which can be confusing when reporting the validation error to users.
1 parent aa39c44 commit b52da8e

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM php:8.2-cli
1+
FROM php:8.1-cli
22

33
RUN apt-get update -y
44

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ services:
44
volumes:
55
- .:/src
66
command: bash
7+
network_mode: host

src/Fields/BaseField.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ public function validateValue($val)
171171

172172
return [];
173173
} catch (FieldValidationException $e) {
174+
foreach ($e->validationErrors as $ve) {
175+
// Replace the cast-value for the violation, with the original value.
176+
// This so the error message contains the original representation of the invalid value.
177+
$ve->extraDetails['value'] = $val;
178+
}
179+
174180
return $e->validationErrors;
175181
}
176182
}

tests/FieldTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,12 @@ public static function provideInvalidDataForConstraint(): array
475475
{
476476
return [
477477
['name: value does not match pattern ("123")', 'string', ['pattern' => '3.*'], '123'],
478-
['name: value not in enum (4)', 'integer', ['enum' => ['1', '2', 3]], '4'],
478+
['name: value not in enum ("4")', 'integer', ['enum' => ['1', '2', 3]], '4'],
479479
['name: value not in enum (4)', 'integer', ['enum' => ['1', '2', 3]], 4],
480480
['name: value is below minimum (0)', 'integer', ['minimum' => 1], 0],
481-
['name: value is below minimum (0)', 'integer', ['minimum' => 1], '0'],
481+
['name: value is below minimum ("0")', 'integer', ['minimum' => 1], '0'],
482482
['name: value is above maximum (2)', 'integer', ['maximum' => 1], 2],
483-
['name: value is above maximum (2)', 'integer', ['maximum' => 1], '2'],
483+
['name: value is above maximum ("2")', 'integer', ['maximum' => 1], '2'],
484484
['name: value is below minimum length ("a")', 'string', ['minLength' => 2], 'a'],
485485
['name: value is above maximum length ("aaa")', 'string', ['maxLength' => 2], 'aaa'],
486486
];

tests/Fields/BaseFieldTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fields;
6+
7+
use frictionlessdata\tableschema\Fields\BaseField;
8+
use PHPUnit\Framework\TestCase;
9+
10+
/**
11+
* @covers \frictionlessdata\tableschema\Fields\BaseField
12+
*/
13+
class BaseFieldTest extends TestCase
14+
{
15+
public function testPreserveOriginalValueInValidationError(): void
16+
{
17+
$descriptor = (object) [
18+
'name' => 'date_col',
19+
'constraints' => (object) ['minimum' => '2025-07-01'],
20+
];
21+
22+
$sut = new class($descriptor) extends BaseField {
23+
protected function validateCastValue($val)
24+
{
25+
// If the logic is wrong, this object will be in the error
26+
// instead of the original date string.
27+
return new \DateTimeImmutable($val);
28+
}
29+
};
30+
31+
$validatedValue = '2025-06-30';
32+
$errors = $sut->validateValue($validatedValue);
33+
34+
self::assertCount(1, $errors);
35+
$error = reset($errors);
36+
self::assertSame(
37+
$validatedValue,
38+
$error->extraDetails['value']
39+
);
40+
}
41+
}

0 commit comments

Comments
 (0)