Valpa.CustomValidator behaviour (Valpa v0.1.2)
View SourceBehaviour for custom validators.
Implement the validate/1 callback to return:
:ok— if the value is valid{:error, %Valpa.Error{}}— if the value is invalid
Example:
defmodule MyApp.Validators.PositiveInteger do
@behaviour Valpa.CustomValidator
@impl true
def validate(val) when is_integer(val) and val > 0, do: :ok
def validate(val), do:
{:error, Valpa.Error.new(%{
validator: :positive_integer,
value: val,
criteria: "> 0"
})}
end
Summary
Types
@type t() :: module()
@type validate() :: (term() -> :ok | {:error, Valpa.Error.t()})
Callbacks
@callback validate(term()) :: :ok | {:error, Valpa.Error.t()}