Browse Source

CLDC-3822 Update date validations for 2025 (#2886)

* Update date validations for 2025

* Refactor date validations, account for leap years
pull/2897/head^2
kosiakkatrina 6 days ago committed by GitHub
parent
commit
7455440e01
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 55
      app/models/validations/date_validations.rb
  2. 4
      config/locales/validations/lettings/date.en.yml
  3. 123
      spec/models/validations/date_validations_spec.rb

55
app/models/validations/date_validations.rb

@ -2,53 +2,56 @@ module Validations::DateValidations
include Validations::SharedValidations include Validations::SharedValidations
def validate_property_major_repairs(record) def validate_property_major_repairs(record)
date_valid?("mrcdate", record) return unless record["mrcdate"].present? && date_valid?("mrcdate", record)
if record["startdate"].present? && record["mrcdate"].present? && record["startdate"] < record["mrcdate"]
record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.before_tenancy_start")
end
if is_rsnvac_first_let?(record) && record["mrcdate"].present? if is_rsnvac_first_let?(record) && record["mrcdate"].present?
record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.not_first_let") record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.not_first_let")
end end
return unless record["startdate"].present? && date_valid?("startdate", record)
if record["mrcdate"].present? && record["startdate"].present? && record["startdate"].to_date - record["mrcdate"].to_date > 3650 if record["startdate"].present? && record["mrcdate"].present? && record["startdate"] < record["mrcdate"]
record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.before_tenancy_start")
record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.after_major_repair_date")
end
if record.form.start_year_2025_or_later?
if record["startdate"].to_date - 20.years > record["mrcdate"].to_date
record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.twenty_years_before_tenancy_start")
record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.twenty_years_after_mrc_date")
end
elsif record["startdate"].to_date - 10.years > record["mrcdate"].to_date
record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.ten_years_before_tenancy_start") record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.ten_years_before_tenancy_start")
record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.ten_years_after_mrc_date")
end end
end end
def validate_property_void_date(record) def validate_property_void_date(record)
if record["voiddate"].present? && record["startdate"].present? && record["startdate"].to_date - record["voiddate"].to_date > 3650 return unless record["voiddate"].present? && date_valid?("voiddate", record)
record.errors.add :voiddate, I18n.t("validations.lettings.date.void_date.ten_years_before_tenancy_start")
end
if record["voiddate"].present? && record["startdate"].present? && record["startdate"].to_date < record["voiddate"].to_date
record.errors.add :voiddate, I18n.t("validations.lettings.date.void_date.before_tenancy_start")
end
if record["voiddate"].present? && record["mrcdate"].present? && record["mrcdate"].to_date < record["voiddate"].to_date if record["mrcdate"].present? && record["mrcdate"].to_date < record["voiddate"].to_date
record.errors.add :voiddate, :after_mrcdate, message: I18n.t("validations.lettings.date.void_date.after_mrcdate") record.errors.add :voiddate, :after_mrcdate, message: I18n.t("validations.lettings.date.void_date.after_mrcdate")
record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.before_void_date") record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.before_void_date")
end end
end return unless record["startdate"].present? && date_valid?("startdate", record)
def validate_startdate(record)
return unless record.startdate && date_valid?("startdate", record)
if record["voiddate"].present? && record.startdate < record["voiddate"] if record["startdate"].to_date < record["voiddate"].to_date
record.errors.add :voiddate, I18n.t("validations.lettings.date.void_date.before_tenancy_start")
record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.after_void_date") record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.after_void_date")
end end
if record["mrcdate"].present? && record.startdate < record["mrcdate"] if record.form.start_year_2025_or_later?
record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.after_major_repair_date") if record["startdate"].to_date - 20.years > record["voiddate"].to_date
end record.errors.add :voiddate, I18n.t("validations.lettings.date.void_date.twenty_years_before_tenancy_start")
record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.twenty_years_after_void_date")
if record["voiddate"].present? && record["startdate"].to_date - record["voiddate"].to_date > 3650 end
elsif record["startdate"].to_date - 10.years > record["voiddate"].to_date
record.errors.add :voiddate, I18n.t("validations.lettings.date.void_date.ten_years_before_tenancy_start")
record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.ten_years_after_void_date") record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.ten_years_after_void_date")
end end
end
if record["mrcdate"].present? && record["startdate"].to_date - record["mrcdate"].to_date > 3650 def validate_startdate(record)
record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.ten_years_after_mrc_date") date_valid?("startdate", record)
end
end end
private private

4
config/locales/validations/lettings/date.en.yml

