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.2.0"

Class Method Summary collapse

Class Method Details

.analyze_variables[String]

It analyzes the current environment and it compares it to the documentation present in .env.sample.

Returns:

  • ([String], [String])

    An array with two arrays. First array: List of missing variables. Second array: List of variables with invalid format.



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
# 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=(.*)/

    valid =
      case Regexp.last_match(1)
      when "int", "integer" then integer?(value)
      when "float" then float?(value)
      when "str", "string" then true
      when "email" then email?(value)
      when "url" then url?(value)
      when "bool", "boolean" then boolean?(value)
      when "uuid" then uuid?(value)
      else
        value.match?(Regexp.new(Regexp.last_match(1)))
      end

    invalid_format << variable_name unless valid
  end

  [missing_variables, invalid_format]
end

.boolean?(string) ⇒ Boolean

It checks the value to check if it is a boolean or not.

Parameters:

  • A (String)

    string

Returns:

  • (Boolean)

    True if it is a boolean value. False otherwise.



122
123
124
# File 'lib/dotenv_validator.rb', line 122

def self.boolean?(string)
  string.match?(/(true|false)/)
end

.checkBoolean

It checks the current environment and it returns a boolean value.

Returns:

  • (Boolean)

    True if everything looks good. False otherwise.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dotenv_validator.rb', line 54

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

.check!Object

It checks the current environment and it raises a runtime exception.

Raises:

  • (RuntimeError)

    Raised if a missing variable is found or an invalid format is encountered.



74
75
76
77
78
79
80
# File 'lib/dotenv_validator.rb', line 74

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: #{invalid_format.join(", ")}") if invalid_format.any?
end

.email?(string) ⇒ Boolean

It checks the value to check if it is an email or not.

Parameters:

  • A (String)

    string

Returns:

  • (Boolean)

    True if it is an email value. False otherwise.



106
107
108
# File 'lib/dotenv_validator.rb', line 106

def self.email?(string)
  string.match?(URI::MailTo::EMAIL_REGEXP)
end

.float?(string) ⇒ Boolean

It checks the value to check if it is a float or not.

Parameters:

  • A (String)

    string

Returns:

  • (Boolean)

    True if it is a float value. False otherwise.



86
87
88
89
90
# File 'lib/dotenv_validator.rb', line 86

def self.float?(string)
  true if Float(string)
rescue
  false
end

.integer?(string) ⇒ Boolean

It checks the value to check if it is an integer or not.

Parameters:

  • A (String)

    string

Returns:

  • (Boolean)

    True if it is an integer value. False otherwise.



96
97
98
99
100
# File 'lib/dotenv_validator.rb', line 96

def self.integer?(string)
  true if Integer(string)
rescue
  false
end

.open_sample_fileObject



135
136
137
138
139
# File 'lib/dotenv_validator.rb', line 135

def self.open_sample_file
  File.open(sample_file)
rescue Errno::ENOENT
  raise DotenvValidator::SampleFileNotFoundError, "#{sample_file} was not found!"
end

.rootObject

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.



150
151
152
153
154
155
156
157
158
# File 'lib/dotenv_validator.rb', line 150

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_fileObject



141
142
143
# File 'lib/dotenv_validator.rb', line 141

def self.sample_file
  File.join(root, ".env.sample")
end

.url?(string) ⇒ Boolean

It checks the value to check if it is a URL or not.

Parameters:

  • A (String)

    string

Returns:

  • (Boolean)

    True if it is an URL value. False otherwise.



114
115
116
# File 'lib/dotenv_validator.rb', line 114

def self.url?(string)
  string.match?(/\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/)
end

.uuid?(string) ⇒ Boolean

It checks the value to check if it is a uuid or not.

Parameters:

  • A (String)

    string

Returns:

  • (Boolean)

    True if it is a UUID value. False otherwise.



130
131
132
133
# File 'lib/dotenv_validator.rb', line 130

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