Browse Source

CLDC-3692: Extract lettings tenancy validations (#2743)

* Extract lettings tenancy validations
pull/2747/head^2
Manny Dinssa 3 months ago committed by GitHub
parent
commit
83e3ddccdd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 39
      app/models/validations/tenancy_validations.rb
  2. 6
      config/locales/en.yml
  3. 25
      config/locales/validations/lettings/tenancy.en.yml
  4. 15
      spec/models/validations/tenancy_validations_spec.rb

39
app/models/validations/tenancy_validations.rb

@ -10,10 +10,9 @@ module Validations::TenancyValidations
min_tenancy_length = 1 min_tenancy_length = 1
return if record.tenancylength.to_i.between?(min_tenancy_length, 99) return if record.tenancylength.to_i.between?(min_tenancy_length, 99)
message = I18n.t("validations.tenancy.length.invalid_fixed", min_tenancy_length:) record.errors.add :needstype, I18n.t("validations.lettings.tenancy.needstype.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :needstype, message record.errors.add :tenancylength, :tenancylength_invalid, message: I18n.t("validations.lettings.tenancy.tenancylength.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :tenancylength, :tenancylength_invalid, message: message record.errors.add :tenancy, I18n.t("validations.lettings.tenancy.tenancy.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :tenancy, message
end end
def validate_general_needs_fixed_tenancy_length_affordable_social_rent(record) def validate_general_needs_fixed_tenancy_length_affordable_social_rent(record)
@ -23,11 +22,10 @@ module Validations::TenancyValidations
min_tenancy_length = 2 min_tenancy_length = 2
return if record.tenancylength.to_i.between?(min_tenancy_length, 99) return if record.tenancylength.to_i.between?(min_tenancy_length, 99)
message = I18n.t("validations.tenancy.length.invalid_fixed", min_tenancy_length:) record.errors.add :needstype, I18n.t("validations.lettings.tenancy.needstype.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :needstype, message record.errors.add :rent_type, I18n.t("validations.lettings.tenancy.rent_type.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :rent_type, message record.errors.add :tenancylength, :tenancylength_invalid, message: I18n.t("validations.lettings.tenancy.tenancylength.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :tenancylength, :tenancylength_invalid, message: message record.errors.add :tenancy, I18n.t("validations.lettings.tenancy.tenancy.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :tenancy, message
end end
def validate_general_needs_fixed_tenancy_length_intermediate_rent(record) def validate_general_needs_fixed_tenancy_length_intermediate_rent(record)
@ -37,11 +35,10 @@ module Validations::TenancyValidations
min_tenancy_length = 1 min_tenancy_length = 1
return if record.tenancylength.to_i.between?(min_tenancy_length, 99) return if record.tenancylength.to_i.between?(min_tenancy_length, 99)
message = I18n.t("validations.tenancy.length.invalid_fixed", min_tenancy_length:) record.errors.add :needstype, I18n.t("validations.lettings.tenancy.needstype.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :needstype, message record.errors.add :rent_type, I18n.t("validations.lettings.tenancy.rent_type.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :rent_type, message record.errors.add :tenancylength, :tenancylength_invalid, message: I18n.t("validations.lettings.tenancy.tenancylength.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :tenancylength, :tenancylength_invalid, message: message record.errors.add :tenancy, I18n.t("validations.lettings.tenancy.tenancy.invalid_fixed_tenancylength", min_tenancy_length:)
record.errors.add :tenancy, message
end end
def validate_periodic_tenancy_length(record) def validate_periodic_tenancy_length(record)
@ -50,18 +47,16 @@ module Validations::TenancyValidations
min_tenancy_length = 1 min_tenancy_length = 1
return if record.tenancylength.to_i.between?(min_tenancy_length, 99) return if record.tenancylength.to_i.between?(min_tenancy_length, 99)
message = I18n.t("validations.tenancy.length.invalid_periodic", min_tenancy_length:) record.errors.add :tenancylength, :tenancylength_invalid, message: I18n.t("validations.lettings.tenancy.tenancylength.invalid_periodic_tenancylength", min_tenancy_length:)
record.errors.add :tenancylength, :tenancylength_invalid, message: message record.errors.add :tenancy, I18n.t("validations.lettings.tenancy.tenancy.invalid_periodic_tenancylength", min_tenancy_length:)
record.errors.add :tenancy, message
end end
def validate_tenancy_length_blank_when_not_required(record) def validate_tenancy_length_blank_when_not_required(record)
return if record.tenancylength.blank? return if record.tenancylength.blank?
return if record.tenancy_type_fixed_term? || record.is_periodic_tenancy? return if record.tenancy_type_fixed_term? || record.is_periodic_tenancy?
message = I18n.t("validations.tenancy.length.fixed_term_not_required") record.errors.add :tenancylength, :tenancylength_invalid, message: I18n.t("validations.lettings.tenancy.tenancylength.fixed_term_not_required")
record.errors.add :tenancylength, :tenancylength_invalid, message: message record.errors.add :tenancy, I18n.t("validations.lettings.tenancy.tenancy.fixed_term_not_required")
record.errors.add :tenancy, message
end end
def validate_other_tenancy_type(record) def validate_other_tenancy_type(record)
@ -72,8 +67,8 @@ module Validations::TenancyValidations
return unless record.collection_start_year && record.joint return unless record.collection_start_year && record.joint
if record.hhmemb == 1 && record.joint == 1 && record.collection_start_year >= 2022 if record.hhmemb == 1 && record.joint == 1 && record.collection_start_year >= 2022
record.errors.add :joint, :not_joint_tenancy, message: I18n.t("validations.tenancy.not_joint") record.errors.add :joint, :not_joint_tenancy, message: I18n.t("validations.lettings.tenancy.joint.sole_tenancy")
record.errors.add :hhmemb, I18n.t("validations.tenancy.joint_more_than_one_member") record.errors.add :hhmemb, I18n.t("validations.lettings.tenancy.joint.multiple_members_required")
end end
end end
end end

6
config/locales/en.yml

@ -541,14 +541,8 @@ en:
nationality: "Select a valid nationality." nationality: "Select a valid nationality."
tenancy: tenancy:
length:
fixed_term_not_required: "You must only answer the length of the tenancy if it's fixed-term."
invalid_fixed: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type."
invalid_periodic: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type."
internal_transfer: "Answer must be secure tenancy as this tenancy is an internal transfer." internal_transfer: "Answer must be secure tenancy as this tenancy is an internal transfer."
cannot_be_internal_transfer: "Answer cannot be internal transfer as this is not a secure tenancy." cannot_be_internal_transfer: "Answer cannot be internal transfer as this is not a secure tenancy."
not_joint: "This cannot be a joint tenancy as you've told us there's only one person in the household."
joint_more_than_one_member: "There must be more than one person in the household as you've told us this is a joint tenancy."
declaration: declaration:
missing: missing:

25
config/locales/validations/lettings/tenancy.en.yml

@ -0,0 +1,25 @@
en:
validations:
lettings:
tenancy:
needstype:
invalid_fixed_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type."
invalid_periodic_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type."
tenancylength:
invalid_fixed_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type."
invalid_periodic_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type."
fixed_term_not_required: "You must only answer the length of the tenancy if it's fixed-term."
tenancy:
invalid_fixed_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type."
invalid_periodic_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type."
fixed_term_not_required: "You must only answer the length of the tenancy if it's fixed-term."
rent_type:
invalid_fixed_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type."
invalid_periodic_tenancylength: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type."
joint:
sole_tenancy: "This cannot be a joint tenancy as you've told us there's only one person in the household."
multiple_members_required: "There must be more than one person in the household as you've told us this is a joint tenancy."

15
spec/models/validations/tenancy_validations_spec.rb

@ -76,12 +76,12 @@ RSpec.describe Validations::TenancyValidations do
{ {
name: "assured shorthold", name: "assured shorthold",
code: 4, code: 4,
expected_error: ->(min_tenancy_length) { I18n.t("validations.tenancy.length.invalid_fixed", min_tenancy_length:) }, expected_error: ->(min_tenancy_length) { I18n.t("validations.lettings.tenancy.tenancylength.invalid_fixed_tenancylength", min_tenancy_length:) },
}, },
{ {
name: "secure fixed term", name: "secure fixed term",
code: 6, code: 6,
expected_error: ->(min_tenancy_length) { I18n.t("validations.tenancy.length.invalid_fixed", min_tenancy_length:) }, expected_error: ->(min_tenancy_length) { I18n.t("validations.lettings.tenancy.tenancylength.invalid_fixed_tenancylength", min_tenancy_length:) },
}, },
] ]
@ -211,7 +211,7 @@ RSpec.describe Validations::TenancyValidations do
periodic_tenancy_case = { periodic_tenancy_case = {
name: "periodic", name: "periodic",
code: 8, code: 8,
expected_error: ->(min_tenancy_length) { I18n.t("validations.tenancy.length.invalid_periodic", min_tenancy_length:) }, expected_error: ->(min_tenancy_length) { I18n.t("validations.lettings.tenancy.tenancylength.invalid_periodic_tenancylength", min_tenancy_length:) },
} }
error_fields = %w[tenancylength tenancy] error_fields = %w[tenancylength tenancy]
include_examples "adds expected errors based on the tenancy length", periodic_tenancy_case, error_fields, 1 include_examples "adds expected errors based on the tenancy length", periodic_tenancy_case, error_fields, 1
@ -237,9 +237,8 @@ RSpec.describe Validations::TenancyValidations do
it "adds errors to tenancylength and tenancy" do it "adds errors to tenancylength and tenancy" do
tenancy_validator.validate_tenancy_length_blank_when_not_required(record) tenancy_validator.validate_tenancy_length_blank_when_not_required(record)
expected_error = I18n.t("validations.tenancy.length.fixed_term_not_required") expect(record.errors["tenancylength"]).to include(I18n.t("validations.lettings.tenancy.tenancylength.fixed_term_not_required"))
expect(record.errors["tenancylength"]).to include(expected_error) expect(record.errors["tenancy"]).to include(I18n.t("validations.lettings.tenancy.tenancy.fixed_term_not_required"))
expect(record.errors["tenancy"]).to include(expected_error)
end end
end end
@ -321,8 +320,8 @@ RSpec.describe Validations::TenancyValidations do
describe "joint tenancy validation" do describe "joint tenancy validation" do
context "when the data inputter has said that there is only one member in the household" do context "when the data inputter has said that there is only one member in the household" do
let(:record) { FactoryBot.build(:lettings_log, :setup_completed, hhmemb: 1) } let(:record) { FactoryBot.build(:lettings_log, :setup_completed, hhmemb: 1) }
let(:expected_error) { I18n.t("validations.tenancy.not_joint") } let(:expected_error) { I18n.t("validations.lettings.tenancy.joint.sole_tenancy") }
let(:hhmemb_expected_error) { I18n.t("validations.tenancy.joint_more_than_one_member") } let(:hhmemb_expected_error) { I18n.t("validations.lettings.tenancy.joint.multiple_members_required") }
it "displays an error if the data inputter says the letting is a joint tenancy" do it "displays an error if the data inputter says the letting is a joint tenancy" do
record.joint = 1 record.joint = 1

Loading…
Cancel
Save