Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:8.2-cli
FROM php:8.1-cli

RUN apt-get update -y

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ services:
volumes:
- .:/src
command: bash
network_mode: host
6 changes: 6 additions & 0 deletions src/Fields/BaseField.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ public function validateValue($val)

return [];
} catch (FieldValidationException $e) {
foreach ($e->validationErrors as $ve) {
// Replace the cast-value for the violation, with the original value.
// This so the error message contains the original representation of the invalid value.
$ve->extraDetails['value'] = $val;
}

return $e->validationErrors;
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,12 @@ public static function provideInvalidDataForConstraint(): array
{
return [
['name: value does not match pattern ("123")', 'string', ['pattern' => '3.*'], '123'],
['name: value not in enum (4)', 'integer', ['enum' => ['1', '2', 3]], '4'],
['name: value not in enum ("4")', 'integer', ['enum' => ['1', '2', 3]], '4'],
['name: value not in enum (4)', 'integer', ['enum' => ['1', '2', 3]], 4],
['name: value is below minimum (0)', 'integer', ['minimum' => 1], 0],
['name: value is below minimum (0)', 'integer', ['minimum' => 1], '0'],
['name: value is below minimum ("0")', 'integer', ['minimum' => 1], '0'],
['name: value is above maximum (2)', 'integer', ['maximum' => 1], 2],
['name: value is above maximum (2)', 'integer', ['maximum' => 1], '2'],
['name: value is above maximum ("2")', 'integer', ['maximum' => 1], '2'],
['name: value is below minimum length ("a")', 'string', ['minLength' => 2], 'a'],
['name: value is above maximum length ("aaa")', 'string', ['maxLength' => 2], 'aaa'],
];
Expand Down
41 changes: 41 additions & 0 deletions tests/Fields/BaseFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Fields;

use frictionlessdata\tableschema\Fields\BaseField;
use PHPUnit\Framework\TestCase;

/**
* @covers \frictionlessdata\tableschema\Fields\BaseField
*/
class BaseFieldTest extends TestCase
{
public function testPreserveOriginalValueInValidationError(): void
{
$descriptor = (object) [
'name' => 'date_col',
'constraints' => (object) ['minimum' => '2025-07-01'],
];

$sut = new class($descriptor) extends BaseField {
protected function validateCastValue($val)
{
// If the logic is wrong, this object will be in the error
// instead of the original date string.
return new \DateTimeImmutable($val);
}
};

$validatedValue = '2025-06-30';
$errors = $sut->validateValue($validatedValue);

self::assertCount(1, $errors);
$error = reset($errors);
self::assertSame(
$validatedValue,
$error->extraDetails['value']
);
}
}