Skip to content

Commit

Permalink
Merge pull request #22 from GenomicDataInfrastructure/fix-tags-valida…
Browse files Browse the repository at this point in the history
…tion

fix: improve tags validation
  • Loading branch information
hcvdwerf authored May 27, 2024
2 parents 00e7a43 + 2793ec8 commit 4a39868
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
19 changes: 12 additions & 7 deletions ckanext/fairdatapoint/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,19 @@ def validate_tags(values_list: List[Dict]) -> List:
tags = []
for item in values_list:
tag_value = item['name']
find_illegal = re.search(illegal_pattern, tag_value)
if find_illegal:
log.warning(f'Tag {tag_value} contains values other than alphanumeric characters, spaces, hyphens, '
f'underscores or dots, they will be replaces with spaces')
tag = {'name': re.sub(illegal_pattern, ' ', tag_value)}
tags.append(tag)
if len(tag_value) < 2:
log.warning(f'Tag {tag_value} is shorter than 2 characters and will be removed')
elif len(tag_value) > 100:
log.warning(f'Tag {tag_value} is longer than 100 characters and will be removed')
else:
tags.append(item)
find_illegal = re.search(illegal_pattern, tag_value)
if find_illegal:
log.warning(f'Tag {tag_value} contains values other than alphanumeric characters, spaces, hyphens, '
f'underscores or dots, they will be replaces with spaces')
tag = {'name': re.sub(illegal_pattern, ' ', tag_value)}
tags.append(tag)
else:
tags.append(item)
return tags


Expand Down
3 changes: 3 additions & 0 deletions ckanext/fairdatapoint/tests/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
([{"name": "CNS/Brain"}], [{"name": "CNS Brain"}]),
([{"name": "COVID-19"}, {"name": "3`-DNA"}], [{"name": "COVID-19"}, {"name": "3 -DNA"}]),
([{"name": "something-1.1"}, {"name": "breast cancer"}], [{"name": "something-1.1"}, {"name": "breast cancer"}]),
([{"name": "-"}], []),
([{"name": "It is a ridiculously long (more 100 chars) text for a tag therefore it should be removed from the "
"result to prevent CKAN harvester from failing"}], []),
([], [])
])
def test_validate_tags(input_tags, expected_tags):
Expand Down

0 comments on commit 4a39868

Please sign in to comment.