From b60fbde4bb0e715e094e3664de14c3c9998296b5 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 23 Feb 2022 17:00:09 +0000 Subject: [PATCH] add more financial validations (#329) --- .../validations/financial_validations.rb | 9 +++++++ config/locales/en.yml | 1 + .../validations/financial_validations_spec.rb | 24 +++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 67ac5a38f..5a69b6a2f 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -40,6 +40,15 @@ module Validations::FinancialValidations end end + def validate_negative_currency(record) + t = %w[earnings brent scharge pscharge supcharg] + t.each do |x| + if record[x].present? && record[x].negative? + record.errors.add x.to_sym, I18n.t("validations.financial.negative_currency") + end + end + end + def validate_tshortfall(record) is_yes = record.hbrentshortfall == "Yes" hb_donotknow = record.hb == "Don’t know" diff --git a/config/locales/en.yml b/config/locales/en.yml index c67daa54b..0b4c9bbd2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -78,6 +78,7 @@ en: under_hard_min: "Net income cannot be less than %{hard_min} given the tenant’s working situation" freq_missing: "Select how often the household receives income" earnings_missing: "Enter how much income the household has in total" + negative_currency: "Enter an amount above 0" household: reasonpref: diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index a5be733de..f22a22fab 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -152,4 +152,28 @@ RSpec.describe Validations::FinancialValidations do end end end + + describe "financial validations" do + context "when currency is negative" do + it "returns error" do + record.earnings = -8 + record.brent = -2 + record.scharge = -134 + record.pscharge = -10_024 + record.supcharg = -1 + + financial_validator.validate_negative_currency(record) + expect(record.errors["earnings"]) + .to include(match I18n.t("validations.financial.negative_currency")) + expect(record.errors["brent"]) + .to include(match I18n.t("validations.financial.negative_currency")) + expect(record.errors["scharge"]) + .to include(match I18n.t("validations.financial.negative_currency")) + expect(record.errors["pscharge"]) + .to include(match I18n.t("validations.financial.negative_currency")) + expect(record.errors["supcharg"]) + .to include(match I18n.t("validations.financial.negative_currency")) + end + end + end end