Spectre.Operation.Validator (Spectre v0.3.0)

Copy Markdown View Source

Safe validation boundary for registered operation input, output and updates.

Validators declared on an operation spec may be primitive type atoms (:map, :binary, …), a module exporting validate/1, or an explicit {module, function} pair. Custom validators are invoked defensively: a missing module, a raise or a throw becomes a tagged {:error, reason} instead of crashing the caller.

Summary

Functions

Checks that value belongs to a declared domain of allowed values.

Validates value against a declared validator.

Functions

domain(values, value, field)

@spec domain([term()] | nil, term(), term()) :: :ok | {:error, term()}

Checks that value belongs to a declared domain of allowed values.

A nil domain accepts everything.

validate(type, value, field)

@spec validate(term(), term(), term()) :: :ok | {:error, term()}

Validates value against a declared validator.

A nil validator accepts everything. Custom validators may reply with :ok, true, {:ok, _}, false or {:error, reason}; any other reply is reported as {:error, {:invalid_operation_validation_reply, ...}}.