Skip to content

Commit

Permalink
Merge pull request #297 from ckan/fix-date-validator-empty-values
Browse files Browse the repository at this point in the history
Fix DCAT date validator on empty values
  • Loading branch information
amercader authored Sep 12, 2024
2 parents cb25389 + d974c3b commit eb83433
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down
18 changes: 18 additions & 0 deletions ckanext/dcat/tests/logic/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 9 additions & 2 deletions ckanext/dcat/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit eb83433

Please sign in to comment.