Skip to content

Instantly share code, notes, and snippets.

@aereal
Last active December 8, 2017 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aereal/506fb70b33e37fd9d7b525f5ff2cecd9 to your computer and use it in GitHub Desktop.
Save aereal/506fb70b33e37fd9d7b525f5ff2cecd9 to your computer and use it in GitHub Desktop.
validates against environment variables
source 'https://rubygems.org'
gem 'json-schema'
#!/usr/bin/env ruby
require 'bundler/setup' unless defined?(Bundler)
require 'json'
require 'json-schema'
schema = JSON.parse(DATA.read)
begin
JSON::Validator.validate!(schema, ENV.to_h)
rescue JSON::Schema::ValidationError => error
warn error.message
exit 1
end
__END__
{
"title": "Schema of some script's environment variables",
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"properties": {
"DEBUG": {
"type": "string",
"enum": [
"1",
"0"
]
},
"LOGLEVEL": {
"type": "string",
"enum": [
"0", "1", "2", "3", "4", "5", "6", "7"
]
},
"PLACK_ENV": {
"type": "string",
"enum": [
"development", "test", "production"
]
},
"PATH": {
"type": "string",
"pattern": "[^:]+(:[^:]+)*"
}
},
"required": [
"PLACK_ENV"
]
}
@aereal
Copy link
Author

aereal commented Dec 8, 2017

✘╹◡╹✘ < ruby validates_env.rb
The property '#/' did not contain a required property of 'PLACK_ENV'
✘>﹏<✘ < PLACK_ENV=a ruby validates_env.rb
The property '#/PLACK_ENV' value "a" did not match one of the following values: development, test, production
✘>﹏<✘ < PLACK_ENV=development ruby validates_env.rb
✘╹◡╹✘ <

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment