Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions lib/config_validator.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
class ConfigValidator
ENV_PREFIX = Figaro::Application::FIGARO_ENV_PREFIX

def validate(env = ENV)
bad_keys = keys_with_bad_values(env, candidate_keys(env))
return unless bad_keys.any?
raise warning(bad_keys).tr("\\\n", ' ')
raise warning(bad_keys).tr("\n", ' ')
end

private

def candidate_keys(env)
env.keys.delete_if { |key| key.starts_with?(Figaro::Application::FIGARO_ENV_PREFIX) }
env.keys.keep_if { |key| candidate_key?(env, key) }
end

def candidate_key?(env, key)
# A key is associated with a configuration setting if there are two
# settings in the environment: one with and without the Figaro prefix.
# We're only interested in the configuration settings and not other
# environment variables.

env.include?(key) and env.include?(ENV_PREFIX + key)
end

def keys_with_bad_values(env, keys)
keys.keep_if { |key| %w[yes no].include?(env[key]) }
# Configuration settings for boolean values need to be "true/false"
# and not "yes/no".

keys.keep_if { |key| %w[yes no].include?(env[key].strip.downcase) }
end

def warning(bad_keys)
"You have invalid values for #{bad_keys.uniq.to_sentence} in " \
"config/application.yml. Please change them to true or false."
"You have invalid values (yes/no) for #{bad_keys.uniq.to_sentence} " \
"in config/application.yml or your environment. " \
"Please change them to true or false."
end
end
19 changes: 17 additions & 2 deletions spec/lib/config_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,34 @@
it 'raises if one or more candidate key values is set to yes or no' do
bad_key = 'bad_key'
other_bad_key = 'other_bad_key'
noncandidate_key = '_FIGARO_KEY'
up_bad_key = 'up_bad_key'
cap_bad_key = 'cap_bad_key'
noncandidate_key = 'KEY'
good_key = 'good_key'

env = {
bad_key => 'yes',
other_bad_key => 'no',
up_bad_key => 'YES ',
cap_bad_key => ' No',
noncandidate_key => 'yes',
good_key => 'foo',
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to test the stripping and downcasing, can you please add some more candidate keys, such as " YES" and "NO "?


# Figaro sets 2 environment variables for each configuration:
# 1 with and 1 without the Figaro prefix. Settings that don't
# have both are not part of the configuration. This mimics
# Figaro by adding the settings with the prefix.

[bad_key, other_bad_key, up_bad_key, cap_bad_key, good_key].each do |key|
env[ConfigValidator::ENV_PREFIX + key] = env[key]
end

list = "#{bad_key}, #{other_bad_key}, #{up_bad_key}, and #{cap_bad_key}"

expect { ConfigValidator.new.validate(env) }.to raise_error(
RuntimeError,
/You have invalid values for #{bad_key} and #{other_bad_key}/
%r{You have invalid values \(yes\/no\) for #{list}}
)
end
end
Expand Down