Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support $schema property to identify schema draft to use #16

Merged
merged 3 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ val valid = schema.validate(elementToValidate, errors::add)

## Future plans

- [ ] Add `$schema` property validation (if not set the latest supported will be used)
- [x] Add `$schema` property validation (if not set the latest supported will be used)
- [ ] Add proper `$id` support (for nested schemas and for referencing)
- [ ] Add support for newer drafts
- [ ] [Draft 2019-09 (Draft 8)](https://json-schema.org/specification-links.html#draft-2019-09-formerly-known-as-draft-8)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ kotlin.js.compiler=ir
org.gradle.jvmargs=-Xmx1G
org.gradle.java.installations.auto-download=false

version=0.0.1-SNAPSHOT
version=0.0.2-SNAPSHOT
group=io.github.optimumcode
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ private val factories: List<AssertionFactory> = listOf(
private const val DEFINITIONS_PROPERTY: String = "definitions"
private const val ID_PROPERTY: String = "\$id"
private const val REF_PROPERTY: String = "\$ref"
private const val SCHEMA_PROPERTY: String = "\$schema"

internal const val ROOT_REFERENCE = '#'

internal class SchemaLoader {
fun load(schemaDefinition: JsonElement): JsonSchema {
extractSchemaType(schemaDefinition)
val baseId = extractBaseID(schemaDefinition)
val context = defaultLoadingContext(baseId)
loadDefinitions(schemaDefinition, context)
Expand All @@ -84,6 +86,17 @@ internal class SchemaLoader {
return JsonSchema(schemaAssertion, context.references)
}

private fun extractSchemaType(schemaDefinition: JsonElement): SchemaType {
return if (schemaDefinition is JsonObject) {
schemaDefinition[SCHEMA_PROPERTY]?.let {
require(it is JsonPrimitive && it.isString) { "$SCHEMA_PROPERTY must be a string" }
SchemaType.find(it.content) ?: throw IllegalArgumentException("unsupported schema type ${it.content}")
} ?: SchemaType.values().last()
} else {
SchemaType.values().last()
}
}

private fun loadDefinitions(schemaDefinition: JsonElement, context: DefaultLoadingContext) {
if (schemaDefinition !is JsonObject) {
return
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.optimumcode.json.schema.internal

import kotlin.jvm.JvmStatic

internal enum class SchemaType(
private val schemaId: String,
) {
DRAFT_7("json-schema.org/draft-07/schema"),
;

companion object {
@JvmStatic
fun find(schemaId: String): SchemaType? {
val id = schemaId.substringAfter("://")
.substringBeforeLast(ROOT_REFERENCE)
return values().find { it.schemaId == id }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,43 @@ class JsonSchemaTest : FunSpec() {
)
}
}

listOf(
"http://json-schema.org/draft-07/schema#",
"http://json-schema.org/draft-07/schema",
"https://json-schema.org/draft-07/schema#",
"https://json-schema.org/draft-07/schema",
).forEach {
test("loads schema with supported '$it' \$schema property") {
shouldNotThrowAny {
JsonSchema.fromDefinition(
"""
{
"${KEY}schema": "$it",
"type": "string"
}
""".trimIndent(),
)
}
}
}

listOf(
"https://json-schema.org/draft/2020-12/schema",
"http://json-schema.org/draft-07/schema/",
).forEach {
test("reports unsupported '$it' \$schema property") {
shouldThrow<IllegalArgumentException> {
JsonSchema.fromDefinition(
"""
{
"${KEY}schema": "$it",
"type": "string"
}
""".trimIndent(),
)
}.message shouldBe "unsupported schema type $it"
}
}
}
}