Browse Source

Add validations to all relevant fields

pull/2256/head
Kat 2 years ago
parent
commit
6c688a75bb
  1. 20
      app/models/validations/household_validations.rb
  2. 16
      app/models/validations/sales/household_validations.rb
  3. 4
      config/locales/en.yml
  4. 25
      spec/models/validations/household_validations_spec.rb
  5. 25
      spec/models/validations/sales/household_validations_spec.rb

20
app/models/validations/household_validations.rb

@ -121,25 +121,27 @@ module Validations::HouseholdValidations
if record.form.start_year_after_2024?
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student_2024", person_num:, person: "lead tenant")
else
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student")
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student") # 1
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.16_19_not_student")
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student")
end
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.16_19_not_student")
end
if !age_between_16_19 && student && child
if record.form.start_year_after_2024?
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.must_be_16_19_2024", person_num:, person: "lead tenant")
else
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.must_be_16_19") # 2
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.student_not_16_19")
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.must_be_16_19")
end
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.student_not_16_19")
end
if student && age_between_16_19 && !child && record.form.start_year_after_2024?
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.must_be_child_2024", person_num:, person: "lead tenant")
end
next unless student && age_between_16_19 && !child && record.form.start_year_after_2024?
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.must_be_child_2024", person_num:, person: "lead tenant")
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_not_child.cannot_be_16_19")
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.not_child_16_19.cannot_be_student")
end
end

16
app/models/validations/sales/household_validations.rb

@ -62,10 +62,10 @@ module Validations::Sales::HouseholdValidations
if record.form.start_year_after_2024?
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student_2024", person_num:, person: "buyer")
else
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student")
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.16_19_not_student")
end
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.16_19_not_student")
end
if !age_between_16_19 && student && child
@ -73,14 +73,16 @@ module Validations::Sales::HouseholdValidations
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.must_be_16_19_2024", person_num:, person: "buyer")
else
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.must_be_16_19")
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.student_not_16_19")
end
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.student_not_16_19")
end
if student && age_between_16_19 && !child && record.form.start_year_after_2024?
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.must_be_child_2024", person_num:, person: "buyer")
end
next unless student && age_between_16_19 && !child && record.form.start_year_after_2024?
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.must_be_child_2024", person_num:, person: "buyer")
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_not_child.cannot_be_16_19")
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.not_child_16_19.cannot_be_student")
end
end

4
config/locales/en.yml

@ -474,6 +474,8 @@ en:
lead:
over_25: "The lead tenant must be under 26 as you told us their housing situation immediately before this letting was a children’s home or foster care"
child_over_19_relat: "Age must be 19 or under as you told us person 2's relationship to the %{person} is child"
student_not_child:
cannot_be_16_19: "Person cannot be aged 16-19 if they are a student but not a child"
ecstat:
retired_over_70: "Person %{person_num} must be retired if over 70"
child_under_16: "Person %{person_num}’s working situation must be ‘child under 16’, ‘other’ or ‘prefers not to say’ as you told us they’re under 16"
@ -486,6 +488,8 @@ en:
must_be_student_2024: "Person %{person_num}'s working situation must be student or prefers not to say, as their age is 16-19 and their relationship to the %{person} is child"
retired_male: "Answer cannot be ‘retired’ as the male tenant is under 65"
retired_female: "Answer cannot be ‘retired’ as the female tenant is under 60"
not_child_16_19:
cannot_be_student: "Person cannot be a student if they are aged 16-19 but are not a child"
relat:
child_under_16_sales: "Answer cannot be ‘partner’ as you told us person %{person_num}'s age is under 16"
child_under_16_lettings: "Answer cannot be ‘partner’ as you told us person %{person_num}'s age is under 16"

25
spec/models/validations/household_validations_spec.rb

