Browse Source

Start date validation (#299)

* Start date validation

* Derive collection windows from form definitions
pull/302/head
baarkerlounger 3 years ago committed by GitHub
parent
commit
ac52864a03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/models/form.rb
  2. 19
      app/models/validations/date_validations.rb
  3. 4
      config/forms/2021_2022.json
  4. 4
      config/locales/en.yml
  5. 2
      spec/fixtures/forms/2021_2022.json
  6. 2
      spec/fixtures/forms/2022_2023.json
  7. 28
      spec/models/validations/date_validations_spec.rb

6
app/models/form.rb

@ -1,14 +1,14 @@
class Form class Form
attr_reader :form_definition, :sections, :subsections, :pages, :questions, attr_reader :form_definition, :sections, :subsections, :pages, :questions,
:start_year, :end_year, :type, :name :start_date, :end_date, :type, :name
def initialize(form_path, name) def initialize(form_path, name)
raise "No form definition file exists for given year".freeze unless File.exist?(form_path) raise "No form definition file exists for given year".freeze unless File.exist?(form_path)
@form_definition = JSON.parse(File.open(form_path).read) @form_definition = JSON.parse(File.open(form_path).read)
@name = name @name = name
@start_year = form_definition["start_year"] @start_date = form_definition["start_date"]
@end_year = form_definition["end_year"] @end_date = form_definition["end_date"]
@type = form_definition["form_type"] @type = form_definition["form_type"]
@sections = form_definition["sections"].map { |id, s| Form::Section.new(id, s, self) } @sections = form_definition["sections"].map { |id, s| Form::Section.new(id, s, self) }
@subsections = sections.flat_map(&:subsections) @subsections = sections.flat_map(&:subsections)

19
app/models/validations/date_validations.rb

@ -29,7 +29,11 @@ module Validations::DateValidations
end end
def validate_startdate(record) def validate_startdate(record)
date_valid?("startdate", record) return unless record.startdate && date_valid?("startdate", record)
if record.startdate < first_collection_start_date || record.startdate > second_collection_end_date
record.errors.add :startdate, I18n.t("validations.date.outside_collection_window")
end
end end
def validate_sale_completion_date(record) def validate_sale_completion_date(record)
@ -38,9 +42,20 @@ module Validations::DateValidations
private private
def first_collection_start_date
@first_collection_start_date ||= FormHandler.instance.forms.map { |form| form.second.start_date }.compact.min
end
def second_collection_end_date
@second_collection_end_date ||= FormHandler.instance.forms.map { |form| form.second.end_date }.compact.max
end
def date_valid?(question, record) def date_valid?(question, record)
if record[question].is_a?(ActiveSupport::TimeWithZone) && record[question].year.zero? if record[question].is_a?(ActiveSupport::TimeWithZone) && record[question].year.zero?
record.errors.add question, I18n.t("validations.date") record.errors.add question, I18n.t("validations.date.invalid_date")
false
else
true
end end
end end

4
config/forms/2021_2022.json

@ -1,7 +1,7 @@
{ {
"form_type": "lettings", "form_type": "lettings",
"start_year": 2021, "start_date": "2021-04-01T00:00:00.000+01:00",
"end_year": 2022, "end_date": "2022-07-01T00:00:00.000+01:00",
"sections": { "sections": {
"setup": { "setup": {
"label": "Before you start", "label": "Before you start",

4
config/locales/en.yml

@ -35,7 +35,9 @@ en:
updated: "Organisation details updated" updated: "Organisation details updated"
validations: validations:
date: "Please enter a valid date" date:
invalid_date: "Please enter a valid date"
outside_collection_window: "Date must be within the current collection windows"
postcode: "Enter a postcode in the correct format, for example AA1 1AA" postcode: "Enter a postcode in the correct format, for example AA1 1AA"
property: property:

2
spec/fixtures/forms/2021_2022.json vendored

@ -1,5 +1,7 @@
{ {
"form_type": "lettings", "form_type": "lettings",
"start_date": "2021-04-01T00:00:00.000+01:00",
"end_date": "2022-07-01T00:00:00.000+01:00",
"sections": { "sections": {
"household": { "household": {
"label": "About the household", "label": "About the household",

2
spec/fixtures/forms/2022_2023.json vendored

@ -1,5 +1,7 @@
{ {
"form_type": "lettings", "form_type": "lettings",
"start_date": "2022-04-01T00:00:00.000+01:00",
"end_date": "2023-07-01T00:00:00.000+01:00",
"sections": { "sections": {
"setup": { "setup": {
"label": "Before you start", "label": "Before you start",

28
spec/models/validations/date_validations_spec.rb

@ -0,0 +1,28 @@
require "rails_helper"
RSpec.describe Validations::DateValidations do
subject(:date_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::DateValidations } }
let(:record) { FactoryBot.create(:case_log) }
describe "tenancy start date" do
it "cannot be before the first collection window start date" do
record.startdate = Time.zone.local(2020, 1, 1)
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to include(match I18n.t("validations.date.outside_collection_window"))
end
it "cannot be after the second collection window end date" do
record.startdate = Time.zone.local(2023, 7, 1, 6)
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to include(match I18n.t("validations.date.outside_collection_window"))
end
it "must be a valid date" do
record.startdate = Time.zone.local(0, 7, 1)
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to include(match I18n.t("validations.date.invalid_date"))
end
end
end
Loading…
Cancel
Save