Instantly share code, notes, and snippets.

@aereal /Gemfile
Last active Dec 8, 2017

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@aereal

aereal 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
✘╹◡╹✘ <
Owner

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