Skip to content

Commit c184ff4

Browse files
validate ProjectSpace names (#249)
* validate ProjectSpace names Co-authored-by: Sven F. <sven.fillinger@qbic.uni-tuebingen.de>
1 parent 920a36f commit c184ff4

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ This project adheres to `Semantic Versioning <https://semver.org/>`_.
1313
* New properties ``internalUnitPrice``, ``externalUnitPrice`` and ``serviceProvider`` for the ``life.qbic.datamodel.dtos.business.services.Product`` and its derivatives (`#245 <https://github.com/qbicsoftware/data-model-lib/pull/245>`_)
1414
* New properties ``totalPrice`` and ``totalDiscountPrice`` for the ``life.qbic.datamodel.dtos.business.ProductItem`` and ``totalDiscountPrice`` for the ``life.qbic.datamodel.dtos.business.Offer``
1515
* A ``life.qbic.datamodel.dtos.business.facilities.FacilityFactory`` to get the facility for a given string representation (`#247 <https://github.com/qbicsoftware/data-model-lib/pull/247>`_)
16+
1617
**Fixed**
1718

19+
* ProjectSpace names are now validated (`#249 <https://github.com/qbicsoftware/data-model-lib/pull/249>`_)
20+
1821
**Dependencies**
1922

2023
**Deprecated**

src/main/groovy/life/qbic/datamodel/dtos/projectmanagement/ProjectSpace.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,24 @@ final class ProjectSpace {
2323
*/
2424
final String name
2525

26+
/**
27+
* Alphanumerical characters, underscores and '-' allowed, German umlauts will lead to a mismatch
28+
*/
29+
private static final def SPACE_NAME_REGEX = ~'[-\\w]+'
30+
2631
ProjectSpace(String name) {
2732
final def space = Objects.requireNonNull(name, "Space name must not be null.")
33+
validateName(name)
2834
this.name = formatSpaceName(space)
2935
}
3036

37+
private void validateName(String name) {
38+
// validation happens with the formatted name, but the input name must be returned so the user does not get confused
39+
if(! SPACE_NAME_REGEX.matcher(formatSpaceName(name)).matches()) {
40+
throw new IllegalArgumentException("${name} is not a valid project code.")
41+
}
42+
}
43+
3144
private static String formatSpaceName(String name) {
3245
def capitalizedName = name.trim().toUpperCase()
3346
def refactoredName = capitalizedName.replaceAll("\\s+", "_")

src/test/groovy/life/qbic/datamodel/dtos/projectmanagement/ProjectSpaceSpec.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ class ProjectSpaceSpec extends Specification {
2020
space.name.equals("MY_NEW_SPACE")
2121
}
2222

23+
def "A String violating the space name format standard shall throw an IllegalArgumentException" () {
24+
given:
25+
String invalidCode = "Plausibilitätsüberprüfungs_Space"
26+
27+
when:
28+
new ProjectSpace(invalidCode)
29+
30+
then:
31+
thrown(IllegalArgumentException)
32+
33+
}
34+
2335
def "Dashes are replaced by underscores"() {
2436
given:
2537
String projectSpaceName = "my-new-space"

0 commit comments

Comments
 (0)