From 35cac321e2ba35b5f9da8714b9857ccca6be9fda Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Fri, 22 Oct 2021 11:59:57 +0100 Subject: [PATCH] Full error messages --- app/controllers/case_logs_controller.rb | 6 +++--- app/models/case_log.rb | 10 +++++----- docs/api/DLUHC-CORE-Data.v1.json | 21 ++++++++++++++------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index c7025e1a7..0a6cbea54 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -15,7 +15,7 @@ class CaseLogsController < ApplicationController if case_log.persisted? render json: case_log, status: :created 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 @@ -26,7 +26,7 @@ class CaseLogsController < ApplicationController if case_log.update(api_case_log_params) render json: case_log, status: :ok else - render json: { errors: case_log.errors.full_messages }, status: :unprocessable_entity + render json: { errors: case_log.errors.messages }, status: :unprocessable_entity end else render json: { error: "Case Log #{params[:id]} not found" }, status: :not_found @@ -74,7 +74,7 @@ class CaseLogsController < ApplicationController if case_log.discard head :no_content else - render json: { errors: case_log.errors.full_messages }, status: :unprocessable_entity + render json: { errors: case_log.errors.messages }, status: :unprocessable_entity end else render json: { error: "Case Log #{params[:id]} not found" }, status: :not_found diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 8fad9709c..a70dff945 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -5,26 +5,26 @@ class CaseLogValidator < ActiveModel::Validator def validate_tenant_age(record) 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 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) - 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 def validate_reasonable_preference(record) 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" 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 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 - 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 diff --git a/docs/api/DLUHC-CORE-Data.v1.json b/docs/api/DLUHC-CORE-Data.v1.json index 8a2b02fc7..015e98a07 100644 --- a/docs/api/DLUHC-CORE-Data.v1.json +++ b/docs/api/DLUHC-CORE-Data.v1.json @@ -108,9 +108,11 @@ "examples": { "Invalid Age": { "value": { - "errors": [ - "Tenant age must be between 0 and 120" - ] + "errors": { + "tenant_age": [ + "Tenant age must be between 0 and 120" + ] + } } } } @@ -211,11 +213,16 @@ "properties": {} }, "examples": { - "Invalid Age": { + "Invalid Age & Reasonable Preference": { "value": { - "errors": [ - "Tenant age must be between 0 and 120" - ] + "errors": { + "reasonable_preference_reason": [ + "If reasonable preference is Yes, a reason must be given" + ], + "tenant_age": [ + "Tenant age must be between 0 and 120" + ] + } } } }