litmus v1.0.1 Litmus.Type.Any View Source

This type provides validation for any type of value.

Options

  • :default - Setting :default will populate a field with the provided value, assuming that it is not present already. If a field already has a value present, it will not be altered.

  • :required - Setting :required to true will cause a validation error when a field is not present or the value is nil. Allowed values for required are true and false. The default is false.

Examples

iex> schema = %{"id" => %Litmus.Type.Any{required: true}}
iex> Litmus.validate(%{"id" => 1}, schema)
{:ok, %{"id" => 1}}

iex> schema = %{"id" => %Litmus.Type.Any{default: "new_id"}}
iex> Litmus.validate(%{}, schema)
{:ok, %{"id" => "new_id"}}

iex> schema = %{"id" => %Litmus.Type.Any{required: true}}
iex> Litmus.validate(%{}, schema)
{:error, "id is required"}

iex> schema = %{"id" => %Litmus.Type.Any{required: true}}
iex> Litmus.validate(%{"id" => nil}, schema)
{:error, "id is required"}

Link to this section Summary

Link to this section Types

Specs

t() :: %Litmus.Type.Any{default: any(), required: boolean()}

Link to this section Functions

Link to this function

validate_field(type, field, data)

View Source

Specs

validate_field(t(), term(), map()) :: {:ok, map()} | {:error, String.t()}