Valpa.CustomValidator behaviour (Valpa v0.1.2)

View Source

Behaviour 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

t()

@type t() :: module()

validate()

@type validate() :: (term() -> :ok | {:error, Valpa.Error.t()})

Callbacks

validate(term)

@callback validate(term()) :: :ok | {:error, Valpa.Error.t()}

Functions

ensure_behaviour(validator)