Repository navigation
refactor(validation): compute a schema's effective resource root and dynamic scope in one place - #103
Conversation
…dynamic scope in one place validateElement, collectEvaluatedIndices and collectEvaluatedProperties each spelled out "a schema with $id becomes the resource root and enters the dynamic scope". Guideline 8 requires the verdict pass and the unevaluated* collectors to agree on this exactly; #90 was a bug from them disagreeing. All three now use resourceRootAt() and scopeAt(), which share one startsResource() test (an object with $id, whatever the value's type), and the $id registration in validateElement uses the same test. The copies were equivalent: validateElement checked `is JsonObject` inline, the collectors had already returned for non-objects. The helpers return their inputs unchanged when there is no $id, so nothing new is allocated. Removes two orphaned KDoc comments that documented nothing: "Checks if a property name matches any pattern" (above an already documented matchesAnyPattern) and "Validates the structure of a schema itself" (above the companion object). Behaviour is unchanged. Closes #98 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Review of #103The refactor does what it says. All three call sites compute exactly what they computed before, the hot path allocates nothing new, and both deleted comments documented nothing. I found nothing blocking. There is one should-fix about test coverage: with the PR applied, two of the four obvious ways the passes could drift apart again still pass the whole suite. A cheap test that catches both is below. The rest are a pre-existing semantics question that the new KDoc now states as intended, and nits. BlockingNone. What I checked:
Should-fix1. No test catches a collector that stops entering an inline resource into its dynamic scope. ( I mutated each call site on the PR head and ran the full suite each time:
So the single definition is only half protected: the root half is tested, the scope half is not. An inline resource's place in the dynamic scope matters when a // An inline subschema with $id joins the collector's dynamic scope, as it does the verdict pass's (#98)
describe("an inline resource is in the collectors' dynamic scope") {
val d = '$'
fun schema(keyword: String, outer: String, inner: String) = """
{"${d}id":"https://ex.com/root",
"allOf":[{"${d}id":"outer","${d}ref":"inner","${d}defs":{"x":{"${d}dynamicAnchor":"ext",$outer}}}],
"$keyword":false,
"${d}defs":{"inner":{"${d}id":"inner","${d}dynamicRef":"#ext","${d}defs":{"y":{"${d}dynamicAnchor":"ext",$inner}}}}}
"""
fun isValid(schema: String, instance: String) =
JsonValidator().validate(Json.parseToJsonElement(instance), JsonSchema.fromString(schema, SchemaVersion.DRAFT_2020_12)).isValid
it("unevaluatedProperties") {
val s = schema("unevaluatedProperties", """"properties":{"a":true}""", """"properties":{"b":true}""")
isValid(s, """{"a":1}""") shouldBe true // #ext -> outer's anchor, the outermost in scope
isValid(s, """{"b":1}""") shouldBe false
}
it("unevaluatedItems") {
val s = schema("unevaluatedItems", """"prefixItems":[true]""", """"prefixItems":[true,true]""")
isValid(s, "[1]") shouldBe true
isValid(s, "[1,2]") shouldBe false
}
}If you add it, update the test counts in the README, CLAUDE.md and 2. (Pre-existing, now written into the KDoc.) CLAUDE.md:386 says {"$id":"https://ex.com/root","$defs":{"a":{"type":"integer"}},
"properties":{"x":{"$id":5,"$ref":"#/$defs/a"}}}
Changing it alters verdicts, so per the commit rules it should be its own
All the other Nits
🤖 Generated with Claude Code |
…c scope Reverting either collector's scope (scopeAt -> plain dynamicScope) passed the whole suite. A 2020-12 test now sends a $dynamicRef through an inline $id resource that the scope must contain, for unevaluatedProperties and unevaluatedItems; each reverted collector fails it. Test counts updated. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The KDoc no longer presents a non-string $id starting a resource as intended: it matches the three copies it replaced, unlike the string-only $id handling elsewhere (#57). Notes that `is JsonObject` before startsResource in validateElement is only for the smart cast. No behaviour change. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Summary
validateElement,collectEvaluatedIndicesandcollectEvaluatedPropertieseach computed the effective resource root and dynamic scope for a schema with$id. They now call two private helpers,resourceRootAt()andscopeAt(), which share onestartsResource()test, so the verdict and collection passes cannot drift apart again (guideline 8, [P2] unevaluated* resolves refs in branches of an inline $id subschema against the parent resource #90). The$idregistration invalidateElementuses the same test and stays where it was.validateElementcheckedis JsonObjectinline, and the collectors had already returned for non-objects.scopeAt()still grows the scope throughentering()([P1] Dynamic scope omits root resources that have a retrieval base but no explicit $id #64). When there is no$id, the helpers return their inputs unchanged, so nothing new is allocated.Behaviour is unchanged.
Verification
./gradlew ktlintFormatCI=true ./gradlew test ktlintCheck --rerun-tasks: 5,850 tests, 0 failures, 0 errors, 0 skipped (frombuild/test-results/test/*.xml), including the official suitepython3 .claude/skills/fix-issue/scripts/test_counts.py: exit 0 (full run 5,850, custom 1,380)Closes #98
🤖 Generated with Claude Code