1
+ package smirnov.oleg.json.schema.assertions.condition
2
+
3
+ import io.kotest.assertions.asClue
4
+ import io.kotest.core.spec.style.FunSpec
5
+ import io.kotest.matchers.collections.shouldContainExactly
6
+ import io.kotest.matchers.collections.shouldHaveSize
7
+ import io.kotest.matchers.shouldBe
8
+ import kotlinx.serialization.json.JsonPrimitive
9
+ import kotlinx.serialization.json.buildJsonObject
10
+ import smirnov.oleg.json.pointer.JsonPointer
11
+ import smirnov.oleg.json.schema.JsonSchema
12
+ import smirnov.oleg.json.schema.KEY
13
+ import smirnov.oleg.json.schema.ValidationError
14
+
15
+ @Suppress(" unused" )
16
+ class JsonSchemaIfThenElseValidationTest : FunSpec () {
17
+ init {
18
+ JsonSchema .fromDescription(
19
+ """
20
+ {
21
+ "${KEY } schema": "http://json-schema.org/draft-07/schema#",
22
+ "if": {
23
+ "type": "object"
24
+ },
25
+ "then": {
26
+ "required": ["f1","f2"]
27
+ },
28
+ "else": {
29
+ "type": "string"
30
+ }
31
+ }
32
+ """ .trimIndent()
33
+ ).also { schema ->
34
+ test(" when matches `if` passes `then` validation" ) {
35
+ val jsonObject = buildJsonObject {
36
+ put(" f1" , JsonPrimitive (42 ))
37
+ put(" f2" , JsonPrimitive (43 ))
38
+ }
39
+
40
+ val errors = mutableListOf<ValidationError >()
41
+ val valid = schema.validate(jsonObject, errors::add)
42
+
43
+ jsonObject.asClue {
44
+ valid shouldBe true
45
+ errors shouldHaveSize 0
46
+ }
47
+ }
48
+
49
+ test(" when matches `if` fails `then` validation" ) {
50
+ val jsonObject = buildJsonObject {
51
+ put(" f1" , JsonPrimitive (42 ))
52
+ put(" f3" , JsonPrimitive (43 ))
53
+ }
54
+
55
+ val errors = mutableListOf<ValidationError >()
56
+ val valid = schema.validate(jsonObject, errors::add)
57
+
58
+ jsonObject.asClue {
59
+ valid shouldBe false
60
+ errors.shouldContainExactly(
61
+ ValidationError (
62
+ schemaPath = JsonPointer (" /then/required" ),
63
+ objectPath = JsonPointer .ROOT ,
64
+ message = " missing required properties: [f2]" ,
65
+ )
66
+ )
67
+ }
68
+ }
69
+
70
+ test(" when does not matches `if` passes `else` validation" ) {
71
+ val element = JsonPrimitive (" test" )
72
+
73
+ val errors = mutableListOf<ValidationError >()
74
+ val valid = schema.validate(element, errors::add)
75
+
76
+ element.asClue {
77
+ valid shouldBe true
78
+ errors shouldHaveSize 0
79
+ }
80
+ }
81
+
82
+ test(" when does not matches `if` fails `else` validation" ) {
83
+ val element = JsonPrimitive (42 )
84
+
85
+ val errors = mutableListOf<ValidationError >()
86
+ val valid = schema.validate(element, errors::add)
87
+
88
+ element.asClue {
89
+ valid shouldBe false
90
+ errors.shouldContainExactly(
91
+ ValidationError (
92
+ schemaPath = JsonPointer (" /else/type" ),
93
+ objectPath = JsonPointer .ROOT ,
94
+ message = " element is not a string" ,
95
+ )
96
+ )
97
+ }
98
+ }
99
+ }
100
+
101
+ JsonSchema .fromDescription(
102
+ """
103
+ {
104
+ "${KEY } schema": "http://json-schema.org/draft-07/schema#",
105
+ "if": {
106
+ "type": "object"
107
+ },
108
+ "then": {
109
+ "required": ["f1","f2"]
110
+ }
111
+ }
112
+ """ .trimIndent()
113
+ ).also { schema ->
114
+ test(" when does not matches `if` and `else` is missing nothing is checked" ) {
115
+ val element = JsonPrimitive (" test" )
116
+
117
+ val errors = mutableListOf<ValidationError >()
118
+ val valid = schema.validate(element, errors::add)
119
+
120
+ element.asClue {
121
+ valid shouldBe true
122
+ errors shouldHaveSize 0
123
+ }
124
+ }
125
+ }
126
+
127
+ JsonSchema .fromDescription(
128
+ """
129
+ {
130
+ "${KEY } schema": "http://json-schema.org/draft-07/schema#",
131
+ "if": {
132
+ "type": "object"
133
+ },
134
+ "else": {
135
+ "type": "string"
136
+ }
137
+ }
138
+ """ .trimIndent()
139
+ ).also { schema ->
140
+ test(" when matches `if` and `then` is missing nothing is checked" ) {
141
+ val element = buildJsonObject {
142
+ put(" f1" , JsonPrimitive (42 ))
143
+ }
144
+
145
+ val errors = mutableListOf<ValidationError >()
146
+ val valid = schema.validate(element, errors::add)
147
+
148
+ element.asClue {
149
+ valid shouldBe true
150
+ errors shouldHaveSize 0
151
+ }
152
+ }
153
+ }
154
+ }
155
+ }
0 commit comments