File tree Expand file tree Collapse file tree 6 files changed +77
-30
lines changed
commonMain/kotlin/smirnov/oleg/json/schema/internal/factories/condition
commonTest/kotlin/smirnov/oleg/json/schema/assertions/condition Expand file tree Collapse file tree 6 files changed +77
-30
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
package smirnov.oleg.json.schema.internal.factories.condition
2
2
3
- import kotlinx.serialization.json.JsonArray
4
3
import kotlinx.serialization.json.JsonElement
5
4
import smirnov.oleg.json.schema.ErrorCollector
6
5
import smirnov.oleg.json.schema.internal.AssertionContext
7
6
import smirnov.oleg.json.schema.internal.JsonSchemaAssertion
8
- import smirnov.oleg.json.schema.internal.LoadingContext
9
- import smirnov.oleg.json.schema.internal.factories.AbstractAssertionFactory
10
7
11
8
@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)
24
11
}
25
12
26
13
private class AllOfAssertion (
Original file line number Diff line number Diff line change 1
1
package smirnov.oleg.json.schema.internal.factories.condition
2
2
3
- import kotlinx.serialization.json.JsonArray
4
3
import kotlinx.serialization.json.JsonElement
5
4
import smirnov.oleg.json.schema.ErrorCollector
6
5
import smirnov.oleg.json.schema.ValidationError
7
6
import smirnov.oleg.json.schema.internal.AssertionContext
8
7
import smirnov.oleg.json.schema.internal.JsonSchemaAssertion
9
- import smirnov.oleg.json.schema.internal.LoadingContext
10
- import smirnov.oleg.json.schema.internal.factories.AbstractAssertionFactory
11
8
12
9
@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)
25
12
}
26
13
27
14
private class AnyOfAssertion (
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import smirnov.oleg.json.schema.ValidationError
14
14
@Suppress(" unused" )
15
15
class JsonSchemaAllOfValidationTest : FunSpec () {
16
16
init {
17
+ testInvalidSchemaInArray(" allOf" )
17
18
JsonSchema .fromDescription(
18
19
"""
19
20
{
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ import smirnov.oleg.json.schema.ValidationError
16
16
@Suppress(" unused" )
17
17
class JsonSchemaAnyOfValidationTest : FunSpec () {
18
18
init {
19
+ testInvalidSchemaInArray(" anyOf" )
19
20
JsonSchema .fromDescription(
20
21
"""
21
22
{
You can’t perform that action at this time.
0 commit comments