Skip to content

Commit

Permalink
add extension function to validate container/annotation names
Browse files Browse the repository at this point in the history
  • Loading branch information
brambg committed Aug 9, 2023
1 parent db16668 commit 0f979ef
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
13 changes: 13 additions & 0 deletions common/src/main/kotlin/nl/knaw/huc/annorepo/api/extensions.kt
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()
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()
}
}

}

0 comments on commit 0f979ef

Please sign in to comment.