Skip to content

Commit ff9667f

Browse files
authored
Merge pull request #1314 from OptimumCode/kmp-validator-new-drafts
Update version: support for draft6 and draft4
2 parents 85b657d + 2aa1716 commit ff9667f

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[versions]
22
kotlin = "2.0.0"
3-
json-schema-validator = "0.2.0"
3+
json-schema-validator = "0.2.1"
44

55
[plugins]
66
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }

src/main/kotlin/BowtieSampsonSchemaValidatorLauncher.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import com.eygraber.uri.Uri
12
import io.github.optimumcode.json.schema.ErrorCollector
23
import io.github.optimumcode.json.schema.JsonSchema
34
import io.github.optimumcode.json.schema.JsonSchemaLoader
45
import io.github.optimumcode.json.schema.SchemaType
6+
import kotlinx.serialization.ExperimentalSerializationApi
57
import kotlinx.serialization.encodeToString
68
import kotlinx.serialization.json.ClassDiscriminatorMode
79
import kotlinx.serialization.json.Json
@@ -14,6 +16,7 @@ import java.io.InputStreamReader
1416
import java.util.jar.Attributes
1517
import java.util.jar.Manifest
1618

19+
@OptIn(ExperimentalSerializationApi::class)
1720
fun main() {
1821
val input = BufferedReader(InputStreamReader(System.`in`))
1922
val outputWriter = System.out.bufferedWriter()
@@ -35,6 +38,8 @@ fun main() {
3538
}
3639

3740
private val SUPPORTED_DIALECTS: Set<String> = hashSetOf(
41+
"http://json-schema.org/draft-04/schema#",
42+
"http://json-schema.org/draft-06/schema#",
3843
"http://json-schema.org/draft-07/schema#",
3944
"https://json-schema.org/draft/2019-09/schema",
4045
"https://json-schema.org/draft/2020-12/schema",
@@ -122,30 +127,30 @@ class BowtieSampsonSchemaValidatorLauncher(
122127
.apply {
123128
currentDialect?.also(this::registerWellKnown)
124129
for ((uri, schema) in command.case.registry) {
125-
if (skipSchema(uri, schema)) {
130+
if (skipSchema(schema)) {
126131
continue
127132
}
133+
var schemaType: SchemaType? = null
134+
if (uri.contains("draft4", ignoreCase = true)) {
135+
// remote schema for draft 4 does not contain $schema block
136+
schemaType = SchemaType.DRAFT_4
137+
}
128138
@Suppress("detekt:TooGenericExceptionCaught")
129139
try {
130-
register(schema, uri)
140+
register(schema, Uri.parse(uri), schemaType)
131141
} catch (ex: Exception) {
132142
throw IllegalStateException("cannot register schema for URI '$uri'", ex)
133143
}
134144
}
135145
}.fromJsonElement(schemaDefinition, currentDialect)
136146

137-
private fun skipSchema(uri: String, schema: JsonElement): Boolean {
138-
if (uri.contains("draft4", ignoreCase = true)) {
139-
// skip draft4 schemas
140-
return true
141-
}
147+
private fun skipSchema(schema: JsonElement): Boolean =
142148
// ignore schemas for unsupported drafts
143-
return schema is JsonObject &&
149+
schema is JsonObject &&
144150
schema["\$schema"]
145151
?.jsonPrimitive
146152
?.content
147153
.let { it != null && SchemaType.find(it) == null }
148-
}
149154

150155
private fun runCase(command: Command.Run, schema: JsonSchema) {
151156
val results: List<TestResult> = command.case.tests.map { test ->

src/main/kotlin/TestFilters.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ interface TestFilter {
1414

1515
fun getFilter(schemaType: SchemaType?): TestFilter =
1616
when (schemaType ?: SchemaType.entries.last()) {
17+
SchemaType.DRAFT_4 -> TestFilterDraft4
18+
SchemaType.DRAFT_6 -> TestFilterDraft6
1719
SchemaType.DRAFT_7 -> TestFilterDraft7
1820
SchemaType.DRAFT_2019_09 -> TestFilterDraft201909
1921
SchemaType.DRAFT_2020_12 -> TestFilterDraft202012
2022
}
2123

24+
object TestFilterDraft4 : TestFilter
25+
26+
object TestFilterDraft6 : TestFilter
27+
2228
object TestFilterDraft7 : TestFilter
2329

2430
object TestFilterDraft201909 : TestFilter {

0 commit comments

Comments
 (0)