Skip to content

Commit

Permalink
closes #173
Browse files Browse the repository at this point in the history
  • Loading branch information
xrotwang committed Apr 26, 2024
1 parent b3f24f9 commit 629d924
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The `pycldf` package adheres to [Semantic Versioning](http://semver.org/spec/v2.
by `json.dumps`.
- Fixed bug whereby SQLite conversion would fail when the name of a renamed column clashed with an
existing column name in the table.
- Emit warning when encountering invalid main part for mediaType property.


## [1.37.1] - 2024-03-18
Expand Down
11 changes: 11 additions & 0 deletions src/pycldf/validators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import warnings
import functools


Expand Down Expand Up @@ -37,6 +38,12 @@ def valid_grammaticalityJudgement(dataset, table, column, row):
raise ValueError('Glottolog language linked from ungrammatical example')


def valid_mediaType(dataset, table, column, row):
main, _, sub = row[column.name].partition('/')
if not re.fullmatch('[a-z]+', main):
warnings.warn('Invalid main part in media type: {}'.format(main))


VALIDATORS = [
(
None,
Expand All @@ -58,4 +65,8 @@ def valid_grammaticalityJudgement(dataset, table, column, row):
None,
'http://cldf.clld.org/v1.0/terms.rdf#grammaticalityJudgement',
valid_grammaticalityJudgement),
(
None,
'http://cldf.clld.org/v1.0/terms.rdf#mediaType',
valid_mediaType),
]
11 changes: 11 additions & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import zipfile
import warnings
import mimetypes
import contextlib

import pytest
Expand Down Expand Up @@ -177,6 +178,16 @@ def test_example_validators(ds):
ds.validate()


def test_invalid_mimetype(ds, recwarn):
ds.add_component('MediaTable')
ds.write(MediaTable=[{
'ID': '1',
'Media_Type': mimetypes.guess_type('f.png'),
'Download_URL': 'http://example.org'}])
ds.validate()
assert 'Invalid main part' in str(recwarn.pop(UserWarning).message)


def test_regex_validator_for_listvalued_column(ds):
ds.add_table(
'test',
Expand Down

0 comments on commit 629d924

Please sign in to comment.