config_validator v0.1.1 ConfigValidator
Configuration validation for Mix config files.
Why
When you set a Mix configuration value equal to an environment variable, that variable must be present at compile time or in your deployment environment for your app to function properly.
It’s easy to forget to do this, causing unecessary bugs. ConfigValidator gives you the tools to prevent that from ever happening again.
How
ConfigValidator reads your config/config.exs
file (or other config file of
your choosing), and looks for nil settings.
It is smart enough to understand both of the following ways of referencing an ENV variable:
System.get_env("VAR_NAME")
{:system, "VAR_NAME"}
If a nil setting is found, ConfigValidator
can raise an error at compile
time, start up, or just log a warning. It’s up to you.
See the following macros and functions for more information:
Link to this section Summary
Functions
Validates a project’s config file, looking for nil settings
Validates a project config file, looking for nil settings. If a nil setting
is found, will raise ConfigValidator.ConfigError
Validates a project config file, similar to validate_config!/1
, but at
compile time.
Link to this section Functions
validate_config(String.t) :: :ok | {:error, ConfigValidator.ConfigError.t}
Validates a project’s config file, looking for nil settings.
Examples
You can validate your default config at config/config.exs
:
iex> validate_config()
:ok
Or a config file at a custom location:
iex> validate_config("test/support/valid_config.exs")
:ok
iex> validate_config("test/support/nil_config.exs")
{:error, %ConfigValidator.ConfigError{
message: "Some app configuration values are nil! [config_validator: [setting: nil]]",
apps: [config_validator: [setting: nil]]
}}
Validates a project config file, looking for nil settings. If a nil setting
is found, will raise ConfigValidator.ConfigError
.
Examples
You can validate your default config at config/config.exs
:
iex> validate_config!()
:ok
Or a config file at a custom location:
iex> validate_config!("test/support/valid_config.exs")
:ok
iex> validate_config!("test/support/nil_config.exs")
** (ConfigValidator.ConfigError) Some app configuration values are nil! [config_validator: [setting: nil]]
Validates a project config file, similar to validate_config!/1
, but at
compile time.
This is useful to prevent code from being deployed if any of the required settings or ENV variables are nil in the deployment environment. The code will refuse to compile.
If your compile environment is different from your runtime environment, or
if there’s a risk that ENV variables might go missing at runtime, you’ll also
want to use validate_config/1
or validate_config!/1
.
defmodule My.Application do
import ConfigValidator
def start(_) do
# Validate that all required ENV vars and settings are present at
# compile time:
validate_config_at_compile_time!
# Validate that all required ENV vars and settings are still present
# after compile, when the application boots
validate_config!
end
end
Examples
The behaviour is the same as validate_config!/1
, just at compile time.