@ -7,14 +7,18 @@ en:
after_major_repair_date: "Enter a tenancy start date that is after the major repair date." after_major_repair_date: "Enter a tenancy start date that is after the major repair date."
ten_years_after_void_date: "Enter a tenancy start date that is no more than 10 years after the void date." ten_years_after_void_date: "Enter a tenancy start date that is no more than 10 years after the void date."
ten_years_after_mrc_date: "Enter a tenancy start date that is no more than 10 years after the major repairs completion date." ten_years_after_mrc_date: "Enter a tenancy start date that is no more than 10 years after the major repairs completion date."
twenty_years_after_void_date: "Enter a tenancy start date that is no more than 20 years after the void date."
twenty_years_after_mrc_date: "Enter a tenancy start date that is no more than 20 years after the major repairs completion date."
mrcdate: mrcdate:
before_tenancy_start: "Enter a major repairs date that is before the tenancy start date." before_tenancy_start: "Enter a major repairs date that is before the tenancy start date."
not_first_let: "Major repairs date must not be completed if the tenancy is a first let." not_first_let: "Major repairs date must not be completed if the tenancy is a first let."
ten_years_before_tenancy_start: "Enter a major repairs completion date that is no more than 10 years before the tenancy start date." ten_years_before_tenancy_start: "Enter a major repairs completion date that is no more than 10 years before the tenancy start date."
twenty_years_before_tenancy_start: "Enter a major repairs completion date that is no more than 20 years before the tenancy start date."
before_void_date: "Major repairs date must be after the void date if provided." before_void_date: "Major repairs date must be after the void date if provided."
void_date: void_date:
ten_years_before_tenancy_start: "Enter a void date no more than 10 years before the tenancy start date." ten_years_before_tenancy_start: "Enter a void date no more than 10 years before the tenancy start date."
twenty_years_before_tenancy_start: "Enter a void date no more than 20 years before the tenancy start date."
before_tenancy_start: "Enter a void date that is before the tenancy start date." before_tenancy_start: "Enter a void date that is before the tenancy start date."
after_mrcdate: "Void date must be before the major repairs date if provided." after_mrcdate: "Void date must be before the major repairs date if provided."

123
spec/models/validations/date_validations_spec.rb

