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()) + } }