I’m trying to configure a pair of related settings in my application with somewhat complicated interaction.
Application.boot(:settings, from: :system) do
settings do
key :option_a, Types::Strict::String.optional
key :option_b, Types::Params.Bool.default(false)
end
end
Either or both of OPTION_A
and OPTION_B
can be specified. If neither is specified, though, the application won’t work well. In a development environment I can provide a reasonable default, but it’s not a reasonable default in production. I’d sort of like to say something like
after(:start) do
settings = target[:settings]
if settings.option_a.nil? && !settings.option_b
if env == :production
# Is there a more idiomatic way to complain?
raise 'either OPTION_A or OPTION_B must be specified'
else
# Can't overwrite the value in the dry-container
target[:settings] = settings.new(option_b: true)
end
end
end
Any advice on the best way to do this sort of conditional, cross-option, environment-dependent option validation?