From 337bb5fc6c75e839c1516670e6a00165b231cd76 Mon Sep 17 00:00:00 2001 From: Thorge Petersen <petersen@rz.uni-kiel.de> Date: Mon, 16 Dec 2024 06:08:35 +0100 Subject: [PATCH] Updated 'tag_name_validator' to replace newlines with whitespaces --- CHANGELOG.md | 8 ++++++-- ckanext/odsh/validation.py | 14 +++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a549e51..7b105b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,14 +9,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Replaced `schema:startDate` with `dcat:startDate` and `schema:endDate` with `dcat:endDate` in test files. +- Updated `tag_name_validator` to replace newlines with whitespaces and ensure tags are non-empty during validation. -## [2.4.4] +## [2.4.4] 2024-09-23 ### Added - Added an alert in the dataset edit form if a GUID was detected in the dataset, advising users to avoid manual editing and to contact the publisher for any data change requests. +### Changed + +- Replaced `schema:startDate` with `dcat:startDate` and `schema:endDate` with `dcat:endDate` in test files. + ### Fixed - Fix search input behavior to remember the last entered value. diff --git a/ckanext/odsh/validation.py b/ckanext/odsh/validation.py index 2131bfa..b49d362 100644 --- a/ckanext/odsh/validation.py +++ b/ckanext/odsh/validation.py @@ -293,12 +293,16 @@ def validate_formats(data, errors): def tag_name_validator(value, context): - """Allow tag name to contain any characters but no newlines + """Validate tag name to ensure it is non-empty and contains no line breaks. + Replaces any newlines with spaces before validation. """ - tagname_match = re.compile(r'^(?=.*[^\n])[^\n]*$', re.UNICODE) - if not tagname_match.match(value): - raise toolkit.Invalid( - _('Invalid tag: "%s". Tags cannot contain line breaks.') % (value)) + # Replace all newlines (\n, \r) with spaces + value = re.sub(r'[\r\n]+', ' ', value).strip() + + # Ensure the tag is non-empty + if not value: + raise toolkit.Invalid(_('Invalid tag: Tags cannot be empty.')) + return value -- GitLab