Browse Source

CLDC-2367 Add tenancy length validation to rent_type field (#1653)

* feat: add tenancy length validations to rent_type as well

* feat: update tests and conditionally add rent_type errors
pull/1656/head
natdeanlewissoftwire 2 years ago committed by GitHub
parent
commit
f5bba4ac5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      app/models/validations/tenancy_validations.rb
  2. 3
      spec/models/validations/tenancy_validations_spec.rb

16
app/models/validations/tenancy_validations.rb

@ -6,11 +6,7 @@ module Validations::TenancyValidations
def validate_fixed_term_tenancy(record) def validate_fixed_term_tenancy(record)
is_present = record.tenancylength.present? is_present = record.tenancylength.present?
is_in_range = record.tenancylength.to_i.between?(min_tenancy_length(record), 99) is_in_range = record.tenancylength.to_i.between?(min_tenancy_length(record), 99)
conditions = [ rent_type_dependent_conditions = [
{
condition: !(record.is_secure_tenancy? || record.is_assured_shorthold_tenancy?) && is_present,
error: I18n.t("validations.tenancy.length.fixed_term_not_required"),
},
{ {
condition: (record.is_assured_shorthold_tenancy? && !is_in_range) && is_present, condition: (record.is_assured_shorthold_tenancy? && !is_in_range) && is_present,
error: I18n.t( error: I18n.t(
@ -19,18 +15,26 @@ module Validations::TenancyValidations
), ),
}, },
{ {
condition: record.is_secure_tenancy? && (!is_in_range && is_present), condition: (record.is_secure_tenancy? && !is_in_range) && is_present,
error: I18n.t( error: I18n.t(
"validations.tenancy.length.secure", "validations.tenancy.length.secure",
min_tenancy_length: min_tenancy_length(record), min_tenancy_length: min_tenancy_length(record),
), ),
}, },
] ]
rent_type_independent_conditions = [
{
condition: !(record.is_secure_tenancy? || record.is_assured_shorthold_tenancy?) && is_present,
error: I18n.t("validations.tenancy.length.fixed_term_not_required"),
},
]
conditions = rent_type_dependent_conditions + rent_type_independent_conditions
conditions.each do |condition| conditions.each do |condition|
next unless condition[:condition] next unless condition[:condition]
record.errors.add :needstype, condition[:error] record.errors.add :needstype, condition[:error]
record.errors.add :rent_type, condition[:error] if rent_type_dependent_conditions.include?(condition)
record.errors.add :tenancylength, :tenancylength_invalid, message: condition[:error] record.errors.add :tenancylength, :tenancylength_invalid, message: condition[:error]
record.errors.add :tenancy, condition[:error] record.errors.add :tenancy, condition[:error]
end end

3
spec/models/validations/tenancy_validations_spec.rb

@ -24,6 +24,7 @@ RSpec.describe Validations::TenancyValidations do
record.tenancylength = 10 record.tenancylength = 10
tenancy_validator.validate_fixed_term_tenancy(record) tenancy_validator.validate_fixed_term_tenancy(record)
expect(record.errors["needstype"]).to include(match(expected_error)) expect(record.errors["needstype"]).to include(match(expected_error))
expect(record.errors["rent_type"]).not_to include(match(expected_error))
expect(record.errors["tenancylength"]).to include(match(expected_error)) expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error)) expect(record.errors["tenancy"]).to include(match(expected_error))
end end
@ -44,6 +45,7 @@ RSpec.describe Validations::TenancyValidations do
record.tenancylength = 1 record.tenancylength = 1
tenancy_validator.validate_fixed_term_tenancy(record) tenancy_validator.validate_fixed_term_tenancy(record)
expect(record.errors["needstype"]).to include(match(expected_error)) expect(record.errors["needstype"]).to include(match(expected_error))
expect(record.errors["rent_type"]).to include(match(expected_error))
expect(record.errors["tenancylength"]).to include(match(expected_error)) expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error)) expect(record.errors["tenancy"]).to include(match(expected_error))
end end
@ -54,6 +56,7 @@ RSpec.describe Validations::TenancyValidations do
record.tenancylength = 100 record.tenancylength = 100
tenancy_validator.validate_fixed_term_tenancy(record) tenancy_validator.validate_fixed_term_tenancy(record)
expect(record.errors["needstype"]).to include(match(expected_error)) expect(record.errors["needstype"]).to include(match(expected_error))
expect(record.errors["rent_type"]).to include(match(expected_error))
expect(record.errors["tenancylength"]).to include(match(expected_error)) expect(record.errors["tenancylength"]).to include(match(expected_error))
expect(record.errors["tenancy"]).to include(match(expected_error)) expect(record.errors["tenancy"]).to include(match(expected_error))
end end

Loading…
Cancel
Save