File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed
Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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+ use ZBan \Tests \PHPUnit \Framework \ZBanTestCase ;
10+
11+ /**
12+ * @covers \frictionlessdata\tableschema\Fields\BaseField
13+ */
14+ class BaseFieldTest extends TestCase
15+ {
16+ public function testPreserveOriginalValueInValidationError (): void
17+ {
18+ $ descriptor = (object ) [
19+ 'name ' => 'date_col ' ,
20+ 'constraints ' => (object ) ['minimum ' => '2025-07-01 ' ]
21+ ];
22+
23+ $ sut = new class ($ descriptor ) extends BaseField {
24+ protected function validateCastValue ($ val )
25+ {
26+ // If the logic is wrong, this object will be in the error
27+ // instead of the original date string.
28+ return new \DateTimeImmutable ($ val );
29+ }
30+ };
31+
32+ $ validatedValue = '2025-06-30 ' ;
33+ $ errors = $ sut ->validateValue ($ validatedValue );
34+
35+ self ::assertCount (1 , $ errors );
36+ $ error = reset ($ errors );
37+ self ::assertSame (
38+ $ validatedValue ,
39+ $ error ->extraDetails ['value ' ]
40+ );
41+ }
42+ }
You can’t perform that action at this time.
0 commit comments