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
30 changes: 16 additions & 14 deletions src/Fields/BaseField.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,25 @@ final public function castValue($val)
}

return null;
} else {
$val = $this->validateCastValue($val);
if (!$this->constraintsDisabled) {
$validationErrors = $this->checkConstraints($val);
if (count($validationErrors) > 0) {
throw new FieldValidationException($validationErrors);
}

$castVal = $this->validateCastValue($val);

if (!$this->constraintsDisabled) {
$validationErrors = $this->checkConstraints($castVal);

if (count($validationErrors) > 0) {
foreach ($validationErrors as $i => $schemaError) {
\assert($schemaError instanceof SchemaValidationError);
$schemaError->extraDetails['value'] = $val;
$validationErrors[$i] = $schemaError;
}
}

return $val;
throw new FieldValidationException($validationErrors);
}
}

return $castVal;
}

public function validateValue($val)
Expand All @@ -171,12 +179,6 @@ 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
43 changes: 42 additions & 1 deletion tests/Fields/BaseFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Fields;

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

Expand All @@ -12,7 +13,7 @@
*/
class BaseFieldTest extends TestCase
{
public function testPreserveOriginalValueInValidationError(): void
public function testPreserveOriginalValueInValidateError(): void
{
$descriptor = (object) [
'name' => 'date_col',
Expand All @@ -38,4 +39,44 @@ protected function validateCastValue($val)
$error->extraDetails['value']
);
}

public function testPreserveOriginalValueInCastError(): 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';
$exception = null;

try {
$sut->castValue($validatedValue);
} catch (FieldValidationException $exception) {
}

self::assertNotNull($exception, 'An exception is expected for this test.');
self::assertSame(
'date_col: value is below minimum ("2025-06-30")',
$exception->getMessage()
);

$errors = $exception->validationErrors;

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