From da207c4715ffd3abc247fc00b77a87ec121876f9 Mon Sep 17 00:00:00 2001 From: Don Denton Date: Mon, 29 Jan 2018 01:48:07 -0600 Subject: [PATCH 1/4] Add specs for validation_errors Tests the current, desired behavior Also displays the current, unwanted behavior --- spec/helpers_spec.rb | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/spec/helpers_spec.rb b/spec/helpers_spec.rb index 44e8440..5e28944 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,20 @@ }) 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 + end + end + end + end From cc3bbd7238d9c2ef1ac41e20cc88e11b2eff0b74 Mon Sep 17 00:00:00 2001 From: Don Denton Date: Mon, 29 Jan 2018 01:49:37 -0600 Subject: [PATCH 2/4] Fix issue #9 It's the least I can do for opening it in the first place. --- lib/jsonapi_spec_helpers/helpers.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/jsonapi_spec_helpers/helpers.rb b/lib/jsonapi_spec_helpers/helpers.rb index c01e5ec..772e2f2 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 if json['errors'].nil? json['errors'].each do |e| attr = e['meta']['attribute'].to_sym message = e['meta']['message'] From 43a7c8cc9a1f200d1c170c3bdc6087bacd3b0edc Mon Sep 17 00:00:00 2001 From: Don Denton Date: Mon, 29 Jan 2018 09:34:17 -0600 Subject: [PATCH 3/4] Add new spec for validation_errors This extra expectation ensures that `validation_errors[:any_key]` also won't throw an error. --- spec/helpers_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/helpers_spec.rb b/spec/helpers_spec.rb index 5e28944..ece18e1 100644 --- a/spec/helpers_spec.rb +++ b/spec/helpers_spec.rb @@ -199,6 +199,7 @@ 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 From 9c32ac83fc22b5fc75cdb428ff31a9d64fd53264 Mon Sep 17 00:00:00 2001 From: Don Denton Date: Mon, 29 Jan 2018 09:34:41 -0600 Subject: [PATCH 4/4] Make the new spec pass --- lib/jsonapi_spec_helpers/helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jsonapi_spec_helpers/helpers.rb b/lib/jsonapi_spec_helpers/helpers.rb index 772e2f2..e37d6c3 100644 --- a/lib/jsonapi_spec_helpers/helpers.rb +++ b/lib/jsonapi_spec_helpers/helpers.rb @@ -56,7 +56,7 @@ def json_ids(integers = false) def validation_errors @validation_errors ||= {}.tap do |errors| - return if json['errors'].nil? + return errors if json['errors'].nil? json['errors'].each do |e| attr = e['meta']['attribute'].to_sym message = e['meta']['message']