Browse Source

replace range validation with multiple to account for single-ended ranges

pull/1253/head
Arthur Campbell 3 years ago
parent
commit
809c9a9bed
  1. 2
      app/models/validations/sales/household_validations.rb
  2. 22
      app/models/validations/shared_validations.rb
  3. 4
      config/locales/en.yml
  4. 4
      spec/models/validations/household_validations_spec.rb
  5. 16
      spec/models/validations/shared_validations_spec.rb
  6. 2
      spec/requests/lettings_logs_controller_spec.rb

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

@ -5,7 +5,7 @@ module Validations::Sales::HouseholdValidations
return if record.hholdcount.blank? return if record.hholdcount.blank?
unless record.hholdcount >= 0 && record.hholdcount <= 4 unless record.hholdcount >= 0 && record.hholdcount <= 4
record.errors.add :hholdcount, I18n.t("validations.numeric.valid", field: "Number of other people living in the property", min: 0, max: 4) record.errors.add :hholdcount, I18n.t("validations.numeric.within_range", field: "Number of other people living in the property", min: 0, max: 4)
end end
end end

22
app/models/validations/shared_validations.rb

@ -20,20 +20,16 @@ module Validations::SharedValidations
next unless question.min || question.max next unless question.min || question.max
next unless record[question.id] next unless record[question.id]
field = question.check_answer_label || question.id
min = [question.prefix, number_with_delimiter(question.min, delimiter: ","), question.suffix].join("")
max = [question.prefix, number_with_delimiter(question.max, delimiter: ","), question.suffix].join("")
begin begin
answer = Float(record.public_send("#{question.id}_before_type_cast")) answer = Float(record.public_send("#{question.id}_before_type_cast"))
rescue ArgumentError rescue ArgumentError
record.errors.add question.id.to_sym, I18n.t("validations.numeric.valid", field:, min:, max:) add_range_error(record, question)
end end
next unless answer next unless answer
if (question.min && question.min > answer) || (question.max && question.max < answer) if (question.min && question.min > answer) || (question.max && question.max < answer)
record.errors.add question.id.to_sym, I18n.t("validations.numeric.valid", field:, min:, max:) add_range_error(record, question)
end end
end end
end end
@ -108,4 +104,18 @@ private
def person_is_partner?(relationship) def person_is_partner?(relationship)
relationship == "P" relationship == "P"
end end
def add_range_error(record, question)
field = question.check_answer_label || question.id
min = [question.prefix, number_with_delimiter(question.min, delimiter: ","), question.suffix].join("") if question.min
max = [question.prefix, number_with_delimiter(question.max, delimiter: ","), question.suffix].join("") if question.max
if min && max
record.errors.add question.id.to_sym, I18n.t("validations.numeric.within_range", field:, min:, max:)
elsif min
record.errors.add question.id.to_sym, I18n.t("validations.numeric.above_min", field:, min:)
else
record.errors.add question.id.to_sym, I18n.t("validations.numeric.below_max", field:, max:)
end
end
end end

4
config/locales/en.yml

@ -131,7 +131,9 @@ en:
other_field_missing: "If %{main_field_label} is other then %{other_field_label} must be provided" other_field_missing: "If %{main_field_label} is other then %{other_field_label} must be provided"
other_field_not_required: "%{other_field_label} must not be provided if %{main_field_label} was not other" other_field_not_required: "%{other_field_label} must not be provided if %{main_field_label} was not other"
numeric: numeric:
valid: "%{field} must be between %{min} and %{max}" within_range: "%{field} must be between %{min} and %{max}"
above_min: "%{field} must be at least %{min}"
below_max: "%{field} must be at most %{max}"
date: date:
invalid_date: "Enter a date in the correct format, for example 31 1 2022" invalid_date: "Enter a date in the correct format, for example 31 1 2022"
outside_collection_window: "Enter a date within the current collection windows" outside_collection_window: "Enter a date within the current collection windows"

4
spec/models/validations/household_validations_spec.rb

@ -463,14 +463,14 @@ RSpec.describe Validations::HouseholdValidations do
record.hhmemb = -1 record.hhmemb = -1
household_validator.validate_numeric_min_max(record) household_validator.validate_numeric_min_max(record)
expect(record.errors["hhmemb"]) expect(record.errors["hhmemb"])
.to include(match I18n.t("validations.numeric.valid", field: "Number of Household Members", min: 0, max: 8)) .to include(match I18n.t("validations.numeric.within_range", field: "Number of Household Members", min: 0, max: 8))
end end
it "validates that the number of household members cannot be more than 8" do it "validates that the number of household members cannot be more than 8" do
record.hhmemb = 9 record.hhmemb = 9
household_validator.validate_numeric_min_max(record) household_validator.validate_numeric_min_max(record)
expect(record.errors["hhmemb"]) expect(record.errors["hhmemb"])
.to include(match I18n.t("validations.numeric.valid", field: "Number of Household Members", min: 0, max: 8)) .to include(match I18n.t("validations.numeric.within_range", field: "Number of Household Members", min: 0, max: 8))
end end
it "expects that the number of other household members is between the min and max" do it "expects that the number of other household members is between the min and max" do

16
spec/models/validations/shared_validations_spec.rb