@ -545,19 +545,6 @@ RSpec.describe Validations::HouseholdValidations do
Singleton.__init__(FormHandler)
end
it "does not add 2023 errors" do
record.age2 = 17
record.relat2 = "C"
record.ecstat2 = 1
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["ecstat2"])
.not_to include(match I18n.t("validations.household.ecstat.student_16_19.must_be_student", person_num: 2))
expect(record.errors["age2"])
.not_to include(match I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student", person_num: 2))
expect(record.errors["relat2"])
.not_to include(match I18n.t("validations.household.relat.student_16_19.cannot_be_child.16_19_not_student", person_num: 2))
end
context "when the household contains a tenant’s child between the ages of 16 and 19" do
it "validates that person's economic status must be full time student or refused" do
record.age2 = 17
@ -567,9 +554,9 @@ RSpec.describe Validations::HouseholdValidations do
expect(record.errors["ecstat2"])
.to include("Person 2's working situation must be student or prefers not to say, as their age is 16-19 and their relationship to the lead tenant is child")
expect(record.errors["age2"])
.to be_empty
.to include("Person cannot be aged 16-19 if they have relationship ‘child’ but are not a student")
expect(record.errors["relat2"])
.to be_empty
.to include("Answer cannot be ‘child’ if the person is aged 16-19 but not a student")
end
it "expects that person can be a full time student" do
@ -609,11 +596,11 @@ RSpec.describe Validations::HouseholdValidations do
record.relat2 = "C"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"])
.to be_empty
.to include("Answer cannot be ‘child’ if the person is a student but not aged 16-19")
expect(record.errors["age2"])
.to include("Person 2's age must be 16-19 as their working situation is student and their relationship to the lead tenant is child")
expect(record.errors["ecstat2"])
.to be_empty
.to include("Person cannot be a student if they are not aged 16-19 but have relationship ‘child’")
end
it "adds errors for a person who is a student and aged 16-19 but not child" do
@ -624,9 +611,9 @@ RSpec.describe Validations::HouseholdValidations do
expect(record.errors["relat2"])
.to include("Person 2's relationship to the lead tenant must be child as their working situation is student and their age is 16-19")
expect(record.errors["age2"])
.to be_empty
.to include("Person cannot be aged 16-19 if they are a student but not a child")
expect(record.errors["ecstat2"])
.to be_empty
.to include("Person cannot be a student if they are aged 16-19 but are not a child")
end
end
end

25
spec/models/validations/sales/household_validations_spec.rb

@ -284,19 +284,6 @@ RSpec.describe Validations::Sales::HouseholdValidations do
context "with 2024 logs" do
let(:log_date) { Time.zone.local(2024, 4, 1) }
it "does not add 2023 errors" do
record.age2 = 17
record.relat2 = "C"
record.ecstat2 = 1
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["ecstat2"])
.not_to include(match I18n.t("validations.household.ecstat.student_16_19.must_be_student", person_num: 2))
expect(record.errors["age2"])
.not_to include(match I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student", person_num: 2))
expect(record.errors["relat2"])
.not_to include(match I18n.t("validations.household.relat.student_16_19.cannot_be_child.16_19_not_student", person_num: 2))
end
context "when the household contains a tenant’s child between the ages of 16 and 19" do
it "validates that person's economic status must be full time student or refused" do
record.age2 = 17
@ -306,9 +293,9 @@ RSpec.describe Validations::Sales::HouseholdValidations do
expect(record.errors["ecstat2"])
.to include("Person 2's working situation must be student or prefers not to say, as their age is 16-19 and their relationship to the buyer is child")
expect(record.errors["age2"])
.to be_empty
.to include("Person cannot be aged 16-19 if they have relationship ‘child’ but are not a student")
expect(record.errors["relat2"])
.to be_empty
.to include("Answer cannot be ‘child’ if the person is aged 16-19 but not a student")
end
it "expects that person can be a full time student" do
@ -348,11 +335,11 @@ RSpec.describe Validations::Sales::HouseholdValidations do
record.relat2 = "C"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"])
.to be_empty
.to include("Answer cannot be ‘child’ if the person is a student but not aged 16-19")
expect(record.errors["age2"])
.to include("Person 2's age must be 16-19 as their working situation is student and their relationship to the buyer is child")
expect(record.errors["ecstat2"])
.to be_empty
.to include("Person cannot be a student if they are not aged 16-19 but have relationship ‘child’")
end
it "adds errors for a person who is a student and aged 16-19 but not child" do
@ -363,9 +350,9 @@ RSpec.describe Validations::Sales::HouseholdValidations do
expect(record.errors["relat2"])
.to include("Person 2's relationship to the buyer must be child as their working situation is student and their age is 16-19")
expect(record.errors["age2"])
.to be_empty
.to include("Person cannot be aged 16-19 if they are a student but not a child")
expect(record.errors["ecstat2"])
.to be_empty
.to include("Person cannot be a student if they are aged 16-19 but are not a child")
end
end
end

Loading…
Cancel
Save