Skip to content

Commit

Permalink
Move references validation to the latter stage (#45)
Browse files Browse the repository at this point in the history
There is no guarantee that all schemes will be registered in the order
of usage (schema with dependency after those dependencies). Because of
that I have moved the references validation to the latter stage when the
actual schema that will be used for validation is created
  • Loading branch information
OptimumCode authored Feb 1, 2024
1 parent 17644f9 commit 8f277d1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private const val SCHEMA_PROPERTY: String = "\$schema"

internal class SchemaLoader : JsonSchemaLoader {
private val references: MutableMap<RefId, AssertionWithPath> = linkedMapOf()
private val usedRefs: MutableSet<RefId> = linkedSetOf()
private val usedRefs: MutableSet<ReferenceLocation> = linkedSetOf()

override fun register(
schema: JsonElement,
Expand Down Expand Up @@ -67,11 +67,12 @@ internal class SchemaLoader : JsonSchemaLoader {
draft: SchemaType?,
): JsonSchema {
val assertion: JsonSchemaAssertion = loadSchemaData(schemaElement, draft, references, usedRefs)
validateReferences(references, usedRefs)
return createSchema(
LoadResult(
assertion,
references.toMutableMap(),
usedRefs.toMutableSet(),
usedRefs.mapTo(hashSetOf()) { it.refId },
),
)
}
Expand Down Expand Up @@ -107,17 +108,18 @@ internal object IsolatedLoader : JsonSchemaLoader {
draft: SchemaType?,
): JsonSchema {
val references: MutableMap<RefId, AssertionWithPath> = linkedMapOf()
val usedRefs: MutableSet<RefId> = hashSetOf()
val usedRefs: MutableSet<ReferenceLocation> = hashSetOf()
val assertion: JsonSchemaAssertion = loadSchemaData(schemaElement, draft, references, usedRefs)
return createSchema(LoadResult(assertion, references, usedRefs))
validateReferences(references, usedRefs)
return createSchema(LoadResult(assertion, references, usedRefs.mapTo(hashSetOf()) { it.refId }))
}
}

private fun loadSchemaData(
schemaDefinition: JsonElement,
defaultType: SchemaType?,
references: MutableMap<RefId, AssertionWithPath>,
usedRefs: MutableSet<RefId>,
usedRefs: MutableSet<ReferenceLocation>,
externalUri: Uri? = null,
): JsonSchemaAssertion {
val schemaType = extractSchemaType(schemaDefinition, defaultType)
Expand All @@ -138,12 +140,18 @@ private fun loadSchemaData(
}
val schemaAssertion = loadSchema(schemaDefinition, context)
references.putAll(isolatedReferences)
context.usedRef.mapTo(usedRefs) { it.refId }
usedRefs.addAll(context.usedRef)
return schemaAssertion
}

private fun validateReferences(
references: Map<RefId, AssertionWithPath>,
usedRefs: Set<ReferenceLocation>,
) {
ReferenceValidator.validateReferences(
references.mapValues { it.value.run { PointerWithBaseId(this.baseId, schemaPath) } },
context.usedRef,
usedRefs,
)
return schemaAssertion
}

private fun createSchema(result: LoadResult): JsonSchema {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import io.github.optimumcode.json.schema.ErrorCollector
import io.github.optimumcode.json.schema.JsonSchemaLoader
import io.github.optimumcode.json.schema.ValidationError
import io.kotest.assertions.assertSoftly
import io.kotest.assertions.throwables.shouldNotThrowAnyUnit
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.assertions.withClue
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldContainExactly
Expand Down Expand Up @@ -161,5 +163,53 @@ class JsonSchemaLoaderTest : FunSpec() {
}
}
}

test("does not report missing refs when schema is registered") {
shouldNotThrowAnyUnit {
JsonSchemaLoader.create()
.register(
"""
{
"id": "https://test.com/a",
"properties": {
"anotherName": {
"${KEY}ref": "https://test.com/b#/properties/name"
}
}
}
""".trimIndent(),
)
}
}

test("reports missing refs when schema is created from definition") {
shouldThrow<IllegalArgumentException> {
JsonSchemaLoader.create()
.register(
"""
{
"${KEY}id": "https://test.com/a",
"properties": {
"anotherName": {
"${KEY}ref": "https://test.com/b#/properties/name"
}
}
}
""".trimIndent(),
).fromDefinition(
"""
{
"${KEY}id": "https://test.com/c",
"properties": {
"subName": {
"${KEY}ref": "https://test.com/a#/properties/anotherName"
}
}
}
""".trimIndent(),
)
}.message shouldBe "cannot resolve references: " +
"{\"https://test.com/b#/properties/name\": [\"/properties/anotherName\"]}"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private fun FunSpec.executeFromDirectory(
JsonSchemaLoader.create()
.apply {
SchemaType.entries.forEach(::registerWellKnown)
for ((uri, schema) in remoteSchemas.entries.reversed()) {
for ((uri, schema) in remoteSchemas) {
if (uri.contains("draft4", ignoreCase = true)) {
// skip draft4 schemas
continue
Expand Down

0 comments on commit 8f277d1

Please sign in to comment.