Skip to content

Commit

Permalink
check for valid decimalDigits range
Browse files Browse the repository at this point in the history
  • Loading branch information
zingmane committed Aug 2, 2024
1 parent 43e8151 commit 1448441
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/main/scala/com/campudus/tableaux/helper/JsonUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ object JsonUtils extends LazyLogging {

val maxLength = Try(json.getInteger("maxLength").intValue()).toOption
val minLength = Try(json.getInteger("minLength").intValue()).toOption
val decimalDigits = Try(json.getInteger("decimalDigits").intValue()).toOption
val decimalDigits = parseDecimalDigits(json)

// languageType or deprecated multilanguage
// if languageType == 'country' countryCodes must be specified
Expand Down Expand Up @@ -283,6 +283,16 @@ object JsonUtils extends LazyLogging {
}
}

private def parseDecimalDigits(json: JsonObject): Option[Int] = {
val decimalDigits = Try(json.getInteger("decimalDigits").intValue()).toOption

decimalDigits.map({
case value if value > 10 || value < 0 =>
throw InvalidJsonException(s"Decimal digits must be between 0 and 10, but was $value.", "decimalDigits")
case value => value
})
}

def toRowValueSeq(json: JsonObject): Seq[Seq[_]] = {
(for {
checkedRowList <- toJsonObjectSeq("rows", json)
Expand Down Expand Up @@ -383,7 +393,7 @@ object JsonUtils extends LazyLogging {

val maxLength = getNullableJsonIntegerValue("maxLength", json).toOption
val minLength = getNullableJsonIntegerValue("minLength", json).toOption
val decimalDigits = getNullableJsonIntegerValue("decimalDigits", json).toOption
val decimalDigits = parseDecimalDigits(json)

(
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,28 @@ class ChangeStructureTest extends TableauxTestBase {

@Test
def changeColumnDecimalDigits(implicit c: TestContext): Unit = okTest {
val postJson = Json.obj("decimalDigits" -> 4)
val postJson = Json.obj("decimalDigits" -> 10)

for {
_ <- createDefaultTable()
resultPost <- sendRequest("POST", "/tables/1/columns/2", postJson)
resultGet <- sendRequest("GET", "/tables/1/columns/2")
} yield {
assertEquals(4, resultGet.getInteger("decimalDigits"))
assertEquals(10, resultGet.getInteger("decimalDigits"))
assertEquals(resultPost, resultGet)
}
}

@Test
def changeColumnDecimalDigitsTooHigh(implicit c: TestContext): Unit = exceptionTest("error.json.decimalDigits") {
val postJson = Json.obj("decimalDigits" -> 11)

for {
_ <- createDefaultTable()
resultPost <- sendRequest("POST", "/tables/1/columns/2", postJson)
} yield ()
}

@Test
def changeColumnKindWhichShouldFail(implicit c: TestContext): Unit = {
okTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class CreateColumnTest extends TableauxTestBase {
def createNumberColumnWithCustomDecimalDigits(implicit c: TestContext): Unit = {
okTest {
val createColumn1 = createNumberColumnJson("column1", None)
val createColumn2 = createNumberColumnJson("column2", Some(5))
val createColumn2 = createNumberColumnJson("column2", Some(10))
val expectedJson = Json.obj(
"status" -> "ok",
"columns" -> Json.arr(
Expand Down Expand Up @@ -272,6 +272,18 @@ class CreateColumnTest extends TableauxTestBase {
}
}

@Test
def createNumberColumnWithCustomDecimalDigitsTooHigh(implicit c: TestContext): Unit = {
exceptionTest("error.json.decimalDigits") {
val createColumn = createNumberColumnJson("column2", Some(11))

for {
_ <- sendRequest("POST", "/tables", createTableJson)
test <- sendRequest("POST", "/tables/1/columns", createColumn)
} yield ()
}
}

@Test
def createIntegerColumn(implicit c: TestContext): Unit = {
okTest {
Expand Down

0 comments on commit 1448441

Please sign in to comment.