diff --git a/app/models/validations/shared_validations.rb b/app/models/validations/shared_validations.rb index da79599ee..7b0ecc420 100644 --- a/app/models/validations/shared_validations.rb +++ b/app/models/validations/shared_validations.rb @@ -54,14 +54,15 @@ module Validations::SharedValidations field = question.check_answer_label || question.id incorrect_accuracy = (value.to_d * 100) % (question.step * 100) != 0 - if question.step < 1 && incorrect_accuracy - record.errors.add question.id.to_sym, I18n.t("validations.shared.numeric.nearest_hundredth", field:) - elsif incorrect_accuracy || value.to_d != value.to_i # if the user enters a value in exponent notation (eg '4e1') the to_i method does not convert this to the correct value - field = question.check_answer_label || question.id - case question.step - when 1 then record.errors.add question.id.to_sym, :not_integer, message: I18n.t("validations.shared.numeric.whole_number", field:) - when 10 then record.errors.add question.id.to_sym, I18n.t("validations.shared.numeric.nearest_ten", field:) - end + next unless incorrect_accuracy + + case question.step + when 0.01 then record.errors.add question.id.to_sym, I18n.t("validations.shared.numeric.nearest_hundredth", field:) + when 0.1 then record.errors.add question.id.to_sym, I18n.t("validations.shared.numeric.nearest_tenth", field:) + when 1 then record.errors.add question.id.to_sym, :not_integer, message: I18n.t("validations.shared.numeric.whole_number", field:) + when 10 then record.errors.add question.id.to_sym, I18n.t("validations.shared.numeric.nearest_ten", field:) + else + record.errors.add question.id.to_sym, I18n.t("validations.shared.numeric.nearest_step", field:, step: question.step) end end end diff --git a/config/locales/validations/shared.en.yml b/config/locales/validations/shared.en.yml index a29d2bd74..ea06636dc 100644 --- a/config/locales/validations/shared.en.yml +++ b/config/locales/validations/shared.en.yml @@ -9,7 +9,9 @@ en: above_min: "%{field} must be at least %{min}." whole_number: "%{field} must be a whole number." nearest_ten: "%{field} must be given to the nearest ten." + nearest_tenth: "%{field} must be given to the nearest tenth." nearest_hundredth: "%{field} must be given to the nearest hundredth." + nearest_step: "${field} must be given to the nearest %{step}." normal_format: "Enter a number." format: "%{field} must be a number." diff --git a/spec/models/validations/shared_validations_spec.rb b/spec/models/validations/shared_validations_spec.rb index 53efc6675..a8475607c 100644 --- a/spec/models/validations/shared_validations_spec.rb +++ b/spec/models/validations/shared_validations_spec.rb @@ -123,12 +123,6 @@ RSpec.describe Validations::SharedValidations do expect(sales_log.errors[:income1]).to include I18n.t("validations.shared.numeric.whole_number", field: "Buyer 1’s gross annual income") end - it "adds an error if the user attempts to input a number in exponent format" do - sales_log.income1 = "3e5" - shared_validator.validate_numeric_step(sales_log) - expect(sales_log.errors[:income1]).to include I18n.t("validations.shared.numeric.whole_number", field: "Buyer 1’s gross annual income") - end - it "does not add an error if input is an integer" do sales_log.income1 = 30_000 shared_validator.validate_numeric_step(sales_log) @@ -144,13 +138,6 @@ RSpec.describe Validations::SharedValidations do expect(sales_log.errors[:savings]).to include I18n.t("validations.shared.numeric.nearest_ten", field: "Buyers’ total savings before any deposit paid") end - it "adds an error if the user attempts to input a number in exponent format" do - sales_log.savings = "3e5" - sales_log.jointpur = 1 - shared_validator.validate_numeric_step(sales_log) - expect(sales_log.errors[:savings]).to include I18n.t("validations.shared.numeric.nearest_ten", field: "Buyers’ total savings before any deposit paid") - end - it "does not add an error if input is a multiple of ten" do sales_log.savings = 30_000 shared_validator.validate_numeric_step(sales_log) @@ -165,12 +152,6 @@ RSpec.describe Validations::SharedValidations do expect(sales_log.errors[:mscharge]).to include I18n.t("validations.shared.numeric.nearest_hundredth", field: "Monthly leasehold charges") end - it "does not add an error if the user attempts to input a number in exponent format" do - sales_log.mscharge = "3e1" - shared_validator.validate_numeric_step(sales_log) - expect(sales_log.errors).to be_empty - end - it "does not add an error if input has 2 or fewer decimal places" do sales_log.mscharge = 30.74 shared_validator.validate_numeric_step(sales_log) @@ -229,6 +210,12 @@ RSpec.describe Validations::SharedValidations do expect(sales_log.errors[:income1]).to include I18n.t("validations.shared.numeric.format", field: "Buyer 1’s gross annual income") end + it "does not allow exponent format" do + sales_log.income1 = "1e4" + shared_validator.validate_numeric_input(sales_log) + expect(sales_log.errors[:income1]).to include I18n.t("validations.shared.numeric.format", field: "Buyer 1’s gross annual income") + end + it "allows a digit" do sales_log.income1 = "300" shared_validator.validate_numeric_input(sales_log)