Skip to content

Commit 7953849

Browse files
author
Oleg
committed
Extract common part from allOf and anyOf factories. Add negative tests
1 parent 512285a commit 7953849

File tree

6 files changed

+77
-30
lines changed

6 files changed

+77
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package smirnov.oleg.json.schema.internal.factories.condition
2+
3+
import kotlinx.serialization.json.JsonArray
4+
import kotlinx.serialization.json.JsonElement
5+
import smirnov.oleg.json.schema.internal.JsonSchemaAssertion
6+
import smirnov.oleg.json.schema.internal.LoadingContext
7+
import smirnov.oleg.json.schema.internal.factories.AbstractAssertionFactory
8+
9+
internal abstract class AbstractAssertionsCollectionFactory(property: String) : AbstractAssertionFactory(property) {
10+
final override fun createFromProperty(element: JsonElement, context: LoadingContext): JsonSchemaAssertion {
11+
require(element is JsonArray) { "$property must be an array" }
12+
require(element.isNotEmpty()) { "$property must have at least one element" }
13+
require(element.all(context::isJsonSchema)) { "each element in $property must be a valid JSON schema" }
14+
15+
val assertions: List<JsonSchemaAssertion> = element.mapIndexed { index, item ->
16+
context.at(index).schemaFrom(item)
17+
}
18+
19+
return createAssertion(assertions)
20+
}
21+
22+
protected abstract fun createAssertion(assertions: List<JsonSchemaAssertion>): JsonSchemaAssertion
23+
}

src/commonMain/kotlin/smirnov/oleg/json/schema/internal/factories/condition/AllOfAssertion.kt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
package smirnov.oleg.json.schema.internal.factories.condition
22

3-
import kotlinx.serialization.json.JsonArray
43
import kotlinx.serialization.json.JsonElement
54
import smirnov.oleg.json.schema.ErrorCollector
65
import smirnov.oleg.json.schema.internal.AssertionContext
76
import smirnov.oleg.json.schema.internal.JsonSchemaAssertion
8-
import smirnov.oleg.json.schema.internal.LoadingContext
9-
import smirnov.oleg.json.schema.internal.factories.AbstractAssertionFactory
107

118
@Suppress("unused")
12-
internal object AllOfAssertionFactory : AbstractAssertionFactory("allOf") {
13-
override fun createFromProperty(element: JsonElement, context: LoadingContext): JsonSchemaAssertion {
14-
require(element is JsonArray) { "$property must be an array" }
15-
require(element.isNotEmpty()) { "$property must have at least one element" }
16-
require(element.all(context::isJsonSchema)) { "each element in $property must be a valid JSON schema" }
17-
18-
val assertions: List<JsonSchemaAssertion> = element.mapIndexed { index, item ->
19-
context.at(index).schemaFrom(item)
20-
}
21-
22-
return AllOfAssertion(assertions)
23-
}
9+
internal object AllOfAssertionFactory : AbstractAssertionsCollectionFactory("allOf") {
10+
override fun createAssertion(assertions: List<JsonSchemaAssertion>): JsonSchemaAssertion = AllOfAssertion(assertions)
2411
}
2512

2613
private class AllOfAssertion(

src/commonMain/kotlin/smirnov/oleg/json/schema/internal/factories/condition/AnyOfAssertion.kt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
package smirnov.oleg.json.schema.internal.factories.condition
22

3-
import kotlinx.serialization.json.JsonArray
43
import kotlinx.serialization.json.JsonElement
54
import smirnov.oleg.json.schema.ErrorCollector
65
import smirnov.oleg.json.schema.ValidationError
76
import smirnov.oleg.json.schema.internal.AssertionContext
87
import smirnov.oleg.json.schema.internal.JsonSchemaAssertion
9-
import smirnov.oleg.json.schema.internal.LoadingContext
10-
import smirnov.oleg.json.schema.internal.factories.AbstractAssertionFactory
118

129
@Suppress("unused")
13-
internal object AnyOfAssertionFactory : AbstractAssertionFactory("anyOf") {
14-
override fun createFromProperty(element: JsonElement, context: LoadingContext): JsonSchemaAssertion {
15-
require(element is JsonArray) { "$property must be an array" }
16-
require(element.isNotEmpty()) { "$property must have at least one element" }
17-
require(element.all(context::isJsonSchema)) { "each element in $property must be a valid JSON schema" }
18-
19-
val assertions: List<JsonSchemaAssertion> = element.mapIndexed { index, item ->
20-
context.at(index).schemaFrom(item)
21-
}
22-
23-
return AnyOfAssertion(assertions)
24-
}
10+
internal object AnyOfAssertionFactory : AbstractAssertionsCollectionFactory("anyOf") {
11+
override fun createAssertion(assertions: List<JsonSchemaAssertion>): JsonSchemaAssertion = AnyOfAssertion(assertions)
2512
}
2613

2714
private class AnyOfAssertion(
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package smirnov.oleg.json.schema.assertions.condition
2+
3+
import io.kotest.assertions.throwables.shouldThrow
4+
import io.kotest.core.spec.style.FunSpec
5+
import io.kotest.matchers.shouldBe
6+
import smirnov.oleg.json.schema.JsonSchema
7+
import smirnov.oleg.json.schema.KEY
8+
9+
fun FunSpec.testInvalidSchemaInArray(name: String) {
10+
test("reports empty array") {
11+
shouldThrow<IllegalArgumentException> {
12+
JsonSchema.fromDescription(
13+
"""
14+
{
15+
"${KEY}schema": "http://json-schema.org/draft-07/schema#",
16+
"$name": []
17+
}
18+
""".trimIndent()
19+
)
20+
}.message shouldBe "$name must have at least one element"
21+
}
22+
23+
test("reports not array") {
24+
shouldThrow<IllegalArgumentException> {
25+
JsonSchema.fromDescription(
26+
"""
27+
{
28+
"${KEY}schema": "http://json-schema.org/draft-07/schema#",
29+
"$name": {}
30+
}
31+
""".trimIndent()
32+
)
33+
}.message shouldBe "$name must be an array"
34+
}
35+
36+
test("reports element in array is not a valid JSON schema") {
37+
shouldThrow<IllegalArgumentException> {
38+
JsonSchema.fromDescription(
39+
"""
40+
{
41+
"${KEY}schema": "http://json-schema.org/draft-07/schema#",
42+
"$name": [42]
43+
}
44+
""".trimIndent()
45+
)
46+
}.message shouldBe "each element in $name must be a valid JSON schema"
47+
}
48+
}

src/commonTest/kotlin/smirnov/oleg/json/schema/assertions/condition/JsonSchemaAllOfValidationTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import smirnov.oleg.json.schema.ValidationError
1414
@Suppress("unused")
1515
class JsonSchemaAllOfValidationTest : FunSpec() {
1616
init {
17+
testInvalidSchemaInArray("allOf")
1718
JsonSchema.fromDescription(
1819
"""
1920
{

src/commonTest/kotlin/smirnov/oleg/json/schema/assertions/condition/JsonSchemaAnyOfValidationTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import smirnov.oleg.json.schema.ValidationError
1616
@Suppress("unused")
1717
class JsonSchemaAnyOfValidationTest : FunSpec() {
1818
init {
19+
testInvalidSchemaInArray("anyOf")
1920
JsonSchema.fromDescription(
2021
"""
2122
{

0 commit comments

Comments
 (0)