Last active
April 14, 2023 15:00
-
-
Save majames/1e93717dbba800e961847778734f8144 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# did they create this test data for you? or did you create it yourself? | |
ROWS_FROM_SHEET = [ | |
{ | |
label: 'Versed Roof', | |
format: 'string', | |
value: 'some value', | |
date: nil, | |
}, | |
{ | |
label: 'Dark Twist', | |
format: 'number', | |
value: 'eleven', | |
date: '2022-09-30', | |
}, | |
{ | |
label: 'Heavenly Song', | |
format: 'number', | |
value: 12.5, | |
date: nil, | |
}, | |
{ | |
label: 'Overtake View', | |
format: 'boolean', | |
value: 'sometimes', | |
date: '01/31/2021', | |
}, | |
{ | |
label: 'Nonchalant Amusement?', | |
format: 'boolean', | |
value: 'maybe', | |
date: nil, | |
}, | |
{ | |
label: 'Cars Quiet', | |
format: 'string', | |
value: 'none', | |
date: nil, | |
}, | |
{ | |
label: 'Promote Popcorn', | |
format: 'string', | |
value: 'thirty-five', | |
date: '2020-02-28', | |
}, | |
{ | |
label: 'Dynamic Observation', | |
format: 'number', | |
value: 239, | |
date: '12-2020', | |
}, | |
{ | |
label: 'Jump Kite?', | |
format: 'boolean', | |
value: 'true', | |
date: nil, | |
}, | |
{ | |
label: nil, | |
format: 'string', | |
value: 'pineapple', | |
date: nil, | |
}, | |
{ | |
label: 'Soap Icicle', | |
format: 'number', | |
value: 40900, | |
date: 'December 2023', | |
}, | |
{ | |
label: 'Painstaking Payment', | |
format: 'number', | |
value: -712.2, | |
date: nil, | |
}, | |
{ | |
label: 'Sugar Rabbit', | |
format: 'string', | |
value: 'symphony', | |
date: '2021-03-30', | |
}, | |
] | |
require 'date' | |
# think you can use Date.parse() instead here to simplify things | |
# def parse_date_with_formats(date_str) | |
# formats = ['%m/%d/%Y', '%Y-%m-%d'] | |
# formats.each do |format| | |
# begin | |
# return Date.strptime(date_str, format) | |
# rescue ArgumentError | |
# next | |
# end | |
# end | |
# raise ArgumentError.new("Invalid date format") | |
# end | |
# def is_valid_date?(date_str) | |
# begin | |
# date = parse_date_with_formats(date_str) | |
# last_day_of_month = Date.new(date.year, date.month, -1).day | |
# date.day == last_day_of_month | |
# rescue ArgumentError | |
# false | |
# end | |
# end | |
def is_valid_date?(date_str) | |
begin | |
date = Date.parse(date_str) | |
return date.end_of_month? | |
rescue ArgumentError | |
false | |
end | |
end | |
def validate_import_data(rows) | |
errors = [] | |
rows.each_with_index do |row, index| | |
# 1. Check if label, format, and value are present | |
# this will also add this error when :label, :format or :value are Nil -- is that what we want? | |
# if not, I think you can use respond_to() method instead --> row.respond_to(:label) | |
errors << "Row #{index + 1}: Missing label, format, or value" unless row[:label] && row[:format] && row[:value] | |
# 2. Check if row's format matches its value | |
case row[:format] | |
when 'string' | |
errors << "Row #{index + 1}: Value '#{row[:value]}' does not match format 'string'" unless row[:value].is_a?(String) | |
when 'number' | |
# think we could simplify this by using "row[:value].is_a?(Numeric)" | |
errors << "Row #{index + 1}: Value '#{row[:value]}' does not match format 'number'" unless row[:value].to_s.match(/^[-+]?\d+(\.\d+)?$/) | |
when 'boolean' | |
errors << "Row #{index + 1}: Value '#{row[:value]}' does not match format 'boolean'" unless ['true', 'false'].include?(row[:value].to_s.downcase) | |
else | |
# handle case when row[:format] is invalid | |
errors << "Row #{index + 1}: '#{row[:format]}' is NOT one of 'string', 'number' or 'boolean'" | |
end | |
# 3. Check if date is valid (if provided) | |
if row[:date] | |
errors << "Row #{index + 1}: Invalid date '#{row[:date]}'" unless is_valid_date?(row[:date]) | |
end | |
# 4. Check if label and value match boolean format requirements | |
if row[:format] == 'boolean' | |
errors << "Row #{index + 1}: Label '#{row[:label]}' must end with a question mark" unless row[:label].end_with?('?') | |
end | |
end | |
errors | |
end | |
errors = validate_import_data(ROWS_FROM_SHEET) | |
puts errors.join("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment