Skip to content

Commit

Permalink
fixes problem with joining validation errors with different nesting l…
Browse files Browse the repository at this point in the history
…evels
  • Loading branch information
erosb committed Sep 16, 2024
1 parent 3499cf1 commit 7b1b0ba
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal class AggregatingValidationFailure(

override fun join(parent: Schema, instance: IJsonValue, other: ValidationFailure): ValidationFailure {
if (instance != this.instance) {
TODO("something went wrong: $instance vs ${this.instance}")
return AggregatingValidationFailure(parent, instance, _causes + other)
}
_causes.add(other)
return this
Expand Down
57 changes: 55 additions & 2 deletions src/test/kotlin/com/github/erosb/jsonsKema/Issue109ReproTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.erosb.jsonsKema

import com.github.erosb.jsonsKema.Validator.Companion.forSchema
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class Issue109ReproTest {
Expand Down Expand Up @@ -75,7 +76,59 @@ class Issue109ReproTest {
""".trimIndent()
).parse()

val failure = validator.validate(instance)
println(failure)
val failure = validator.validate(instance)!!

assertThat(failure.causes).hasSize(3)
}

@Test
fun simpler() {
val schemaJson = JsonParser(
"""
{
"${'$'}schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"aaa": {
"properties": {
"aaa1": {
"const": 1
},
"aaa2": {
"const": 1
}
}
},
"bbb": {
"const": 1
}
}
}
""".trimIndent()
).parse()
val schema = SchemaLoader(schemaJson).load()
val validator = forSchema(schema)
val instance = JsonParser(
"""
{
"aaa": {
"aaa1": 0,
"aaa2": 0
},
"bbb": 0
}
""".trimIndent()
).parse()

val failure = validator.validate(instance)!!

println(failure.toJSON())

assertThat(failure.causes).hasSize(3)
failure.causes.find { it.instance.location.pointer.toString() == "#/aaa/aaa1" }!!
failure.causes.find { it.instance.location.pointer.toString() == "#/aaa/aaa2" }!!
failure.causes.find { it.instance.location.pointer.toString() == "#/bbb" }!!
}
}

0 comments on commit 7b1b0ba

Please sign in to comment.