Browse Source

Full error messages (#57)

* Full error messages

* Adjust error in spec
pull/58/head
Daniel Baark 3 years ago committed by GitHub
parent
commit
cc17a48ee2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      Gemfile.lock
  2. 6
      app/controllers/case_logs_controller.rb
  3. 10
      app/models/case_log.rb
  4. 13
      docs/api/DLUHC-CORE-Data.v1.json
  5. 4
      spec/requests/case_log_controller_spec.rb

10
Gemfile.lock

@ -1,6 +1,6 @@
GIT GIT
remote: https://github.com/rspec/rspec-core.git remote: https://github.com/rspec/rspec-core.git
revision: 053fcfeb6b0b6627edf7261737553a6f7df8cc14 revision: d57c371ee92b16211b80ac7b0b025968438f5297
branch: main branch: main
specs: specs:
rspec-core (3.11.0.pre) rspec-core (3.11.0.pre)
@ -198,7 +198,7 @@ GEM
public_suffix (4.0.6) public_suffix (4.0.6)
puma (5.5.2) puma (5.5.2)
nio4r (~> 2.0) nio4r (~> 2.0)
racc (1.5.2) racc (1.6.0)
rack (2.2.3) rack (2.2.3)
rack-mini-profiler (2.3.3) rack-mini-profiler (2.3.3)
rack (>= 1.2.0) rack (>= 1.2.0)
@ -279,7 +279,7 @@ GEM
sass (~> 3.5, >= 3.5.5) sass (~> 3.5, >= 3.5.5)
scss_lint-govuk (0.2.0) scss_lint-govuk (0.2.0)
scss_lint scss_lint
selenium-webdriver (4.0.0) selenium-webdriver (4.0.3)
childprocess (>= 0.5, < 5.0) childprocess (>= 0.5, < 5.0)
rexml (~> 3.2, >= 3.2.5) rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2) rubyzip (>= 1.2.2)
@ -300,7 +300,7 @@ GEM
stimulus-rails (0.7.1) stimulus-rails (0.7.1)
rails (>= 6.0.0) rails (>= 6.0.0)
thor (1.1.0) thor (1.1.0)
turbo-rails (7.1.0) turbo-rails (7.1.1)
rails (>= 6.0.0) rails (>= 6.0.0)
tzinfo (2.0.4) tzinfo (2.0.4)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
@ -323,7 +323,7 @@ GEM
websocket-extensions (0.1.5) websocket-extensions (0.1.5)
xpath (3.2.0) xpath (3.2.0)
nokogiri (~> 1.8) nokogiri (~> 1.8)
zeitwerk (2.4.2) zeitwerk (2.5.1)
PLATFORMS PLATFORMS
x86_64-darwin-19 x86_64-darwin-19

6
app/controllers/case_logs_controller.rb

@ -15,7 +15,7 @@ class CaseLogsController < ApplicationController
if case_log.persisted? if case_log.persisted?
render json: case_log, status: :created render json: case_log, status: :created
else else
render json: { errors: case_log.errors.full_messages }, status: :unprocessable_entity render json: { errors: case_log.errors.messages }, status: :unprocessable_entity
end end
end end
end end
@ -26,7 +26,7 @@ class CaseLogsController < ApplicationController
if case_log.update(api_case_log_params) if case_log.update(api_case_log_params)
render json: case_log, status: :ok render json: case_log, status: :ok
else else
render json: { errors: case_log.errors.full_messages }, status: :unprocessable_entity render json: { errors: case_log.errors.messages }, status: :unprocessable_entity
end end
else else
render json: { error: "Case Log #{params[:id]} not found" }, status: :not_found render json: { error: "Case Log #{params[:id]} not found" }, status: :not_found
@ -74,7 +74,7 @@ class CaseLogsController < ApplicationController
if case_log.discard if case_log.discard
head :no_content head :no_content
else else
render json: { errors: case_log.errors.full_messages }, status: :unprocessable_entity render json: { errors: case_log.errors.messages }, status: :unprocessable_entity
end end
else else
render json: { error: "Case Log #{params[:id]} not found" }, status: :not_found render json: { error: "Case Log #{params[:id]} not found" }, status: :not_found

10
app/models/case_log.rb

