module DotenvValidator
Knows how to check the environment variables and compares it to .env.sample and the comments in every line of your .env.sample file.
Constants
- VERSION
Public Class Methods
It analyzes the current environment and it compares it to the documentation present in .env.sample.
@return [[String],] An array with two arrays. First array: List of missing variables. Second array: List of variables with invalid format.
# File lib/dotenv_validator.rb, line 11 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 if config =~ /format=(.*)/ valid = case $1 when "int", "integer" then integer?(value) when "float" then float?(value) when "str", "string" then false when "email" then email?(value) when "url" then url?(value) else value.match?(Regexp.new($1)) end invalid_format << variable_name unless valid end end [missing_variables, invalid_format] end
It checks the current environment and it returns a boolean value.
@return [Boolean] True if everything looks good. False otherwise.
# File lib/dotenv_validator.rb, line 49 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: #{invalid_format.join(", ")}") result = false end result end
It checks the current environment and it raises a runtime exception.
@raise [RuntimeError] Raised if a missing variable is found or an invalid format is encountered.
# File lib/dotenv_validator.rb, line 69 def self.check! missing_variables, invalid_format = analyze_variables if missing_variables.any? raise("Missing environment variables: #{missing_variables.join(", ")}") end if invalid_format.any? raise("Environment variables with invalid format: #{invalid_format.join(", ")}") end end
It checks the value to check if it is an email or not.
@param [String] A string @return [Boolean] True if it is an email value. False otherwise.
# File lib/dotenv_validator.rb, line 105 def self.email?(string) string.match?(/[\w@]+@[\w@]+\.[\w@]+/) end
It checks the value to check if it is a float or not.
@param [String] A string @return [Boolean] True if it is a float value. False otherwise.
# File lib/dotenv_validator.rb, line 85 def self.float?(string) true if Float(string) rescue false end
It checks the value to check if it is an integer or not.
@param [String] A string @return [Boolean] True if it is an integer value. False otherwise.
# File lib/dotenv_validator.rb, line 95 def self.integer?(string) true if Integer(string) rescue false end
# File lib/dotenv_validator.rb, line 117 def self.open_sample_file File.open(sample_file) end
# File lib/dotenv_validator.rb, line 121 def self.sample_file File.join(File.expand_path(File.dirname(__FILE__)), ".env.sample") end
It checks the value to check if it is a URL or not.
@param [String] A string @return [Boolean] True if it is an URL value. False otherwise.
# File lib/dotenv_validator.rb, line 113 def self.url?(string) string.match?(/https?:\/\/.+/) end