Browse Source

Change numeric validation to display prefixes in error message (#1227)

* change numeric validation

* Fix test, add delimiter and suffix to the numeric error mesage
pull/1234/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
8ca9cbfb49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/models/form/sales/questions/deposit_amount.rb
  2. 8
      app/models/validations/sales/sale_information_validations.rb
  3. 8
      app/models/validations/shared_validations.rb
  4. 2
      spec/models/form/sales/questions/deposit_amount_spec.rb
  5. 42
      spec/models/validations/sales/sale_information_validations_spec.rb
  6. 19
      spec/models/validations/shared_validations_spec.rb

2
app/models/form/sales/questions/deposit_amount.rb

@ -7,7 +7,7 @@ class Form::Sales::Questions::DepositAmount < ::Form::Question
@type = "numeric" @type = "numeric"
@min = 0 @min = 0
@width = 5 @width = 5
@max = 9_999_999 @max = 999_999
@prefix = "£" @prefix = "£"
@hint_text = "Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage" @hint_text = "Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage"
@derived = true @derived = true

8
app/models/validations/sales/sale_information_validations.rb

@ -1,12 +1,4 @@
module Validations::Sales::SaleInformationValidations module Validations::Sales::SaleInformationValidations
def validate_deposit_range(record)
return if record.deposit.blank?
unless record.deposit >= 0 && record.deposit <= 999_999
record.errors.add :deposit, "Cash deposit must be £0 - £999,999"
end
end
def validate_pratical_completion_date_before_saledate(record) def validate_pratical_completion_date_before_saledate(record)
return if record.saledate.blank? || record.hodate.blank? return if record.saledate.blank? || record.hodate.blank?

8
app/models/validations/shared_validations.rb

@ -1,4 +1,6 @@
module Validations::SharedValidations module Validations::SharedValidations
include ActionView::Helpers::NumberHelper
def validate_other_field(record, value_other = nil, main_field = nil, other_field = nil, main_label = nil, other_label = nil) def validate_other_field(record, value_other = nil, main_field = nil, other_field = nil, main_label = nil, other_label = nil)
return unless main_field || other_field return unless main_field || other_field
@ -19,17 +21,19 @@ module Validations::SharedValidations
next unless record[question.id] next unless record[question.id]
field = question.check_answer_label || 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: question.min, max: question.max) record.errors.add question.id.to_sym, I18n.t("validations.numeric.valid", field:, min:, max:)
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: question.min, max: question.max) record.errors.add question.id.to_sym, I18n.t("validations.numeric.valid", field:, min:, max:)
end end
end end
end end

2
spec/models/form/sales/questions/deposit_amount_spec.rb

@ -48,6 +48,6 @@ RSpec.describe Form::Sales::Questions::DepositAmount, type: :model do
end end
it "has correct max" do it "has correct max" do
expect(question.max).to eq(9_999_999) expect(question.max).to eq(999_999)
end end
end end

42
spec/models/validations/sales/sale_information_validations_spec.rb

@ -5,48 +5,6 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
let(:validator_class) { Class.new { include Validations::Sales::SaleInformationValidations } } let(:validator_class) { Class.new { include Validations::Sales::SaleInformationValidations } }
describe "#validate_deposit_range" do
context "when within permitted bounds" do
let(:record) { build(:sales_log, deposit: 0) }
it "does not add an error" do
sale_information_validator.validate_deposit_range(record)
expect(record.errors[:deposit]).not_to be_present
end
end
context "when blank" do
let(:record) { build(:sales_log, deposit: nil) }
it "does not add an error" do
sale_information_validator.validate_deposit_range(record)
expect(record.errors[:deposit]).not_to be_present
end
end
context "when below lower bound" do
let(:record) { build(:sales_log, deposit: -1) }
it "adds an error" do
sale_information_validator.validate_deposit_range(record)
expect(record.errors[:deposit]).to be_present
end
end
context "when higher than upper bound" do
let(:record) { build(:sales_log, deposit: 1_000_000) }
it "adds an error" do
sale_information_validator.validate_deposit_range(record)
expect(record.errors[:deposit]).to be_present
end
end
end
describe "#validate_pratical_completion_date_before_saledate" do describe "#validate_pratical_completion_date_before_saledate" do
context "when hodate blank" do context "when hodate blank" do
let(:record) { build(:sales_log, hodate: nil) } let(:record) { build(:sales_log, hodate: nil) }

19
spec/models/validations/shared_validations_spec.rb

@ -5,6 +5,7 @@ RSpec.describe Validations::SharedValidations do
let(:validator_class) { Class.new { include Validations::SharedValidations } } let(:validator_class) { Class.new { include Validations::SharedValidations } }
let(:record) { FactoryBot.create(:lettings_log) } let(:record) { FactoryBot.create(:lettings_log) }
let(:sales_record) { FactoryBot.create(:sales_log) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
describe "numeric min max validations" do describe "numeric min max validations" do
@ -67,6 +68,24 @@ RSpec.describe Validations::SharedValidations do
expect(record.errors["age6"]).to be_empty expect(record.errors["age6"]).to be_empty
end end
end end
context "when validating percent" do
it "validates that % suffix is added in the error message" do
sales_record.stairbought = "random"
shared_validator.validate_numeric_min_max(sales_record)
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"))
end
end
context "when validating price" do
it "validates that £ prefix and , is added in the error message" do
sales_record.income1 = "random"
shared_validator.validate_numeric_min_max(sales_record)
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"))
end
end
end end
describe "radio options validations" do describe "radio options validations" do

Loading…
Cancel
Save