Confispex.Schema behaviour (confispex v1.1.0)

Example

defmodule MyApp.RuntimeConfigSchema do
  import Confispex.Schema
  @behaviour Confispex.Schema
  alias Confispex.Type

  defvariables(%{
    "RUNTIME_CONFIG_REPORT" => %{
      cast: {Type.Enum, values: ["disabled", "detailed", "brief"]},
      default: "disabled",
      groups: [:misc]
    },
    "TZDATA_AUTOUPDATE_ENABLED" => %{
      doc: "Autoupdate timezones from IANA Time Zone Database",
      cast: Type.Boolean,
      default: "false",
      groups: [:base],
      context: [env: [:dev, :prod]]
    },
    "LOG_LEVEL" => %{
      cast:
        {Type.Enum,
         values: [
           "emergency",
           "alert",
           "critical",
           "error",
           "warning",
           "notice",
           "info",
           "debug",
           "none"
         ]},
      default_lazy: fn
        %{env: :test} -> "warning"
        %{env: :dev} -> "debug"
        %{env: :prod} -> "debug"
      end,
      groups: [:base]
    },
    "DATABASE_URL" => %{
      aliases: ["DB_URL"],
      doc: "Full DB URL",
      cast: Type.URL,
      context: [env: [:prod]],
      groups: [:primary_db],
      required: [:primary_db]
    },
    "DATABASE_POOL_SIZE" => %{
      aliases: ["DB_POOL_SIZE", "POOL_SIZE"],
      cast: {Type.Integer, scope: :positive},
      default: "10",
      context: [env: [:prod]],
      groups: [:primary_db]
    }
  })
end

Link to this section Summary

Types

A spec for a single variable

Functions

A helper which performs basic validations of the input schema and then defines variables_schema/0 function.

Link to this section Types

Link to this type

variable_name()

Specs

variable_name() :: term()
Link to this type

variable_spec()

Specs

variable_spec() :: %{
  :cast => module() | {module(), opts :: keyword()},
  :groups => [atom()],
  optional(:doc) => String.t(),
  optional(:default) => String.t(),
  optional(:default_lazy) => (Confispex.context() -> String.t() | nil),
  optional(:template_value_generator) => (() -> String.t()),
  optional(:required) => [atom()] | (Confispex.context() -> [atom()]),
  optional(:context) => [{atom(), atom()}],
  optional(:aliases) => [variable_name()]
}

A spec for a single variable

  • :cast - describes how value should be cast.
  • :groups - a list of groups which are affected by variable.
  • :doc - a description about variable, shown in generated .envrc file.
  • :default - default value. Must be set in raw format. Raw format was choosen to populate .envrc file with default values.
  • :default_lazy - default value based on given context. Useful when default value must be different for different environments. Cannot be used alongside with :default parameter. Return nil if default value should be ignored.
  • :template_value_generator - a function that is used in confispex.gen.envrc_template mix task to generate a value for a variable. Such value will always be uncommented even if it is not required. This is useful for variables like "SECRET_KEY_BASE" which should be generated only once.
  • :required - a list of groups or a function that returns a list of groups in which variable is required. When all required variables of the group are cast successfully, then the group is considered as ready for using.
  • :context - specifies context in which variable is used.
  • :aliases - a list of alias names.

Link to this section Callbacks

Link to this callback

variables_schema()

Specs

variables_schema() :: %{required(variable_name()) => variable_spec()}

Link to this section Functions

Link to this macro

defvariables(variables)

(macro)

A helper which performs basic validations of the input schema and then defines variables_schema/0 function.