From 7b1b0ba2024d8942f213bae00e7fe579dd9e9e34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Mon, 16 Sep 2024 18:53:34 +0200 Subject: [PATCH] fixes problem with joining validation errors with different nesting levels --- .../erosb/jsonsKema/ValidationFailure.kt | 2 +- .../erosb/jsonsKema/Issue109ReproTest.kt | 57 ++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/github/erosb/jsonsKema/ValidationFailure.kt b/src/main/kotlin/com/github/erosb/jsonsKema/ValidationFailure.kt index 07c2122..202d7dd 100644 --- a/src/main/kotlin/com/github/erosb/jsonsKema/ValidationFailure.kt +++ b/src/main/kotlin/com/github/erosb/jsonsKema/ValidationFailure.kt @@ -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 diff --git a/src/test/kotlin/com/github/erosb/jsonsKema/Issue109ReproTest.kt b/src/test/kotlin/com/github/erosb/jsonsKema/Issue109ReproTest.kt index fdeda70..ba46912 100644 --- a/src/test/kotlin/com/github/erosb/jsonsKema/Issue109ReproTest.kt +++ b/src/test/kotlin/com/github/erosb/jsonsKema/Issue109ReproTest.kt @@ -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 { @@ -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" }!! } }