Skip to content

Commit

Permalink
dynamic path support for prefixItems
Browse files Browse the repository at this point in the history
  • Loading branch information
erosb committed Dec 11, 2024
1 parent 1c2d420 commit cf3cee8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/com/github/erosb/jsonsKema/PrefixItems.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.github.erosb.jsonsKema
data class PrefixItemsValidationFailure(
val itemFailures: Map<Int, ValidationFailure>,
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,
Expand Down
30 changes: 16 additions & 14 deletions src/main/kotlin/com/github/erosb/jsonsKema/Validator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -554,22 +554,24 @@ private class DefaultValidator(
}
}

override fun visitPrefixItemsSchema(schema: PrefixItemsSchema): ValidationFailure? = instance.maybeArray { array ->
val failures = mutableMapOf<Int, ValidationFailure>()
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<Int, ValidationFailure>()
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())
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/test/kotlin/com/github/erosb/jsonsKema/ArrayValidationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

0 comments on commit cf3cee8

Please sign in to comment.