Module: DotenvValidator
- Defined in:
- lib/dotenv_validator.rb,
lib/dotenv_validator/errors.rb,
lib/dotenv_validator/version.rb
Overview
:nodoc: all
Defined Under Namespace
Classes: SampleFileNotFoundError
Constant Summary collapse
- VERSION =
"1.3.0"
Class Method Summary collapse
-
.analyze_variables ⇒ [String]
It analyzes the current environment and it compares it to the documentation present in .env.sample.
-
.boolean?(string) ⇒ Boolean
It checks if the value is a boolean or not.
-
.check ⇒ Boolean
It checks the current environment and it returns a boolean value.
-
.check! ⇒ Object
It checks the current environment and it raises a runtime exception.
-
.email?(string) ⇒ Boolean
It checks if the value is an email or not.
-
.float?(string) ⇒ Boolean
It checks if the value is float or not.
-
.integer?(string) ⇒ Boolean
It checks if the value is an integer or not.
- .invalid_format_list(invalid_format) ⇒ Object
- .open_sample_file ⇒ Object
-
.root ⇒ Object
Internal:
Rails.rootis nil in Rails 4.1 before the application is initialized, so this falls back to theRAILS_ROOTenvironment variable, or the current working directory. - .sample_file ⇒ Object
-
.url?(string) ⇒ Boolean
It checks if the value is a URL or not.
-
.uuid?(string) ⇒ Boolean
It checks if the value is a uuid or not.
Class Method Details
.analyze_variables ⇒ [String]
It analyzes the current environment and it compares it to the documentation present in .env.sample.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/dotenv_validator.rb', line 14 def self.analyze_variables return [], [] unless File.exist?(sample_file) missing_variables = [] invalid_format = [] open_sample_file.each do |line| variable, config = line.split(" #") variable_name, _sample = variable.split("=") value = ENV[variable_name] if value.nil? || value.blank? missing_variables << variable_name if config.to_s.match?(/required/) next end next unless config =~ /format=(.*)/ format = Regexp.last_match(1) valid = case format when "int", "integer", "Integer" then integer?(value) when "float", "Float" then float?(value) when "str", "string", "String" then true when "email" then email?(value) when "url" then url?(value) when "bool", "boolean", "Boolean" then boolean?(value) when "uuid", "UUID" then uuid?(value) else value.match?(Regexp.new(Regexp.last_match(1))) end invalid_format << {name: variable_name, value: value, format: format} unless valid end [missing_variables, invalid_format] end |
.boolean?(string) ⇒ Boolean
It checks if the value is a boolean or not.
125 126 127 |
# File 'lib/dotenv_validator.rb', line 125 def self.boolean?(string) string.match?(/(true|false)/) end |
.check ⇒ Boolean
It checks the current environment and it returns a boolean value.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dotenv_validator.rb', line 56 def self.check result = true missing_variables, invalid_format = analyze_variables if missing_variables.any? puts("WARNING - Missing environment variables: #{missing_variables.join(", ")}") result = false end if invalid_format.any? puts "WARNING - Environment variables with invalid format:" puts invalid_format_list(invalid_format) result = false end result end |
.check! ⇒ Object
It checks the current environment and it raises a runtime exception.
77 78 79 80 81 82 83 |
# File 'lib/dotenv_validator.rb', line 77 def self.check! missing_variables, invalid_format = analyze_variables raise("Missing environment variables: #{missing_variables.join(", ")}") if missing_variables.any? raise("Environment variables with invalid format:\n#{invalid_format_list(invalid_format)}") if invalid_format.any? end |
.email?(string) ⇒ Boolean
It checks if the value is an email or not.
109 110 111 |
# File 'lib/dotenv_validator.rb', line 109 def self.email?(string) string.match?(URI::MailTo::EMAIL_REGEXP) end |
.float?(string) ⇒ Boolean
It checks if the value is float or not.
89 90 91 92 93 |
# File 'lib/dotenv_validator.rb', line 89 def self.float?(string) true if Float(string) rescue false end |
.integer?(string) ⇒ Boolean
It checks if the value is an integer or not.
99 100 101 102 103 |
# File 'lib/dotenv_validator.rb', line 99 def self.integer?(string) true if Integer(string) rescue false end |
.invalid_format_list(invalid_format) ⇒ Object
163 164 165 166 167 |
# File 'lib/dotenv_validator.rb', line 163 def self.invalid_format_list(invalid_format) invalid_format.map do |var| %(- #{var[:name]}: expected "#{var[:value]}" to match "#{var[:format]}" format) end.join("\n") end |
.open_sample_file ⇒ Object
138 139 140 141 142 |
# File 'lib/dotenv_validator.rb', line 138 def self.open_sample_file File.open(sample_file) rescue Errno::ENOENT raise DotenvValidator::SampleFileNotFoundError, "#{sample_file} was not found!" end |
.root ⇒ Object
Internal: Rails.root is nil in Rails 4.1 before the application is initialized, so this falls back to the RAILS_ROOT environment variable, or the current working directory.
Taken from Dotenv source code.
153 154 155 156 157 158 159 160 161 |
# File 'lib/dotenv_validator.rb', line 153 def self.root root_or_pwd = Pathname.new(ENV["RAILS_ROOT"] || Dir.pwd) if defined?(Rails) Rails.root || root_or_pwd else root_or_pwd end end |
.sample_file ⇒ Object
144 145 146 |
# File 'lib/dotenv_validator.rb', line 144 def self.sample_file File.join(root, ".env.sample") end |
.url?(string) ⇒ Boolean
It checks if the value is a URL or not.
117 118 119 |
# File 'lib/dotenv_validator.rb', line 117 def self.url?(string) string.match?(/\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/) end |
.uuid?(string) ⇒ Boolean
It checks if the value is a uuid or not.
133 134 135 136 |
# File 'lib/dotenv_validator.rb', line 133 def self.uuid?(string) string.match?(/\A[\da-f]{32}\z/i) || string.match?(/\A[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i) end |