From 5b192dc0b6310da4492759e06a3ba2abff5dadb2 Mon Sep 17 00:00:00 2001 From: Cedrick Cooke Date: Fri, 12 Nov 2021 10:44:28 -0800 Subject: [PATCH] Validate types when creating `Key` object (#3) --- core/src/jsMain/kotlin/Key.kt | 14 ++++++- core/src/jsTest/kotlin/KeyTests.kt | 60 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 core/src/jsTest/kotlin/KeyTests.kt diff --git a/core/src/jsMain/kotlin/Key.kt b/core/src/jsMain/kotlin/Key.kt index 6aa9415..c923105 100644 --- a/core/src/jsMain/kotlin/Key.kt +++ b/core/src/jsMain/kotlin/Key.kt @@ -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.validateKeyTypes() { + for (value in this) when (value) { + null, is String, is Date, is Double, is ByteArray, is IDBKeyRange -> continue + is Array<*> -> (value as Array).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 } @@ -11,7 +20,7 @@ public class KeyPath private constructor( private val paths: Array, ) { 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)) @@ -24,7 +33,8 @@ public class Key private constructor( private val values: Array, ) { 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)) diff --git a/core/src/jsTest/kotlin/KeyTests.kt b/core/src/jsTest/kotlin/KeyTests.kt new file mode 100644 index 0000000..fd087b2 --- /dev/null +++ b/core/src/jsTest/kotlin/KeyTests.kt @@ -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(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) + } +}