Skip to content

Report a nested Array scope that got no Array under its own name only - #3099

Merged
ericproulx merged 1 commit into
masterfrom
fix/scalar-for-nested-array-scope
Nov 22, 2026
Merged

ericproulx merged 1 commit into
masterfrom
fix/scalar-for-nested-array-scope

Conversation

@ericproulx

Copy link
Copy Markdown
Contributor

Summary

Fixes #1998.

An Array scope inside an Array scope is read from each element of the outer Array. When an element left that param out, or held something other than an Array there, AttributesIterator yielded the value as an element of the inner Array. The inner scope's validators then reported a second error, named with the outer element's index for the inner Array and a stale index for the outer one:

params do
  requires :points, type: Array do
    requires :location, type: Array do
      requires :id, type: Integer
    end
  end
end
body before after
{"points": [{"location": [{"id": 1}]}, {"location": [{"id": 1}, {"id": 2}]}, {"location": 0}]} points[2][location] is invalid, points[1][location][2][id] is missing, points[1][location][2][id] is invalid points[2][location] is invalid
{"points": [{"location": [{"id": 1}, {"id": 2}]}, {}]} points[1][location] is missing, points[0][location][1][id] is missing points[1][location] is missing

The iterator now passes over such a value, as #each already does at the top for an Array scope that did not get an Array. The scope's own validators still report it, so these requests answer 400 as before, with the errors the same scope gives at the root (subitems is missing, subitems is invalid).

The reports in #1998 for well-formed input already had the right indices on master. This was the case left over, from the issue's { location: 0 } element.

Specs

Five specs in validations_spec.rb had pinned the extra error, for example children[1][parents] is missing, children[0][parents][1][name] is missing, children[0][parents][1][name] is empty for a parents left out of children[1]. They now expect the scope's own error alone. Two new specs cover the issue's shape and fail without the change.

Test plan

  • bundle exec rake passes locally (RuboCop clean, 3342 examples, 0 failures).
  • CI green.

🤖 Generated with Claude Code

An Array scope inside an Array scope is read from each element of the
outer Array. When an element left that param out, or held something
other than an Array there, such as `0`, AttributesIterator yielded the
value as an element of the inner Array. The inner scope's validators
then reported a second error, named with the outer element's index for
the inner Array and a stale index for the outer one:

    requires :points, type: Array do
      requires :location, type: Array do
        requires :id, type: Integer
      end
    end

    {"points": [{"location": [{"id": 1}]},
                {"location": [{"id": 1}, {"id": 2}]},
                {"location": 0}]}

    Before: points[2][location] is invalid,
            points[1][location][2][id] is missing,
            points[1][location][2][id] is invalid
    After:  points[2][location] is invalid

The iterator now passes over such a value, as #each already does at
the top for an Array scope that did not get an Array. The scope's own
validators still report it, so these requests answer 400 as before,
with the errors the same scope gives at the root. Five specs had pinned
the extra error and are updated.

Fixes #1998.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ericproulx
ericproulx force-pushed the fix/scalar-for-nested-array-scope branch from e14fc46 to 1a3685f Compare November 21, 2026 18:05
@ericproulx
ericproulx merged commit 2952721 into master Nov 22, 2026
52 checks passed
@ericproulx
ericproulx deleted the fix/scalar-for-nested-array-scope branch November 22, 2026 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array[Array[X]]] and Array[Hash[X]]] return wrong indices in errors

1 participant