diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fd439a9..a4222837 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## [Unreleased](https://github.com/ckan/ckanext-dcat/compare/v2.0.0...HEAD) -* Add support for hydra collection type PartialCollectionView +* Fix DCAT date validator on empty values ([#297](https://github.com/ckan/ckanext-dcat/pull/297)) +* Add support for hydra collection type PartialCollectionView ([#299](https://github.com/ckan/ckanext-dcat/pull/299)) ## [v2.0.0](https://github.com/ckan/ckanext-dcat/compare/v1.7.0...v2.0.0) - 2024-08-30 @@ -117,7 +118,7 @@ ## [v1.1.0](https://github.com/ckan/ckanext-dcat/compare/v1.0.0...v1.1.0) - 2020-03-12 -* Python 3 support and new pytest based test suite ([#174](https://github.com/ckan/ckanext-dcat/pull/174)) +* Python 3 support and new pytest based test suite ([#174](https://github.com/ckan/ckanext-dcat/pull/174))painful * Fix `after_show - set_titles` in plugins.py ([#172](https://github.com/ckan/ckanext-dcat/pull/172)) * Add support for DCT.rightsStatement in DCT.accessRights and DCT.rights ([#177](https://github.com/ckan/ckanext-dcat/pull/177)) * Add support for additional vcard representations ([#178](https://github.com/ckan/ckanext-dcat/pull/178)) 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: