diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 9e23699e1..e9afa7807 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -1,5 +1,6 @@ class CaseLogValidator < ActiveModel::Validator - # Validations methods need to be called 'validate_' to run on model save + # Validations methods need to be called 'validate_' to run on model save + # or 'validate_' to run on submit as well include HouseholdValidations include PropertyValidations include FinancialValidations @@ -132,7 +133,7 @@ private dynamically_not_required << "net_income" dynamically_not_required << "net_income_frequency" end - + start_range = (household_number_of_other_members || 0) + 2 (start_range..8).each do |n| dynamically_not_required << "person_#{n}_age" diff --git a/app/validations/financial_validations.rb b/app/validations/financial_validations.rb index 90093e134..3567e334d 100644 --- a/app/validations/financial_validations.rb +++ b/app/validations/financial_validations.rb @@ -1,5 +1,6 @@ module FinancialValidations - # Validations methods need to be called 'validate_' to run on model save + # Validations methods need to be called 'validate_' to run on model save + # or 'validate_' to run on submit as well def validate_outstanding_rent_amount(record) if record.outstanding_rent_or_charges == "Yes" && record.outstanding_amount.blank? record.errors.add :outstanding_amount, "You must answer the oustanding amout question if you have outstanding rent or charges." diff --git a/app/validations/household_validations.rb b/app/validations/household_validations.rb index e0e17d97d..d458b7607 100644 --- a/app/validations/household_validations.rb +++ b/app/validations/household_validations.rb @@ -1,5 +1,6 @@ module HouseholdValidations - # Validations methods need to be called 'validate_' to run on model save + # Validations methods need to be called 'validate_' to run on model save + # or 'validate_' to run on submit as well 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" @@ -50,16 +51,20 @@ module HouseholdValidations end end - def validate_other_household_members(record) - (1..8).each do |n| + def validate_household_number_of_other_members(record) + (2..8).each do |n| validate_person_age(record, n) validate_person_age_matches_economic_status(record, n) - validate_person_age_matches_relationship(record, n) if n > 1 + validate_person_age_matches_relationship(record, n) validate_person_age_and_gender_match_economic_status(record, n) end validate_partner_count(record) end + def validate_person_1_age(record) + validate_person_age(record, 1) + end + private def women_of_child_bearing_age_in_household(record) @@ -72,8 +77,8 @@ private def validate_person_age(record, person_num) age = record.public_send("person_#{person_num}_age") - return unless age - + return unless age + if !age.is_a?(Integer) || age < 1 || age > 120 record.errors.add "person_#{person_num}_age".to_sym, "Tenant age must be an integer between 0 and 120" end @@ -98,7 +103,7 @@ private def validate_person_age_matches_relationship(record, person_num) age = record.public_send("person_#{person_num}_age") relationship = record.public_send("person_#{person_num}_relationship") - return unless age && relationship + return unless age && relationship if age < 16 && relationship != "Child - includes young adult and grown-up" record.errors.add "person_#{person_num}_relationship", "Tenant #{person_num}'s relationship to tenant 1 must be Child if their age is under 16" @@ -122,9 +127,9 @@ private def validate_partner_count(record) # TODO probably need to keep track of which specific field is wrong so we can highlight it in the UI - partner_count = (2..8).map { |n| record.public_send("person_#{n}_relationship") }.uniq.count + partner_count = (2..8).select { |n| record.public_send("person_#{n}_relationship") == "Partner" }.count if partner_count > 1 - record.errors.add :base, "Number of partners cannot be greater than 1" + record.errors.add :base, "Number of partners cannot be greater than 1" end end end diff --git a/app/validations/property_validations.rb b/app/validations/property_validations.rb index 9d40a2ee5..65dde4a08 100644 --- a/app/validations/property_validations.rb +++ b/app/validations/property_validations.rb @@ -1,8 +1,9 @@ module PropertyValidations - # Validations methods need to be called 'validate_' to run on model save + # Validations methods need to be called 'validate_' to run on model save + # or 'validate_' to run on submit as well 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, "Property number of times relet must be between 0 and 20" end end end diff --git a/app/validations/tenancy_validations.rb b/app/validations/tenancy_validations.rb index 063b4672c..5738bc2a3 100644 --- a/app/validations/tenancy_validations.rb +++ b/app/validations/tenancy_validations.rb @@ -1,5 +1,6 @@ module TenancyValidations - # Validations methods need to be called 'validate_' to run on model save + # Validations methods need to be called 'validate_' to run on model save + # or 'validate_' to run on submit as well def validate_fixed_term_tenancy(record) is_present = record.fixed_term_tenancy.present? is_in_range = record.fixed_term_tenancy.to_i.between?(2, 99) diff --git a/docs/api/DLUHC-CORE-Data.v1.json b/docs/api/DLUHC-CORE-Data.v1.json index 3820d9aee..fed09b9d6 100644 --- a/docs/api/DLUHC-CORE-Data.v1.json +++ b/docs/api/DLUHC-CORE-Data.v1.json @@ -110,7 +110,7 @@ "value": { "errors": { "person_1_age": [ - "Tenant age must be between 0 and 120" + "Tenant age must be an integer between 0 and 120" ] } } @@ -220,7 +220,7 @@ "If reasonable preference is Yes, a reason must be given" ], "person_1_age": [ - "Tenant age must be between 0 and 120" + "Tenant age must be an integer between 0 and 120" ] } } diff --git a/spec/requests/case_log_controller_spec.rb b/spec/requests/case_log_controller_spec.rb index 6ddbd0130..50a2250ed 100644 --- a/spec/requests/case_log_controller_spec.rb +++ b/spec/requests/case_log_controller_spec.rb @@ -66,7 +66,7 @@ RSpec.describe CaseLogsController, type: :request do it "validates case log parameters" do json_response = JSON.parse(response.body) expect(response).to have_http_status(:unprocessable_entity) - expect(json_response["errors"]).to match_array([["property_number_of_times_relet", ["Must be between 0 and 20"]], ["person_1_age", ["Tenant age must be between 0 and 120"]]]) + expect(json_response["errors"]).to match_array([["property_number_of_times_relet", ["Property number of times relet must be between 0 and 20"]], ["person_1_age", ["Tenant age must be an integer between 0 and 120"]]]) end end @@ -165,7 +165,7 @@ RSpec.describe CaseLogsController, type: :request do it "returns an error message" do json_response = JSON.parse(response.body) - expect(json_response["errors"]).to eq({ "person_1_age" => ["Tenant age must be between 0 and 120"] }) + expect(json_response["errors"]).to eq({ "person_1_age" => ["Tenant age must be an integer between 0 and 120"] }) end end