Browse Source

only put validation on the pages that are routed to

pull/1249/head
Kat 3 years ago
parent
commit
c4fe8d9688
  1. 2
      app/models/validations/shared_validations.rb
  2. 23
      spec/models/validations/shared_validations_spec.rb

2
app/models/validations/shared_validations.rb

@ -18,7 +18,7 @@ module Validations::SharedValidations
def validate_numeric_min_max(record) def validate_numeric_min_max(record)
record.form.numeric_questions.each do |question| record.form.numeric_questions.each do |question|
next unless question.min || question.max next unless question.min || question.max
next unless record[question.id] next unless record[question.id] && question.page.routed_to?(record, nil)
begin begin
answer = Float(record.public_send("#{question.id}_before_type_cast")) answer = Float(record.public_send("#{question.id}_before_type_cast"))

23
spec/models/validations/shared_validations_spec.rb

@ -5,7 +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(:sales_record) { FactoryBot.create(:sales_log, :completed) }
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 +67,25 @@ RSpec.describe Validations::SharedValidations do
shared_validator.validate_numeric_min_max(record) shared_validator.validate_numeric_min_max(record)
expect(record.errors["age6"]).to be_empty expect(record.errors["age6"]).to be_empty
end end
context "with sales log" do
it "validates that person 2's age is between 16 and 120 for non joint purchase" do
sales_record.jointpur = 2
sales_record.hholdcount = 1
sales_record.details_known_2 = 1
sales_record.jointmore = 1
sales_record.age2 = 130
shared_validator.validate_numeric_min_max(sales_record)
expect(sales_record.errors["age2"].first).to eq("Person 2’s age must be between 0 and 110")
end
it "validates that buyer 2's age is between 16 and 120 for joint purchase" do
sales_record.jointpur = 1
sales_record.age2 = 130
shared_validator.validate_numeric_min_max(sales_record)
expect(sales_record.errors["age2"].first).to eq("Buyer 2’s age must be between 0 and 110")
end
end
end end
it "adds the correct validation text when a question has a min but not a max" do it "adds the correct validation text when a question has a min but not a max" do
@ -77,6 +96,8 @@ RSpec.describe Validations::SharedValidations do
context "when validating percent" do context "when validating percent" do
it "validates that suffixes are added in the error message" do it "validates that suffixes are added in the error message" do
sales_record.ownershipsch = 1
sales_record.staircase = 1
sales_record.stairbought = 150 sales_record.stairbought = 150
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"])

Loading…
Cancel
Save