Valpa.Error exception (Valpa v0.1.1)

View Source

Error Handling

Valpa returns detailed error structs on validation failure. All errors use the Valpa.Error struct, which contains rich metadata. Optionally, a stacktrace may be included for debugging purposes.

Valpa.Error structure

%Valpa.Error{
  validator: :integer,       # The failed validator name (e.g., :min, :string, :custom_validator)
  value: 3.14,               # The value that failed
  field: :age,               # (optional) Field name
  criteria: nil,             # (optional) Validator-specific context (like min-max range for example)
  text: nil,                 # (optional) Custom error message
  __trace__: [...]           # Internal trace, used for error reporting
}

Stacktrace configuration

By default, stacktraces are:

  • enabled in :dev and :test
  • disabled in :prod

You can override this in your application config if desired:

# config/config.exs
config :valpa, :stacktrace, true

# config/prod.exs
config :valpa, :stacktrace, false

⚠️ You do not have to set this — safe defaults are applied automatically.

Constructing Errors

You can manually create an error with:

Valpa.Error.new(%{
  validator: :sum,
  value: [4, 5, 6],
  field: :diceRolls,
  criteria: 20,
  text: "Sum must be exactly 20"
})

This returns a {:error, %Valpa.Error{...}} tuple ready for use in custom validators.

Setting the Field Later

To associate an error with a field after creation:

Valpa.Error.new(%{...})
|> Valpa.Error.at(:my_field)

Summary

Types

t()

@type t() :: %Valpa.Error{
  __exception__: true,
  __trace__: any() | nil,
  criteria: any() | nil,
  field: atom() | nil,
  text: String.t() | nil,
  validator: atom() | nil,
  value: any() | nil
}

Functions

at(error, field)

new(fields)