@ -21,22 +21,6 @@ RSpec.describe Validations::DateValidations do
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
it "validates that the tenancy start date is after the void date if it has a void date" do
record.startdate = Time.zone.local(2022, 1, 1)
record.voiddate = Time.zone.local(2022, 2, 1)
date_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.lettings.date.startdate.after_void_date"))
end
it "validates that the tenancy start date is after the major repair date if it has a major repair date" do
record.startdate = Time.zone.local(2022, 1, 1)
record.mrcdate = Time.zone.local(2022, 2, 1)
date_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.lettings.date.startdate.after_major_repair_date"))
end
it "produces no error when the tenancy start date is before the end date of the chosen scheme if it has an end date" do it "produces no error when the tenancy start date is before the end date of the chosen scheme if it has an end date" do
record.startdate = Time.zone.today - 30.days record.startdate = Time.zone.today - 30.days
record.scheme = scheme record.scheme = scheme
@ -59,6 +43,8 @@ RSpec.describe Validations::DateValidations do
date_validator.validate_property_major_repairs(record) date_validator.validate_property_major_repairs(record)
expect(record.errors["mrcdate"]) expect(record.errors["mrcdate"])
.to include(match I18n.t("validations.lettings.date.mrcdate.before_tenancy_start")) .to include(match I18n.t("validations.lettings.date.mrcdate.before_tenancy_start"))
expect(record.errors["startdate"])
.to include(match I18n.t("validations.lettings.date.startdate.after_major_repair_date"))
end end
it "must be before the tenancy start date" do it "must be before the tenancy start date" do
@ -68,23 +54,44 @@ RSpec.describe Validations::DateValidations do
expect(record.errors["mrcdate"]).to be_empty expect(record.errors["mrcdate"]).to be_empty
end end
it "cannot be more than 10 years before the tenancy start date" do context "with 2024 logs or earlier" do
record.startdate = Time.zone.local(2022, 2, 1) it "cannot be more than 10 years before the tenancy start date" do
record.mrcdate = Time.zone.local(2012, 1, 1) record.startdate = Time.zone.local(2024, 2, 1)
date_validator.validate_property_major_repairs(record) record.mrcdate = Time.zone.local(2014, 1, 31)
date_validator.validate_startdate(record) date_validator.validate_property_major_repairs(record)
expect(record.errors["mrcdate"]) expect(record.errors["mrcdate"])
.to include(match I18n.t("validations.lettings.date.mrcdate.ten_years_before_tenancy_start")) .to include(match I18n.t("validations.lettings.date.mrcdate.ten_years_before_tenancy_start"))
expect(record.errors["startdate"]) expect(record.errors["startdate"])
.to include(match I18n.t("validations.lettings.date.startdate.ten_years_after_mrc_date")) .to include(match I18n.t("validations.lettings.date.startdate.ten_years_after_mrc_date"))
end
it "must be within 10 years of the tenancy start date" do
record.startdate = Time.zone.local(2024, 2, 1)
record.mrcdate = Time.zone.local(2014, 2, 1)
date_validator.validate_property_major_repairs(record)
expect(record.errors["mrcdate"]).to be_empty
expect(record.errors["startdate"]).to be_empty
end
end end
it "must be within 10 years of the tenancy start date" do context "with 2025 logs or later" do
record.startdate = Time.zone.local(2022, 2, 1) it "cannot be more than 20 years before the tenancy start date" do
record.mrcdate = Time.zone.local(2012, 3, 1) record.startdate = Time.zone.local(2026, 2, 1)
date_validator.validate_property_major_repairs(record) record.mrcdate = Time.zone.local(2006, 1, 31)
expect(record.errors["mrcdate"]).to be_empty date_validator.validate_property_major_repairs(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["mrcdate"])
.to include(match I18n.t("validations.lettings.date.mrcdate.twenty_years_before_tenancy_start"))
expect(record.errors["startdate"])
.to include(match I18n.t("validations.lettings.date.startdate.twenty_years_after_mrc_date"))
end
it "must be within 20 years of the tenancy start date" do
record.startdate = Time.zone.local(2026, 2, 1)
record.mrcdate = Time.zone.local(2006, 2, 1)
date_validator.validate_property_major_repairs(record)
expect(record.errors["mrcdate"]).to be_empty
expect(record.errors["startdate"]).to be_empty
end
end end
context "when reason for vacancy is first let of property" do context "when reason for vacancy is first let of property" do
@ -130,6 +137,8 @@ RSpec.describe Validations::DateValidations do
date_validator.validate_property_void_date(record) date_validator.validate_property_void_date(record)
expect(record.errors["voiddate"]) expect(record.errors["voiddate"])
.to include(match I18n.t("validations.lettings.date.void_date.before_tenancy_start")) .to include(match I18n.t("validations.lettings.date.void_date.before_tenancy_start"))
expect(record.errors["startdate"])
.to include(match I18n.t("validations.lettings.date.startdate.after_void_date"))
end end
it "must be before the tenancy start date" do it "must be before the tenancy start date" do
@ -139,23 +148,45 @@ RSpec.describe Validations::DateValidations do
expect(record.errors["voiddate"]).to be_empty expect(record.errors["voiddate"]).to be_empty
end end
it "cannot be more than 10 years before the tenancy start date" do context "with 2024 logs or earlier" do
record.startdate = Time.zone.local(2022, 2, 1) it "cannot be more than 10 years before the tenancy start date" do
record.voiddate = Time.zone.local(2012, 1, 1) record.startdate = Time.zone.local(2024, 2, 1)
date_validator.validate_property_void_date(record) record.voiddate = Time.zone.local(2014, 1, 31)
date_validator.validate_startdate(record) date_validator.validate_property_void_date(record)
expect(record.errors["voiddate"]) expect(record.errors["voiddate"])
.to include(match I18n.t("validations.lettings.date.void_date.ten_years_before_tenancy_start")) .to include(match I18n.t("validations.lettings.date.void_date.ten_years_before_tenancy_start"))
expect(record.errors["startdate"]) expect(record.errors["startdate"])
.to include(match I18n.t("validations.lettings.date.startdate.ten_years_after_void_date")) .to include(match I18n.t("validations.lettings.date.startdate.ten_years_after_void_date"))
end
it "must be within 10 years of the tenancy start date" do
record.startdate = Time.zone.local(2024, 2, 1)
record.voiddate = Time.zone.local(2014, 2, 1)
date_validator.validate_property_void_date(record)
expect(record.errors["voiddate"]).to be_empty
expect(record.errors["startdate"]).to be_empty
end
end end
it "must be within 10 years of the tenancy start date" do context "with 2025 logs or later" do
record.startdate = Time.zone.local(2022, 2, 1) it "cannot be more than 20 years before the tenancy start date" do
record.voiddate = Time.zone.local(2012, 3, 1) record.startdate = Time.zone.local(2026, 2, 1)
date_validator.validate_property_void_date(record) record.voiddate = Time.zone.local(2006, 1, 31)
expect(record.errors["voiddate"]).to be_empty date_validator.validate_property_void_date(record)
expect(record.errors["startdate"]).to be_empty date_validator.validate_startdate(record)
expect(record.errors["voiddate"])
.to include(match I18n.t("validations.lettings.date.void_date.twenty_years_before_tenancy_start"))
expect(record.errors["startdate"])
.to include(match I18n.t("validations.lettings.date.startdate.twenty_years_after_void_date"))
end
it "must be within 20 years of the tenancy start date" do
record.startdate = Time.zone.local(2026, 2, 1)
record.voiddate = Time.zone.local(2006, 2, 1)
date_validator.validate_property_void_date(record)
expect(record.errors["voiddate"]).to be_empty
expect(record.errors["startdate"]).to be_empty
end
end end
context "when major repairs have been carried out" do context "when major repairs have been carried out" do

Loading…
Cancel
Save