env_check is a lightweight Ruby gem to validate and document your environment variables.
It ensures critical env vars are present and properly formatted before your app boots or deploys.
- ✅ Validate required environment variables
⚠️ Warn on missing or invalid optional variables- 🔒 Type checking (boolean, integer, URL, email)
- 📁 YAML-based configuration
- 🔧 Works with any Ruby or Rails app
- 🌱 Supports
.envfile viadotenv(optional) - 🎯 Programmatic API for custom integrations
- 📊 Detailed validation results
Add this line to your application's Gemfile:
gem 'env_check'And then execute:
$ bundle install
- Ruby 3.0+ - Fully supported
- Ruby 3.1+ - Fully supported
- Ruby 3.2+ - Fully supported
- Ruby 3.3+ - Fully supported
- Ruby 3.4+ - Fully supported
This gem is framework-agnostic and works with any Ruby application. For Rails applications, it supports:
| Rails Version | Ruby Version | Support Status |
|---|---|---|
| Rails 8.0+ | Ruby 3.2+ | ✅ Supported |
| Rails 7.2+ | Ruby 3.1+ | ✅ Supported |
| Rails 7.1+ | Ruby 3.1+ | ✅ Supported |
| Rails 7.0+ | Ruby 2.7+ | ✅ Supported |
| Rails 6.1+ | Ruby 2.5+ | ✅ Supported |
| Rails 6.0+ | Ruby 2.5+ | ✅ Supported |
| Rails 5.x | Ruby 2.2+ |
Note: While the gem doesn't directly depend on Rails, it's been designed to work seamlessly in Rails environments and integrates well with Rails' environment variable patterns.
Or install manually:
gem install env_checkenv_check initThis creates a configuration file using smart defaults:
- Simple projects:
.env_check.yml(root level, pairs with.env) - Rails projects:
config/env_check.yml(Rails convention)
Example configuration:
# EnvCheck Configuration
# Required environment variables (must be present and non-empty)
required:
- DATABASE_URL
- SECRET_KEY_BASE
# Optional environment variables with type validation
optional:
DEBUG: boolean # true, false, 1, 0, yes, no
PORT: integer # numeric values only
API_URL: url # must start with http:// or https://
ADMIN_EMAIL: email # valid email formatenv_check checkOutput:
✅ DATABASE_URL is set
✅ SECRET_KEY_BASE is set
⚠️ DEBUG should be a boolean, got 'maybe'
❌ Missing required ENV: API_KEY
# Create configuration file
env_check init
# Validate environment variables
env_check check
# Show version
env_check versionrequire 'env_check'
# Basic validation (returns Result object)
result = EnvCheck.verify
if result.success?
puts "✅ All environment variables are valid!"
puts "Valid vars: #{result.valid_vars.join(', ')}"
else
puts "❌ Validation failed!"
puts "Errors: #{result.errors.join(', ')}"
puts "Warnings: #{result.warnings.join(', ')}"
end
# Validate with custom config file
result = EnvCheck.verify("path/to/custom.yml")
# Validate with inline configuration
result = EnvCheck.verify_with_config({
"required" => ["API_KEY", "DATABASE_URL"],
"optional" => { "DEBUG" => "boolean" }
})
# Legacy method (raises exception on failure)
begin
EnvCheck.verify!
puts "✅ Validation passed!"
rescue EnvCheck::Error => e
puts "❌ #{e.message}"
exit 1
endAdd to your config/application.rb or initializer:
# config/initializers/env_check.rb
EnvCheck.verify! if Rails.env.production?1. Environment-Specific Validation
# config/initializers/env_check.rb
Rails.application.config.after_initialize do
# Load Rails-specific config
config_file = Rails.root.join('config', 'env_check.yml')
if config_file.exist?
result = EnvCheck.verify(config_file)
unless result.success?
Rails.logger.error "Environment validation failed: #{result.errors.join(', ')}"
# Only raise in production
raise EnvCheck::Error, result.errors.join(', ') if Rails.env.production?
end
end
end2. Controller Integration
class ApplicationController < ActionController::Base
before_action :validate_critical_env_vars, if: -> { Rails.env.production? }
private
def validate_critical_env_vars
result = EnvCheck.verify_with_config({
"required" => ["DATABASE_URL", "SECRET_KEY_BASE", "RAILS_MASTER_KEY"]
})
unless result.success?
render json: { error: 'Service temporarily unavailable' }, status: 503
end
end
end3. Health Check Endpoint
# config/routes.rb
Rails.application.routes.draw do
get '/health/env', to: 'health#env_check'
end
# app/controllers/health_controller.rb
class HealthController < ApplicationController
def env_check
result = EnvCheck.verify
if result.success?
render json: {
status: 'ok',
valid_vars: result.valid_vars.count,
warnings: result.warnings
}
else
render json: {
status: 'error',
errors: result.errors
}, status: 422
end
end
end4. Environment-Specific Configuration
Create your config file (.env_check.yml or config/env_check.yml):
default: &default
optional:
DEBUG: boolean
LOG_LEVEL: string
development:
<<: *default
required:
- DATABASE_URL
- SECRET_KEY_BASE
test:
<<: *default
required:
- SECRET_KEY_BASE
optional:
DATABASE_URL: url
production:
<<: *default
required:
- DATABASE_URL
- SECRET_KEY_BASE
- RAILS_MASTER_KEY
- REDIS_URL
optional:
MEMCACHED_URL: url
CDN_HOST: url
SMTP_HOST: string# In your Rakefile or Rails app
require 'env_check'
# Use the built-in rake task
rake env:checkboolean:true,false,1,0,yes,no(case-insensitive)integer: Positive or negative integers (123,-456)url: URLs starting withhttp://orhttps://email: Valid email addressesstring: Any value (default, no validation)
# config/env_check.yml
required:
- DATABASE_URL
- SECRET_KEY_BASE
- API_KEY
optional:
# Type validations
DEBUG: boolean
PORT: integer
API_URL: url
ADMIN_EMAIL: email
# Environment-specific
RAILS_ENV: string
RACK_ENV: stringEnvCheck automatically discovers your configuration file using this priority:
.env_check.yml(root level - simple projects, pairs with.env)config/env_check.yml(Rails convention - ifconfig/directory exists)- Custom path via
--configflag orENV_CHECK_CONFIGenvironment variable
# Auto-discovery (checks .env_check.yml, then config/env_check.yml)
env_check check
# Explicit path
env_check check --config custom/path.yml
# Via environment variable
ENV_CHECK_CONFIG=custom/path.yml env_check check
# Via Ruby API
EnvCheck.verify("custom/path.yml")Run the test suite:
bundle exec rspecRun linting:
bundle exec rubocop- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Please make sure to update tests and run the linter before submitting.
The gem is available as open source under the terms of the MIT License.
See CHANGELOG.md for version history and changes.