diff --git a/lib/jsonapi_spec_helpers/helpers.rb b/lib/jsonapi_spec_helpers/helpers.rb index c01e5ec..e37d6c3 100644 --- a/lib/jsonapi_spec_helpers/helpers.rb +++ b/lib/jsonapi_spec_helpers/helpers.rb @@ -56,6 +56,7 @@ def json_ids(integers = false) def validation_errors @validation_errors ||= {}.tap do |errors| + return errors if json['errors'].nil? json['errors'].each do |e| attr = e['meta']['attribute'].to_sym message = e['meta']['message'] diff --git a/spec/helpers_spec.rb b/spec/helpers_spec.rb index 44e8440..ece18e1 100644 --- a/spec/helpers_spec.rb +++ b/spec/helpers_spec.rb @@ -70,6 +70,27 @@ } end + let(:errors_json) do + { + 'errors' => [ + { + 'code' => 'unprocessable_entity', + 'status' => '422', + 'title' => 'Validation Error', + 'detail' => 'Patron must exist', + 'source' => { + 'pointer' => '/data/relationships/patron' + }, + 'meta' => { + 'attribute' => 'patron', + 'message' => 'must exist', + 'code' => 'blank' + } + } + ] + } + end + describe '#json_item' do let(:json) { show_json } @@ -166,4 +187,21 @@ }) end end + + describe '#validation_errors' do + let(:json) { errors_json } + + it 'creates a hash of the errors' do + expect(validation_errors).to eq({:patron => 'must exist'}) + end + + describe 'when there are no errors' do + let(:json) { show_json } + it 'does not raise an error of its own' do + expect{ validation_errors }.not_to raise_error + expect(validation_errors[:any_key_here]).to be_nil + end + end + end + end