From 76701536cee7168fd80ec673a95c44c37f2b3dd8 Mon Sep 17 00:00:00 2001 From: amercader Date: Mon, 2 Sep 2024 14:37:38 +0200 Subject: [PATCH 1/2] Fix DCAT date validator on empty values --- ckanext/dcat/tests/logic/test_validators.py | 18 ++++++++++++++++++ ckanext/dcat/validators.py | 11 +++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/ckanext/dcat/tests/logic/test_validators.py b/ckanext/dcat/tests/logic/test_validators.py index 700cc644..562bd067 100644 --- a/ckanext/dcat/tests/logic/test_validators.py +++ b/ckanext/dcat/tests/logic/test_validators.py @@ -90,9 +90,27 @@ def test_dcat_date_invalid(): invalid_values = [ "2024+07", "not_a_date", + True ] for value in invalid_values: data = {key: value} with pytest.raises(Invalid): dcat_date(key, data, errors, {}), value + + +def test_dcat_date_empty_values(): + + key = ("some_date",) + errors = {key: []} + valid_values = [ + None, + False, + "" + ] + + for value in valid_values: + data = {key: value} + dcat_date(key, data, errors, {}), value + + assert data[key] is None diff --git a/ckanext/dcat/validators.py b/ckanext/dcat/validators.py index c9ee7d50..9bf18d49 100644 --- a/ckanext/dcat/validators.py +++ b/ckanext/dcat/validators.py @@ -41,12 +41,19 @@ def is_date(value): def dcat_date(key, data, errors, context): value = data[key] - if isinstance(value, datetime.datetime): + if not value: + data[key] = None return - if is_year(value) or is_year_month(value) or is_date(value): + if isinstance(value, datetime.datetime): return + try: + if is_year(value) or is_year_month(value) or is_date(value): + return + except TypeError: + raise Invalid(_("Dates must be provided as strings or datetime objects")) + try: parse_date(value) except ValueError: From 748b23fc621462b8413b87f5e336bc2745b02287 Mon Sep 17 00:00:00 2001 From: amercader Date: Mon, 2 Sep 2024 14:53:36 +0200 Subject: [PATCH 2/2] Changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 059dcbfa..da0748b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## [Unreleased](https://github.com/ckan/ckanext-dcat/compare/v2.0.0...HEAD) -* ... +* Fix DCAT date validator on empty values ([#297](https://github.com/ckan/ckanext-dcat/pull/297)) ## [v2.0.0](https://github.com/ckan/ckanext-dcat/compare/v1.7.0...v2.0.0) - 2024-08-30