Skip to content

Commit ea144fd

Browse files
committed
Add tests for vocabulary
1 parent 91b5df3 commit ea144fd

File tree

3 files changed

+449
-0
lines changed

3 files changed

+449
-0
lines changed

src/commonMain/kotlin/io/github/optimumcode/json/schema/internal/config/Draft202012SchemaLoaderConfig.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ internal object Draft202012SchemaLoaderConfig : SchemaLoaderConfig {
139139
val allEnabled = applicators && validations && unevaluated
140140
return when {
141141
allEnabled -> allFactories()
142+
applicators && validations -> applicatorFactories + validationFactories
142143
applicators -> applicatorFactories
143144
validations -> validationFactories
144145
else -> emptyList() // no vocabulary enabled
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package io.github.optimumcode.json.schema.base
2+
3+
import io.github.optimumcode.json.schema.ErrorCollector
4+
import io.github.optimumcode.json.schema.JsonSchemaLoader
5+
import io.github.optimumcode.json.schema.SchemaType.DRAFT_2019_09
6+
import io.kotest.assertions.assertSoftly
7+
import io.kotest.assertions.withClue
8+
import io.kotest.core.spec.style.FunSpec
9+
import io.kotest.matchers.shouldBe
10+
import kotlinx.serialization.json.JsonPrimitive
11+
import kotlinx.serialization.json.buildJsonArray
12+
import kotlinx.serialization.json.buildJsonObject
13+
14+
class JsonSchemaDraft201909Test : FunSpec() {
15+
init {
16+
JsonSchemaLoader.create()
17+
.register(
18+
"""
19+
{
20+
"${KEY}schema": "https://json-schema.org/draft/2019-09/schema",
21+
"${KEY}id": "https://localhost:8080/custom_meta",
22+
"${KEY}vocabulary": {
23+
"https://json-schema.org/draft/2019-09/vocab/core": true,
24+
"https://json-schema.org/draft/2019-09/vocab/applicator": false,
25+
"https://json-schema.org/draft/2019-09/vocab/validation": true
26+
}
27+
}
28+
""".trimIndent(),
29+
DRAFT_2019_09,
30+
).fromDefinition(
31+
"""
32+
{
33+
"${KEY}schema": "https://localhost:8080/custom_meta",
34+
"type": "object",
35+
"properties": {
36+
"test": {
37+
"type": "string"
38+
}
39+
}
40+
}
41+
""".trimIndent(),
42+
DRAFT_2019_09,
43+
).also { schema ->
44+
test("assertion inside applicator is not applied when it is disabled draft 2019-09") {
45+
schema.validate(
46+
buildJsonObject {
47+
put("test", JsonPrimitive(42))
48+
},
49+
ErrorCollector.EMPTY,
50+
) shouldBe true
51+
}
52+
53+
test("top level assertion is applied when applicator is disabled draft 2019-09") {
54+
schema.validate(
55+
buildJsonArray { },
56+
ErrorCollector.EMPTY,
57+
) shouldBe false
58+
}
59+
}
60+
61+
JsonSchemaLoader.create()
62+
.register(
63+
"""
64+
{
65+
"${KEY}schema": "https://json-schema.org/draft/2019-09/schema",
66+
"${KEY}id": "https://localhost:8080/custom_meta",
67+
"${KEY}vocabulary": {
68+
"https://json-schema.org/draft/2019-09/vocab/core": true,
69+
"https://json-schema.org/draft/2019-09/vocab/applicator": true,
70+
"https://json-schema.org/draft/2019-09/vocab/validation": false
71+
}
72+
}
73+
""".trimIndent(),
74+
DRAFT_2019_09,
75+
).fromDefinition(
76+
"""
77+
{
78+
"${KEY}schema": "https://localhost:8080/custom_meta",
79+
"type": "object",
80+
"properties": {
81+
"test": {
82+
"type": "string"
83+
}
84+
}
85+
}
86+
""".trimIndent(),
87+
DRAFT_2019_09,
88+
).also { schema ->
89+
test("top level assertion is not applied when validation is disabled draft 2019-09") {
90+
schema.validate(
91+
buildJsonArray { },
92+
ErrorCollector.EMPTY,
93+
) shouldBe true
94+
}
95+
96+
test("assertion in applicator is not applied when validation is disabled draft 2019-09") {
97+
schema.validate(
98+
buildJsonObject {
99+
put("test", JsonPrimitive(42))
100+
},
101+
ErrorCollector.EMPTY,
102+
) shouldBe true
103+
}
104+
}
105+
106+
JsonSchemaLoader.create()
107+
.register(
108+
"""
109+
{
110+
"${KEY}schema": "https://json-schema.org/draft/2019-09/schema",
111+
"${KEY}id": "https://localhost:8080/custom_meta",
112+
"${KEY}vocabulary": {
113+
"https://json-schema.org/draft/2019-09/vocab/core": true,
114+
"https://json-schema.org/draft/2019-09/vocab/applicator": false,
115+
"https://json-schema.org/draft/2019-09/vocab/validation": false
116+
}
117+
}
118+
""".trimIndent(),
119+
DRAFT_2019_09,
120+
).fromDefinition(
121+
"""
122+
{
123+
"${KEY}schema": "https://localhost:8080/custom_meta",
124+
"type": "object",
125+
"properties": {
126+
"test": {
127+
"type": "string"
128+
}
129+
}
130+
}
131+
""".trimIndent(),
132+
DRAFT_2019_09,
133+
).also { schema ->
134+
test("everything is valid when validation and applicator are disabled draft 2019-09") {
135+
assertSoftly {
136+
listOf(
137+
JsonPrimitive(42),
138+
JsonPrimitive("42a"),
139+
buildJsonArray { },
140+
buildJsonObject {
141+
put("test", JsonPrimitive(54))
142+
},
143+
).forEach {
144+
withClue(it) {
145+
schema.validate(
146+
it,
147+
ErrorCollector.EMPTY,
148+
) shouldBe true
149+
}
150+
}
151+
}
152+
}
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)