Valpa.Error exception (Valpa v0.1.1)
View SourceError 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)