Skip to content

Conversation

@seanpdoyle
Copy link
Contributor

@seanpdoyle seanpdoyle commented Oct 22, 2025

This commit changes the ActiveResource::Errors#from_array method to be implemented in terms of its complementary #from_hash method.

Rather than modifying the Errors instance itself (like managing attribute and :base assignment, clearing, etc), #from_array will instead transform the Array instance into a Hash keyed by known attributes. Once the Hash is constructed, the method hands it off to the #from_hash method.

The goal of this change is to reduce the duplicated burden of responsibility.

Along with the implementation change, this commit also includes explicit coverage of the Errors#from_xml and Errors#from_json methods. They're part of the gem's public API (since they're documented and not marked # :nodoc:), but aren't explicitly tested elsewhere in the suite.

There are two XML-focused tests. The first covers the case where the payload has multiple <error> elements:

Hash.from_xml "<errors><error>Name can't be blank</error><error>Email can't be blank</error></errors>"
 # => {"errors"=>{"error"=>["Name can't be blank", "Email can't be blank"]}}

The call to Hash.from_xml transforms each <error> element into an item in the Array.

The second test covers the case where the payload has a single payload has a single <error> element:

Hash.from_xml "<errors><error>Name can't be blank</error></errors>"
 # => {"errors"=>{"error"=>"Name can't be blank"}}

In this case, The call to Hash.from_xml transforms the <error> element into a single String value.

Since the Errors#from_xml method is part of the public API, and since its implementation invokes Errors#from_array, dedicating explicit test coverage for both scenarios can help prevent regressions or breaks in backwards-compatibility.

@seanpdoyle seanpdoyle force-pushed the validation-errors-from-array branch 2 times, most recently from 18751c8 to 617a762 Compare October 22, 2025 17:10
This commit changes the `ActiveResource::Errors#from_array`
method to be implemented in terms of its complementary `#from_hash`
method.

Rather than modifying the `Errors` instance itself (like managing
attribute and `:base` assignment, clearing, etc), `#from_array` will
instead transform the Array instance into a Hash keyed by known
attributes. Once the Hash is constructed, the method hands it off to the
`#from_hash` method.

The goal of this change is to reduce the duplicated burden of
responsibility.

Along with the implementation change, this commit also includes explicit
coverage of the `Errors#from_xml` and `Errors#from_json` methods.
They're part of the gem's public API (since they're documented and not
marked `# :nodoc:`), but aren't explicitly tested elsewhere in the
suite.

There are two XML-focused tests. The first covers the case where the
payload has multiple `<error>` elements:

```ruby
Hash.from_xml "<errors><error>Name can't be blank</error><error>Email can't be blank</error></errors>"
 # => {"errors"=>{"error"=>["Name can't be blank", "Email can't be blank"]}}
```

The call to `Hash.from_xml` transforms each `<error>` element into an
item in the Array.

The second test covers the case where the payload has a single
payload has a single `<error>` element:

```ruby
Hash.from_xml "<errors><error>Name can't be blank</error></errors>"
 # => {"errors"=>{"error"=>"Name can't be blank"}}
```

In this case, The call to `Hash.from_xml` transforms the `<error>`
element into a single String value.

Since the `Errors#from_xml` method is part of the public API, and since
its implementation invokes `Errors#from_array`, dedicating explicit test
coverage for both scenarios can help prevent regressions or
breaks in backwards-compatibility.
@seanpdoyle seanpdoyle force-pushed the validation-errors-from-array branch from 617a762 to 954cecc Compare October 22, 2025 22:27
@rafaelfranca rafaelfranca merged commit b86cf0c into rails:main Oct 22, 2025
19 checks passed
@seanpdoyle seanpdoyle deleted the validation-errors-from-array branch October 22, 2025 23:12
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.

2 participants