Skip to content

Commit ec5eaaf

Browse files
authored
Merge pull request #10 from EmCousin/patch-1
Update validation.rb to have a translated `detail`
2 parents 3ec204f + 57f0dd4 commit ec5eaaf

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

lib/jsonapi_errorable/serializers/validation.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ def errors
1717
meta = { attribute: attribute, message: message }.merge(@relationship_message)
1818
meta = { relationship: meta } if @relationship_message.present?
1919

20-
detail = "#{attribute.capitalize} #{message}"
21-
22-
if attribute.to_s.downcase == 'base'
23-
detail = message
24-
end
20+
detail = object.errors.full_message(attribute, message)
21+
detail = message if attribute.to_s.downcase == 'base'
2522

2623
{
2724
code: 'unprocessable_entity',

spec/serializers/serializable_validation_spec.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
require 'spec_helper'
22

33
RSpec.describe JsonapiErrorable::Serializers::Validation do
4-
let(:errors_hash) { { username: ["can't be blank"] } }
4+
let(:attribute) { :username }
5+
let(:message) { "can't be blank" }
6+
let(:errors_hash) { { attribute => [message] } }
57

68
let(:object) { double(id: 123).as_null_object }
79
let(:instance) { described_class.new(object) }
@@ -12,6 +14,7 @@
1214
.to receive(:reflect_on_all_associations)
1315
.and_return([double(name: :pets)])
1416
allow(object).to receive_message_chain(:errors, :to_hash) { errors_hash }
17+
allow(object).to receive_message_chain(:errors, :full_message).with(attribute, message) { "#{attribute.capitalize} #{message}" }
1518
end
1619

1720
describe '#errors' do
@@ -23,7 +26,7 @@
2326

2427
context 'when the error is on an attribute' do
2528
before do
26-
allow(object).to receive(:respond_to?).with(:username) { true }
29+
allow(object).to receive(:respond_to?).with(attribute) { true }
2730
end
2831

2932
it 'renders valid JSONAPI error format' do
@@ -45,10 +48,11 @@
4548
end
4649

4750
context 'when the error attribute is "base"' do
48-
let(:errors_hash) { { base: ["Model is invalid"] } }
51+
let(:attribute) { :base }
52+
let(:message) { "Model is invalid" }
4953

5054
before do
51-
allow(object).to receive(:respond_to?).with(:base) { true }
55+
allow(object).to receive(:respond_to?).with(attribute) { true }
5256
end
5357

5458
it 'should not render the attribute in the message detail' do
@@ -72,7 +76,8 @@
7276
end
7377

7478
context 'when the error is on a relationship' do
75-
let(:errors_hash) { { pets: ["is invalid"] } }
79+
let(:attribute) { :pets }
80+
let(:message) { "is invalid" }
7681

7782
it 'puts the source pointer on relationships' do
7883
expect(subject).to eq(
@@ -92,7 +97,7 @@
9297
context 'but the object is not activerecord' do
9398
before do
9499
allow(instance).to receive(:activemodel?) { false }
95-
allow(object).to receive(:respond_to?).with(:pets) { true }
100+
allow(object).to receive(:respond_to?).with(attribute) { true }
96101
end
97102

98103
it 'places the error on attribute' do
@@ -112,7 +117,7 @@
112117

113118
context 'but the object does not respond to this property' do
114119
before do
115-
allow(object).to receive(:respond_to?).with(:pets) { false }
120+
allow(object).to receive(:respond_to?).with(attribute) { false }
116121
end
117122

118123
it 'defaults to relationship' do
@@ -134,10 +139,11 @@
134139
end
135140

136141
context 'when the error is neither a relationship or attribute of the object' do
137-
let(:errors_hash) { { :'foo.bar' => ["is invalid"] } }
142+
let(:attribute) { :'foo.bar' }
143+
let(:message) { "is invalid" }
138144

139145
before do
140-
allow(object).to receive(:respond_to?).with(:'foo.bar') { false }
146+
allow(object).to receive(:respond_to?).with(attribute) { false }
141147
end
142148

143149
it 'puts the source pointer on relationships' do

0 commit comments

Comments
 (0)