Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DCAT date validator on empty values #297

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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