From cf3cee8c51c480194cb8a3abdd439bb7498d9cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Wed, 11 Dec 2024 06:19:04 +0100 Subject: [PATCH] dynamic path support for prefixItems --- .../com/github/erosb/jsonsKema/PrefixItems.kt | 3 +- .../com/github/erosb/jsonsKema/Validator.kt | 30 ++++++++++--------- .../erosb/jsonsKema/ArrayValidationTest.kt | 10 +++++++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/com/github/erosb/jsonsKema/PrefixItems.kt b/src/main/kotlin/com/github/erosb/jsonsKema/PrefixItems.kt index 20648ff..f9ae03a 100644 --- a/src/main/kotlin/com/github/erosb/jsonsKema/PrefixItems.kt +++ b/src/main/kotlin/com/github/erosb/jsonsKema/PrefixItems.kt @@ -3,7 +3,8 @@ package com.github.erosb.jsonsKema data class PrefixItemsValidationFailure( val itemFailures: Map, override val schema: PrefixItemsSchema, - override val instance: IJsonArray<*> + override val instance: IJsonArray<*>, + val dynamicPath: JsonPointer ) : ValidationFailure( "array items ${itemFailures.keys.joinToString(", ")} failed to validate against \"prefixItems\" subschema", schema, diff --git a/src/main/kotlin/com/github/erosb/jsonsKema/Validator.kt b/src/main/kotlin/com/github/erosb/jsonsKema/Validator.kt index 75618d6..77651f5 100644 --- a/src/main/kotlin/com/github/erosb/jsonsKema/Validator.kt +++ b/src/main/kotlin/com/github/erosb/jsonsKema/Validator.kt @@ -554,22 +554,24 @@ private class DefaultValidator( } } - override fun visitPrefixItemsSchema(schema: PrefixItemsSchema): ValidationFailure? = instance.maybeArray { array -> - val failures = mutableMapOf() - for (index in 0 until Math.min(array.length(), schema.prefixSchemas.size)) { - val subschema = schema.prefixSchemas[index] - withOtherInstance(array.markEvaluated(index)) { - val failure = subschema.accept(this) - if (failure != null) { - failures[index] = failure - array.markUnevaluated(index) + override fun visitPrefixItemsSchema(schema: PrefixItemsSchema): ValidationFailure? = inPathSegment(Keyword.PREFIX_ITEMS) { + instance.maybeArray { array -> + val failures = mutableMapOf() + for (index in 0 until Math.min(array.length(), schema.prefixSchemas.size)) { + val subschema = schema.prefixSchemas[index] + withOtherInstance(array.markEvaluated(index)) { + val failure = subschema.accept(this) + if (failure != null) { + failures[index] = failure + array.markUnevaluated(index) + } } } - } - if (failures.isEmpty()) { - null - } else { - PrefixItemsValidationFailure(failures, schema, array) + if (failures.isEmpty()) { + null + } else { + PrefixItemsValidationFailure(failures, schema, array, dynamicPath()) + } } } diff --git a/src/test/kotlin/com/github/erosb/jsonsKema/ArrayValidationTest.kt b/src/test/kotlin/com/github/erosb/jsonsKema/ArrayValidationTest.kt index e2fbd0e..83837b3 100644 --- a/src/test/kotlin/com/github/erosb/jsonsKema/ArrayValidationTest.kt +++ b/src/test/kotlin/com/github/erosb/jsonsKema/ArrayValidationTest.kt @@ -150,4 +150,14 @@ class ArrayValidationTest { assertEquals(JsonPointer("maxItems"), (actual.causes.filter { it.keyword == Keyword.MAX_ITEMS }.first() as MaxItemsValidationFailure).dynamicPath) assertEquals(JsonPointer("minItems"), (actual.causes.filter { it.keyword == Keyword.MIN_ITEMS }.first() as MinItemsValidationFailure).dynamicPath) } + + @Test + fun `prefixItems dynamicPath`() { + val schema = SchemaLoader(""" + { "prefixItems": [{"type": "string"}]} + """.trimIndent())() + + val actual = Validator.forSchema(schema).validate("[2]") as PrefixItemsValidationFailure + assertEquals("#/prefixItems", actual.dynamicPath.toString()) + } }