File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -56,6 +56,7 @@ def json_ids(integers = false)
5656
5757 def validation_errors
5858 @validation_errors ||= { } . tap do |errors |
59+ return errors if json [ 'errors' ] . nil?
5960 json [ 'errors' ] . each do |e |
6061 attr = e [ 'meta' ] [ 'attribute' ] . to_sym
6162 message = e [ 'meta' ] [ 'message' ]
Original file line number Diff line number Diff line change 7070 }
7171 end
7272
73+ let ( :errors_json ) do
74+ {
75+ 'errors' => [
76+ {
77+ 'code' => 'unprocessable_entity' ,
78+ 'status' => '422' ,
79+ 'title' => 'Validation Error' ,
80+ 'detail' => 'Patron must exist' ,
81+ 'source' => {
82+ 'pointer' => '/data/relationships/patron'
83+ } ,
84+ 'meta' => {
85+ 'attribute' => 'patron' ,
86+ 'message' => 'must exist' ,
87+ 'code' => 'blank'
88+ }
89+ }
90+ ]
91+ }
92+ end
93+
7394 describe '#json_item' do
7495 let ( :json ) { show_json }
7596
166187 } )
167188 end
168189 end
190+
191+ describe '#validation_errors' do
192+ let ( :json ) { errors_json }
193+
194+ it 'creates a hash of the errors' do
195+ expect ( validation_errors ) . to eq ( { :patron => 'must exist' } )
196+ end
197+
198+ describe 'when there are no errors' do
199+ let ( :json ) { show_json }
200+ it 'does not raise an error of its own' do
201+ expect { validation_errors } . not_to raise_error
202+ expect ( validation_errors [ :any_key_here ] ) . to be_nil
203+ end
204+ end
205+ end
206+
169207end
You can’t perform that action at this time.
0 commit comments