Skip to content

Commit

Permalink
Return config validation errors to front end
Browse files Browse the repository at this point in the history
  • Loading branch information
Nubbify committed Apr 13, 2023
1 parent 65f1217 commit c793205
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions app/models/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,11 @@ def validate_config

# run the validators and get a boolean, exit if all are true
# (see comment above in `clean_config_value` for an explainer)
return if validators.all? { |validator| method(validator).call }
validation_errors = validators.map { |validator| method(validator).call }
return if validation_errors.all? { |validation_error| validation_error.nil? }

errors.add(:invalid_value_for,
"#{config_key.humanize(capitalize: false)}: #{options.join(', ')}")
"#{config_key.humanize(capitalize: false)}: #{validation_errors.compact.join(', ')}")
end

# generic cleaner for words (so we have standardized capitalization)
Expand All @@ -267,12 +268,16 @@ def titleize_capitalization

# generic validator for numerics
def validate_number
options.last =~ /\A\d+\z/
if options.last !=~ /\A\d+\z/
'Must be number'
end
end

# validator for singletons (no lists allowed)
def validate_singleton
options.length == 1
if options.length != 1
'No lists allowed'
end
end

### URL fields
Expand All @@ -286,7 +291,7 @@ def validate_url
url = UriService.new(maybe_url).uri

# uriservice returns nil if there's a problem.
return false if !url
return false unless url


config_value['options'] = [url]
Expand All @@ -307,7 +312,9 @@ def validate_url
].freeze

def validate_start_of_week
START_OF_WEEK.include?(options.last.capitalize)
unless START_OF_WEEK.include?(options.last.capitalize)
"Must be a day of the week (or the word monthly)"
end
end

### Time zone
Expand All @@ -325,38 +332,45 @@ def validate_start_of_week
}.stringify_keys!

def validate_time_zone
TIME_ZONE.keys.include?(options.last.titleize)
unless TIME_ZONE.keys.include?(options.last.titleize)
"Timezone provided is not supported"
end
end

### Practical support

def validate_yes_or_no
# allow yes or no, to be nice (technically only yes is considered)
options.last =~ /\A(yes|no)\z/i
if !options.last =~ /\A(yes|no)\z/i
"Field must be either 'yes' or 'no'"
end
end

### Patient archive
ARCHIVE_MIN_DAYS = 60 # 2 months
ARCHIVE_MAX_DAYS = 550 # 1.5 years

def validate_patient_archive
validate_number && options.last.to_i.between?(ARCHIVE_MIN_DAYS, ARCHIVE_MAX_DAYS)
if !validate_number || !options.last.to_i.between?(ARCHIVE_MIN_DAYS, ARCHIVE_MAX_DAYS)
"Must be between #{ARCHIVE_MIN_DAYS} and #{ARCHIVE_MAX_DAYS} days."
end
end

### shared reset
SHARED_MIN_DAYS = 2 # 2 days
SHARED_MAX_DAYS = 7 * 6 # 6 weeks

def validate_shared_reset
validate_number && options.last.to_i.between?(SHARED_MIN_DAYS, SHARED_MAX_DAYS)
if !validate_number || !options.last.to_i.between?(SHARED_MIN_DAYS, SHARED_MAX_DAYS)
"Must be between #{SHARED_MIN_DAYS} and #{SHARED_MAX_DAYS} days."
end
end

def validate_length
total_length = 0
options.each do |option|
total_length += option.length
return false if total_length > 4000
end
true
"Length of provided values is too long (over 4000 characters)" if total_length > 4000
end
end

0 comments on commit c793205

Please sign in to comment.