diff --git a/app/models/validations/property_validations.rb b/app/models/validations/property_validations.rb index fa79798ae..107460dec 100644 --- a/app/models/validations/property_validations.rb +++ b/app/models/validations/property_validations.rb @@ -66,16 +66,23 @@ module Validations::PropertyValidations end def validate_shared_housing_rooms(record) + if record.beds.present? && record.beds.negative? + record.errors.add :beds, I18n.t("validations.property.beds.negative") + end + unless record.unittype_gn.nil? if record.unittype_gn == "Bedsit" && record.beds != 1 && record.beds.present? record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_bedroom_bedsit") + record.errors.add :beds, I18n.t("validations.property.unittype_gn.one_bedroom_bedsit") end if record.other_hhmemb&.zero? && record.unittype_gn.include?("Shared") && !record.beds.to_i.between?(1, 3) && record.beds.present? record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared") + record.errors.add :beds, I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared") elsif record.unittype_gn.include?("Shared") && record.beds.present? && !record.beds.to_i.between?(1, 7) record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared") + record.errors.add :beds, I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared") end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index c67daa54b..d8eefc05b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -65,6 +65,8 @@ en: one_bedroom_bedsit: "A bedsit can only have one bedroom" one_seven_bedroom_shared: "A shared house must have 1 to 7 bedrooms" one_three_bedroom_single_tenant_shared: "A shared house with less than two tenants must have 1 to 3 bedrooms" + beds: + negative: "Number of bedrooms has to be greater than 0" financial: tshortfall: diff --git a/spec/models/validations/property_validations_spec.rb b/spec/models/validations/property_validations_spec.rb index 7428c25dc..fb11039c9 100644 --- a/spec/models/validations/property_validations_spec.rb +++ b/spec/models/validations/property_validations_spec.rb @@ -75,6 +75,7 @@ RSpec.describe Validations::PropertyValidations do record.unittype_gn = "Bedsit" property_validator.validate_shared_housing_rooms(record) expect(record.errors["unittype_gn"]).to include(match(expected_error)) + expect(record.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit")) end end @@ -86,6 +87,7 @@ RSpec.describe Validations::PropertyValidations do record.unittype_gn = "Bedsit" property_validator.validate_shared_housing_rooms(record) expect(record.errors["unittype_gn"]).to include(match(expected_error)) + expect(record.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit")) end end @@ -98,6 +100,7 @@ RSpec.describe Validations::PropertyValidations do record.other_hhmemb = 2 property_validator.validate_shared_housing_rooms(record) expect(record.errors["unittype_gn"]).to include(match(expected_error)) + expect(record.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared")) end end @@ -110,6 +113,7 @@ RSpec.describe Validations::PropertyValidations do record.other_hhmemb = 2 property_validator.validate_shared_housing_rooms(record) expect(record.errors["unittype_gn"]).to include(match(expected_error)) + expect(record.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared")) end end @@ -122,6 +126,15 @@ RSpec.describe Validations::PropertyValidations do record.other_hhmemb = 0 property_validator.validate_shared_housing_rooms(record) expect(record.errors["unittype_gn"]).to include(match(expected_error)) + expect(record.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared")) + end + end + + context "when a negative number of bedrooms is entered" do + it "adds an error" do + record.beds = -4 + property_validator.validate_shared_housing_rooms(record) + expect(record.errors["beds"]).to include(I18n.t("validations.property.beds.negative")) end end end