Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added require keys method to raise if not defined #354

Merged
merged 1 commit into from
Sep 23, 2018
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,21 @@ SECRET_KEY=YOURSECRETKEYGOESHERE # comment
SECRET_HASH="something-with-a-#-hash"
```

### Required Keys

If a particular configuration value is required but not set, it's appropriate to raise an error.

To require configuration keys:

```ruby
# config/initializers/dotenv.rb

Dotenv.require_keys("SERVICE_APP_ID", "SERVICE_KEY", "SERVICE_SECRET")
```

If any of the configuration keys above are not set, your application will raise an error during initialization. This method is preferred because it prevents runtime errors in a production application due to improper configuration.


## Frequently Answered Questions

### Can I use dotenv in production?
Expand Down
7 changes: 7 additions & 0 deletions lib/dotenv.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "dotenv/parser"
require "dotenv/environment"
require "dotenv/missing_keys"

# The top level Dotenv module. The entrypoint for the application logic.
module Dotenv
Expand Down Expand Up @@ -55,6 +56,12 @@ def instrument(name, payload = {}, &block)
end
end

def require_keys(*keys)
missing_keys = keys.flatten - ::ENV.keys
return if missing_keys.empty?
raise MissingKeys, missing_keys
end

def ignoring_nonexistent_files
yield
rescue Errno::ENOENT
Expand Down
10 changes: 10 additions & 0 deletions lib/dotenv/missing_keys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Dotenv
class Error < StandardError; end

class MissingKeys < Error # :nodoc:
def initialize(keys)
key_word = "key#{keys.size > 1 ? 's' : ''}"
super("Missing required configuration #{key_word}: #{keys.inspect}")
end
end
end
14 changes: 14 additions & 0 deletions spec/dotenv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@
end
end

describe "require keys" do
let(:env_files) { [".env", fixture_path("bom.env")] }

before { Dotenv.load(*env_files) }

it "raises exception with not defined mandatory ENV keys" do
expect { Dotenv.require_keys("BOM", "TEST") }.to \
raise_error(
Dotenv::MissingKeys,
'Missing required configuration key: ["TEST"]'
)
end
end

describe "Unicode" do
subject { fixture_path("bom.env") }

Expand Down