Browse Source

feat: add validation that max col count not exceeded when no headers

pull/1557/head
Sam Seed 3 years ago
parent
commit
e778f41b44
  1. 9
      app/services/bulk_upload/lettings/validator.rb
  2. 9
      app/services/bulk_upload/lettings/year2023/csv_parser.rb
  3. 1
      config/locales/en.yml

9
app/services/bulk_upload/lettings/validator.rb

@ -10,6 +10,7 @@ class BulkUpload::Lettings::Validator
validate :validate_file_not_empty
validate :validate_field_numbers_count
validate :validate_max_columns_count_if_no_headers
def initialize(bulk_upload:, path:)
@bulk_upload = bulk_upload
@ -134,6 +135,14 @@ private
errors.add(:base, :wrong_field_numbers_count) if csv_parser.valid_field_numbers_count != csv_parser.class::FIELDS
end
def validate_max_columns_count_if_no_headers
return if halt_validations? || csv_parser.with_headers?
max_columns_count = body_rows.map(&:size).max - col_offset
errors.add(:base, :over_max_column_count) if max_columns_count > csv_parser.class::MAX_COLUMNS
end
def halt_validations!
@halt_validations = true
end

9
app/services/bulk_upload/lettings/year2023/csv_parser.rb

@ -2,6 +2,7 @@ require "csv"
class BulkUpload::Lettings::Year2023::CsvParser
FIELDS = 134
MAX_COLUMNS = 141
attr_reader :path
@ -50,6 +51,10 @@ class BulkUpload::Lettings::Year2023::CsvParser
field_numbers.count { |f| f != "field_blank" }
end
def with_headers?
rows.map { |r| r[0] }.any? { |cell| cell&.match?(/field number/i) }
end
private
def default_field_numbers
@ -64,10 +69,6 @@ private
end
end
def with_headers?
rows.map { |r| r[0] }.any? { |cell| cell&.match?(/field number/i) }
end
def row_sep
"\n"
end

1
config/locales/en.yml

@ -45,6 +45,7 @@ en:
attributes:
base:
wrong_field_numbers_count: "incorrect number of fields, please ensure you have used the correct template"
over_max_column_count: "too many columns, please ensure you have used the correct template"
forms/bulk_upload_lettings/year:
attributes:
year:

Loading…
Cancel
Save