diff --git a/src/Schema/TypeFormats/StringDate.php b/src/Schema/TypeFormats/StringDate.php index 6bf8a65a..f36774b7 100644 --- a/src/Schema/TypeFormats/StringDate.php +++ b/src/Schema/TypeFormats/StringDate.php @@ -15,6 +15,11 @@ public function __invoke($value): bool { // full-date notation as defined by RFC 3339, section 5.6, for example, 2017-07-21 - return DateTime::createFromFormat('Y-m-d', $value) !== false; + $datetime = DateTime::createFromFormat('Y-m-d', $value); + if ($datetime === false) { + return false; + } + + return $datetime->format('Y-m-d') === $value; } } diff --git a/tests/Schema/TypeFormats/StringDateTest.php b/tests/Schema/TypeFormats/StringDateTest.php new file mode 100644 index 00000000..17b47784 --- /dev/null +++ b/tests/Schema/TypeFormats/StringDateTest.php @@ -0,0 +1,54 @@ +assertTrue((new StringDate())($date)); + } + + /** + * @return string[][] + */ + public function dateGreenDataProvider(): array + { + return [ + ['1985-04-12'], + ['1937-01-01'], + ['1996-12-19'], + ['1990-12-31'], + ]; + } + + /** + * @dataProvider dateRedDataProvider + */ + public function testRedDateTypeFormat(string $date): void + { + $this->assertFalse((new StringDate())($date)); + } + + /** + * @return string[][] + */ + public function dateRedDataProvider(): array + { + return [ + ['2021-0-32'], + ['2021-09-32'], + ['0000-00-00'], + [''], + ['somestring'], + ]; + } +}