chore: fix error serialization for string errors#2
Merged
martingregoire merged 2 commits intoeasypep-masterfrom Aug 28, 2025
Merged
chore: fix error serialization for string errors#2martingregoire merged 2 commits intoeasypep-masterfrom
martingregoire merged 2 commits intoeasypep-masterfrom
Conversation
When adding custom validation errors, Rails allows both Symbols and
Strings:
```
class Foo < ActiveRecord::Base
validate :add_symbol_error
validate :add_string_error
def add_symbol_error
errors.add(:title, :too_short)
end
def add_string_error
errors.add(:title, 'is too short')
end
end
```
The Symbol error will later be translated by Rails using the i18n logic,
when calling `foo.errors.full_messages` or similar methods. The String
error will stay as it is.
see https://api.rubyonrails.org/v7.0.8.7/classes/ActiveModel/Errors.html#method-i-add
This commit introduces support for these String errors in the error
serialization class.
Before this change, the error response for the validation that was
added in the specs looked like this:
```ruby
[
{
"status"=>"422",
"source"=>{"pointer"=>"/data/attributes/title"},
"title"=>"Unprocessable Entity",
"detail"=> "Title translation missing: en.activerecord.errors.models.note.attributes.title.is not so nice",
"code"=>"is_not_so_nice"
}
]
```
Please note the "translation missing" part in the `detail`, and the
`code` that is a parameterized version of the String error.
After this change, the error response looks like this:
```ruby
[
{
"status"=>"422",
"source"=>{"pointer"=>"/data/attributes/title"},
"title"=>"Unprocessable Entity",
"detail"=>"Title is not so nice",
"code"=>"invalid"
}
]
```
The `detail` is now the String error as passed in by Rails, and the
`code` is a generic `"invalid"` one.
see https://github.com/easyPEP/jsonapi.rb/actions/runs/17267231602/job/49002149588?pr=2 ``` Running RuboCop... RuboCop failed! Inspecting 18 files ................C. Offenses: spec/pagination_spec.rb:61:15: C: [Correctable] Rails/CompactBlank: Use compact_blank instead. }.reject { |_k, _v| _v.blank? } ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ spec/pagination_spec.rb:160:15: C: [Correctable] Rails/CompactBlank: Use compact_blank instead. }.reject { |_k, _v| _v.blank? } ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 18 files inspected, 2 offenses detected, 2 offenses autocorrectable ```
fluxsaas
approved these changes
Aug 28, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When adding custom validation errors, Rails allows both Symbols and Strings:
The Symbol error will later be translated by Rails using the i18n logic, when calling
foo.errors.full_messagesor similar methods. The String error will stay as it is.see https://api.rubyonrails.org/v7.0.8.7/classes/ActiveModel/Errors.html#method-i-add
This commit introduces support for these String errors in the error serialization class.
Before this change, the error response for the validation that was added in the specs looked like this:
Please note the "translation missing" part in the
detail, and thecodethat is a parameterized version of the String error.After this change, the error response looks like this:
The
detailis now the String error as passed in by Rails, and thecodeis a generic"invalid"one.