From d90473e14373060c7b9791fe3c8198a2f91d9006 Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Thu, 3 Jul 2025 16:13:25 -0500 Subject: [PATCH] Fix date format validator to allow non-str objects Rejecting non-string data results in incorrect rejection of data in which `string` is one of the available types and the `date-time` format is in use. Fixes #571 --- CHANGELOG.rst | 3 +++ .../formats/implementations/rfc3339.py | 2 +- .../positive/date-format/instance.json | 1 + .../explicit-schema/positive/date-format/schema.json | 12 ++++++++++++ tests/unit/formats/test_rfc3339.py | 1 - 5 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 tests/example-files/explicit-schema/positive/date-format/instance.json create mode 100644 tests/example-files/explicit-schema/positive/date-format/schema.json diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f9aa780ce..8003c11c4 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,9 @@ Unreleased ---------- .. vendor-insert-here +- Fix a bug in the evaluation of the ``date-time`` format on non-string data, + which incorrectly rejected values for which ``string`` was one of several + valid types. Thanks :user:`katylava`! (:issue:`571`) 0.33.1 ------ diff --git a/src/check_jsonschema/formats/implementations/rfc3339.py b/src/check_jsonschema/formats/implementations/rfc3339.py index 03a073f7e..05ccf391c 100644 --- a/src/check_jsonschema/formats/implementations/rfc3339.py +++ b/src/check_jsonschema/formats/implementations/rfc3339.py @@ -57,7 +57,7 @@ def validate(date_str: object) -> bool: """Validate a string as a RFC3339 date-time.""" if not isinstance(date_str, str): - return False + return True if not RFC3339_REGEX.match(date_str): return False diff --git a/tests/example-files/explicit-schema/positive/date-format/instance.json b/tests/example-files/explicit-schema/positive/date-format/instance.json new file mode 100644 index 000000000..3c2359f00 --- /dev/null +++ b/tests/example-files/explicit-schema/positive/date-format/instance.json @@ -0,0 +1 @@ +{"some_date": null} diff --git a/tests/example-files/explicit-schema/positive/date-format/schema.json b/tests/example-files/explicit-schema/positive/date-format/schema.json new file mode 100644 index 000000000..de437857f --- /dev/null +++ b/tests/example-files/explicit-schema/positive/date-format/schema.json @@ -0,0 +1,12 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "object", + "properties": { + "some_date": { + "type": ["string", "null"], + "format": "date-time" + } + }, + "required": ["some_date"], + "additionalProperties": false +} diff --git a/tests/unit/formats/test_rfc3339.py b/tests/unit/formats/test_rfc3339.py index 87f61580e..84c096b5c 100644 --- a/tests/unit/formats/test_rfc3339.py +++ b/tests/unit/formats/test_rfc3339.py @@ -22,7 +22,6 @@ def test_simple_positive_cases(datestr): @pytest.mark.parametrize( "datestr", ( - object(), "2018-12-31T23:59:59", "2018-12-31T23:59:59+00:00Z", "2018-12-31 23:59:59",