diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5f41912..ecbf86d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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') }} diff --git a/Dockerfile b/Dockerfile index fc88748..f707c89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM php:8.2-cli +FROM php:8.1-cli RUN apt-get update -y diff --git a/docker-compose.yml b/docker-compose.yml index 14c1b20..e437b11 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,3 +4,4 @@ services: volumes: - .:/src command: bash + network_mode: host diff --git a/src/Fields/BaseField.php b/src/Fields/BaseField.php index d3074b5..ffcc22e 100644 --- a/src/Fields/BaseField.php +++ b/src/Fields/BaseField.php @@ -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; } } diff --git a/tests/FieldTest.php b/tests/FieldTest.php index 29cb120..9b269ac 100644 --- a/tests/FieldTest.php +++ b/tests/FieldTest.php @@ -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'], ]; diff --git a/tests/Fields/BaseFieldTest.php b/tests/Fields/BaseFieldTest.php new file mode 100644 index 0000000..6c594f3 --- /dev/null +++ b/tests/Fields/BaseFieldTest.php @@ -0,0 +1,41 @@ + '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'] + ); + } +}