Skip to content

Conversation

@martingregoire
Copy link

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:

[
  {
    "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:

[
  {
    "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.

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
```
@martingregoire martingregoire merged commit c1c4023 into easypep-master Aug 28, 2025
0 of 12 checks passed
@martingregoire martingregoire deleted the chore/fix-error-serialization branch August 28, 2025 12:17
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.

3 participants