Skip to content

Commit

Permalink
Validate types when creating Key object (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
cedrickcooke authored Nov 12, 2021
1 parent b2103b7 commit 5b192dc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
14 changes: 12 additions & 2 deletions core/src/jsMain/kotlin/Key.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ package com.juul.indexeddb

import com.juul.indexeddb.external.IDBKeyRange
import kotlinext.js.jsObject
import kotlin.js.Date

private fun Array<dynamic>.validateKeyTypes() {
for (value in this) when (value) {
null, is String, is Date, is Double, is ByteArray, is IDBKeyRange -> continue
is Array<*> -> (value as Array<dynamic>).validateKeyTypes()
else -> error("Illegal key: expected string, date, float, binary blob, or array of those types, but got $value.")
}
}

public object AutoIncrement {
internal fun toJs(): dynamic = jsObject { autoIncrement = true }
Expand All @@ -11,7 +20,7 @@ public class KeyPath private constructor(
private val paths: Array<String>,
) {
init {
require(paths.isNotEmpty())
require(paths.isNotEmpty()) { "A key path must have at least one member." }
}

public constructor(path: String, vararg morePaths: String) : this(arrayOf(path, *morePaths))
Expand All @@ -24,7 +33,8 @@ public class Key private constructor(
private val values: Array<dynamic>,
) {
init {
require(values.isNotEmpty())
require(values.isNotEmpty()) { "A key must have at least one member." }
values.validateKeyTypes()
}

public constructor(value: dynamic, vararg moreValues: dynamic) : this(arrayOf(value, *moreValues))
Expand Down
60 changes: 60 additions & 0 deletions core/src/jsTest/kotlin/KeyTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.juul.indexeddb

import com.juul.indexeddb.external.IDBKeyRange
import kotlinext.js.jsObject
import kotlin.js.Date
import kotlin.test.Test
import kotlin.test.assertFails

public class KeyTests {

@Test
public fun constructor_withObjectType_shouldFail() {
assertFails { Key(jsObject()) }
}

@Test
public fun constructor_withArrayOfObjectType_shouldFail() {
assertFails { Key(arrayOf<dynamic>(jsObject())) }
}

@Test
public fun constructor_withLong_shouldFail() {
assertFails { Key(4L) }
}

@Test
public fun constructor_withString_completes() {
Key("string")
}

@Test
public fun constructor_withDate_completes() {
Key(Date("2021-11-11T12:00:00"))
}

@Test
public fun constructor_withNiceNumbers_completes() {
Key(1, 2f, 3.0)
}

@Test
public fun constructor_withByteArray_completes() {
Key(byteArrayOf(1, 2, 3, 4, 5, 6))
}

@Test
public fun constructor_withArrayOfString_completes() {
Key(arrayOf(arrayOf("foo"), "bar"))
}

@Test
public fun constructor_withRange_completes() {
Key(IDBKeyRange.upperBound("foobar", false))
}

@Test
public fun constructor_withNull_completes() {
Key(null)
}
}

0 comments on commit 5b192dc

Please sign in to comment.