@ -5,26 +5,26 @@ class CaseLogValidator < ActiveModel::Validator
def validate_tenant_age(record) def validate_tenant_age(record)
if record.tenant_age && !/^[1-9][0-9]?$|^120$/.match?(record.tenant_age.to_s) if record.tenant_age && !/^[1-9][0-9]?$|^120$/.match?(record.tenant_age.to_s)
record.errors.add :tenant_age, "must be between 0 and 120" record.errors.add :tenant_age, "Tenant age must be between 0 and 120"
end end
end end
def validate_property_number_of_times_relet(record) def validate_property_number_of_times_relet(record)
if record.property_number_of_times_relet && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.property_number_of_times_relet.to_s) if record.property_number_of_times_relet && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.property_number_of_times_relet.to_s)
record.errors.add :property_number_of_times_relet, "must be between 0 and 20" record.errors.add :property_number_of_times_relet, "Must be between 0 and 20"
end end
end end
def validate_reasonable_preference(record) def validate_reasonable_preference(record)
if record.homelessness == "No" && record.reasonable_preference == "Yes" if record.homelessness == "No" && record.reasonable_preference == "Yes"
record.errors.add :reasonable_preference, "can not be Yes if Not Homeless immediately prior to this letting has been selected" record.errors.add :reasonable_preference, "Can not be Yes if Not Homeless immediately prior to this letting has been selected"
elsif record.reasonable_preference == "Yes" elsif record.reasonable_preference == "Yes"
if !record.reasonable_preference_reason_homeless && !record.reasonable_preference_reason_unsatisfactory_housing && !record.reasonable_preference_reason_medical_grounds && !record.reasonable_preference_reason_avoid_hardship && !record.reasonable_preference_reason_do_not_know if !record.reasonable_preference_reason_homeless && !record.reasonable_preference_reason_unsatisfactory_housing && !record.reasonable_preference_reason_medical_grounds && !record.reasonable_preference_reason_avoid_hardship && !record.reasonable_preference_reason_do_not_know
record.errors.add :reasonable_preference_reason, "- if reasonable preference is Yes, a reason must be given" record.errors.add :reasonable_preference_reason, "If reasonable preference is Yes, a reason must be given"
end end
elsif record.reasonable_preference == "No" elsif record.reasonable_preference == "No"
if record.reasonable_preference_reason_homeless || record.reasonable_preference_reason_unsatisfactory_housing || record.reasonable_preference_reason_medical_grounds || record.reasonable_preference_reason_avoid_hardship || record.reasonable_preference_reason_do_not_know if record.reasonable_preference_reason_homeless || record.reasonable_preference_reason_unsatisfactory_housing || record.reasonable_preference_reason_medical_grounds || record.reasonable_preference_reason_avoid_hardship || record.reasonable_preference_reason_do_not_know
record.errors.add :reasonable_preference_reason, "- if reasonable preference is No, no reasons should be given" record.errors.add :reasonable_preference_reason, "If reasonable preference is No, no reasons should be given"
end end
end end
end end

13
docs/api/DLUHC-CORE-Data.v1.json

@ -108,7 +108,8 @@
"examples": { "examples": {
"Invalid Age": { "Invalid Age": {
"value": { "value": {
"errors": [ "errors": {
"tenant_age": [
"Tenant age must be between 0 and 120" "Tenant age must be between 0 and 120"
] ]
} }
@ -117,6 +118,7 @@
} }
} }
} }
}
}, },
"description": "Update the information of an existing case log", "description": "Update the information of an existing case log",
"requestBody": { "requestBody": {
@ -211,9 +213,13 @@
"properties": {} "properties": {}
}, },
"examples": { "examples": {
"Invalid Age": { "Invalid Age & Reasonable Preference": {
"value": { "value": {
"errors": [ "errors": {
"reasonable_preference_reason": [
"If reasonable preference is Yes, a reason must be given"
],
"tenant_age": [
"Tenant age must be between 0 and 120" "Tenant age must be between 0 and 120"
] ]
} }
@ -222,6 +228,7 @@
} }
} }
} }
}
}, },
"requestBody": { "requestBody": {
"content": { "content": {

4
spec/requests/case_log_controller_spec.rb

@ -66,7 +66,7 @@ RSpec.describe CaseLogsController, type: :request do
it "validates case log parameters" do it "validates case log parameters" do
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
expect(json_response["errors"]).to match_array(["Tenant age must be between 0 and 120", "Property number of times relet must be between 0 and 20"]) expect(json_response["errors"]).to match_array([["property_number_of_times_relet", ["Must be between 0 and 20"]], ["tenant_age", ["Tenant age must be between 0 and 120"]]])
end end
end end
@ -157,7 +157,7 @@ RSpec.describe CaseLogsController, type: :request do
it "returns an error message" do it "returns an error message" do
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response["errors"]).to eq(["Tenant age must be between 0 and 120"]) expect(json_response["errors"]).to eq({ "tenant_age" => ["Tenant age must be between 0 and 120"] })
end end
end end

Loading…
Cancel
Save