Class: Dotenv::Schema

Inherits:
Hash
  • Object
show all
Defined in:
lib/dotenv/schema.rb

Defined Under Namespace

Classes: ValidationError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Schema

Returns a new instance of Schema.



8
9
10
# File 'lib/dotenv/schema.rb', line 8

def initialize(hash={})
  replace hash if hash
end

Class Method Details

.load(file) ⇒ Object



12
13
14
# File 'lib/dotenv/schema.rb', line 12

def self.load(file)
  new YAML.load(File.read(file))
end

Instance Method Details

#validate!(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dotenv/schema.rb', line 16

def validate!(env)
  undefined_keys = env.keys - keys
  unless undefined_keys.empty?
    raise ValidationError, "Undefined variable(s): #{undefined_keys.join(', ')}; Please add them into #{Dotenv.schema_path}"
  end
  each do |key, options|
    if env[key] == '' && (options && !options['allow_empty_string'] || !options)
      raise ValidationError, "ENV['#{key}'] must not be empty string"
    end
    if !env.has_key?(key) && (options && !options['allow_not_exists'] || !options)
      raise ValidationError, "ENV['#{key}'] must exist"
    end
  end
  true
end