@ -18,42 +18,42 @@ RSpec.describe Validations::SharedValidations do
record.age1 = "random" record.age1 = "random"
shared_validator.validate_numeric_min_max(record) shared_validator.validate_numeric_min_max(record)
expect(record.errors["age1"]) expect(record.errors["age1"])
.to include(match I18n.t("validations.numeric.valid", field: "Lead tenant’s age", min: 16, max: 120)) .to include(match I18n.t("validations.numeric.within_range", field: "Lead tenant’s age", min: 16, max: 120))
end end
it "validates that other household member ages are a number" do it "validates that other household member ages are a number" do
record.age2 = "random" record.age2 = "random"
shared_validator.validate_numeric_min_max(record) shared_validator.validate_numeric_min_max(record)
expect(record.errors["age2"]) expect(record.errors["age2"])
.to include(match I18n.t("validations.numeric.valid", field: "Person 2’s age", min: 1, max: 120)) .to include(match I18n.t("validations.numeric.within_range", field: "Person 2’s age", min: 1, max: 120))
end end
it "validates that person 1's age is greater than 16" do it "validates that person 1's age is greater than 16" do
record.age1 = 15 record.age1 = 15
shared_validator.validate_numeric_min_max(record) shared_validator.validate_numeric_min_max(record)
expect(record.errors["age1"]) expect(record.errors["age1"])
.to include(match I18n.t("validations.numeric.valid", field: "Lead tenant’s age", min: 16, max: 120)) .to include(match I18n.t("validations.numeric.within_range", field: "Lead tenant’s age", min: 16, max: 120))
end end
it "validates that other household member ages are greater than 1" do it "validates that other household member ages are greater than 1" do
record.age2 = 0 record.age2 = 0
shared_validator.validate_numeric_min_max(record) shared_validator.validate_numeric_min_max(record)
expect(record.errors["age2"]) expect(record.errors["age2"])
.to include(match I18n.t("validations.numeric.valid", field: "Person 2’s age", min: 1, max: 120)) .to include(match I18n.t("validations.numeric.within_range", field: "Person 2’s age", min: 1, max: 120))
end end
it "validates that person 1's age is less than 121" do it "validates that person 1's age is less than 121" do
record.age1 = 121 record.age1 = 121
shared_validator.validate_numeric_min_max(record) shared_validator.validate_numeric_min_max(record)
expect(record.errors["age1"]) expect(record.errors["age1"])
.to include(match I18n.t("validations.numeric.valid", field: "Lead tenant’s age", min: 16, max: 120)) .to include(match I18n.t("validations.numeric.within_range", field: "Lead tenant’s age", min: 16, max: 120))
end end
it "validates that other household member ages are greater than 121" do it "validates that other household member ages are greater than 121" do
record.age2 = 123 record.age2 = 123
shared_validator.validate_numeric_min_max(record) shared_validator.validate_numeric_min_max(record)
expect(record.errors["age2"]) expect(record.errors["age2"])
.to include(match I18n.t("validations.numeric.valid", field: "Person 2’s age", min: 1, max: 120)) .to include(match I18n.t("validations.numeric.within_range", field: "Person 2’s age", min: 1, max: 120))
end end
it "validates that person 1's age is between 16 and 120" do it "validates that person 1's age is between 16 and 120" do
@ -74,7 +74,7 @@ RSpec.describe Validations::SharedValidations do
sales_record.stairbought = "random" sales_record.stairbought = "random"
shared_validator.validate_numeric_min_max(sales_record) shared_validator.validate_numeric_min_max(sales_record)
expect(sales_record.errors["stairbought"]) expect(sales_record.errors["stairbought"])
.to include(match I18n.t("validations.numeric.valid", field: "Percentage bought in this staircasing transaction", min: "0 percent", max: "100 percent")) .to include(match I18n.t("validations.numeric.within_range", field: "Percentage bought in this staircasing transaction", min: "0 percent", max: "100 percent"))
end end
end end
@ -83,7 +83,7 @@ RSpec.describe Validations::SharedValidations do
sales_record.income1 = "random" sales_record.income1 = "random"
shared_validator.validate_numeric_min_max(sales_record) shared_validator.validate_numeric_min_max(sales_record)
expect(sales_record.errors["income1"]) expect(sales_record.errors["income1"])
.to include(match I18n.t("validations.numeric.valid", field: "Buyer 1’s gross annual income", min: "£0", max: "£999,999")) .to include(match I18n.t("validations.numeric.within_range", field: "Buyer 1’s gross annual income", min: "£0", max: "£999,999"))
end end
end end
end end

2
spec/requests/lettings_logs_controller_spec.rb

@ -82,7 +82,7 @@ RSpec.describe LettingsLogsController, type: :request do
it "validates lettings log parameters" do it "validates lettings log parameters" do
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
expect(json_response["errors"]).to match_array([["offered", [I18n.t("validations.property.offered.relet_number")]], ["age1", [I18n.t("validations.numeric.valid", field: "Lead tenant’s age", min: 16, max: 120)]]]) expect(json_response["errors"]).to match_array([["offered", [I18n.t("validations.property.offered.relet_number")]], ["age1", [I18n.t("validations.numeric.within_range", field: "Lead tenant’s age", min: 16, max: 120)]]])
end end
end end

Loading…
Cancel
Save