-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add extension function to validate container/annotation names
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
common/src/main/kotlin/nl/knaw/huc/annorepo/api/extensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package nl.knaw.huc.annorepo.api | ||
|
||
private val restrictedChars = | ||
setOf(';', ',', '/', '?', ':', '@', '&', '=', '+', '$', '.', '!', '~', '*', '\'', '(', ')', '#', '"', '%') | ||
|
||
fun String.isValidAnnotationName(): Boolean { | ||
val isAscii = this.all { it.code in 0..127 } | ||
val hasNoWhitespace = this.none { it.isWhitespace() } | ||
val hasNoRestrictedChars = this.none { it in restrictedChars } | ||
return isAscii && hasNoWhitespace && hasNoRestrictedChars | ||
} | ||
|
||
fun String.isValidContainerName(): Boolean = isValidAnnotationName() |
31 changes: 31 additions & 0 deletions
31
common/src/test/kotlin/nl/knaw/huc/annorepo/api/ExtensionsKtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package nl.knaw.huc.annorepo.api | ||
|
||
import org.junit.jupiter.api.Test | ||
import org.assertj.core.api.Assertions.assertThat | ||
|
||
class ExtensionsTest() { | ||
@Test | ||
fun `these names are not valid`() { | ||
val invalidNames = listOf( | ||
"spaces are not allowed in annotation names", | ||
"behold:a_name" | ||
) | ||
invalidNames.forEach { | ||
assertThat(it.isValidAnnotationName()).withFailMessage { "$it should be an invalid name" }.isFalse() | ||
assertThat(it.isValidContainerName()).withFailMessage { "$it should be an invalid name" }.isFalse() | ||
} | ||
} | ||
|
||
@Test | ||
fun `these names are valid`() { | ||
val validNames = listOf( | ||
"spacesarenotallowedinannotationnames", | ||
"behold-a_name" | ||
) | ||
validNames.forEach { | ||
assertThat(it.isValidAnnotationName()).withFailMessage { "$it should be a valid name" }.isTrue() | ||
assertThat(it.isValidContainerName()).withFailMessage { "$it should be a valid name" }.isTrue() | ||
} | ||
} | ||
|
||
} |