Casts and validates parameters against compiled schemas.
This low-level module powers ExParamsSchema.parse/2 and the parse/1 functions
generated by use ExParamsSchema. Use the public API in normal use.
Summary
Functions
Casts and validates an input map with a compiled schema.
Casts the result and returns it in the specified struct.
Casts and validates an input map, returning errors with paths and constraints on failure.
Casts and validates an input map, then returns the result in the specified struct.
Types
@type detailed_parse_result() :: ExParamsSchema.detailed_parse_result()
@type map_parse_result() :: ExParamsSchema.parse_result()
@type struct_parse_result() :: {:ok, params :: struct()} | {:error, reason :: ExParamsSchema.error_reason()}
Functions
@spec parse(map(), ExParamsSchema.Schema.t()) :: map_parse_result()
Casts and validates an input map with a compiled schema.
iex> schema = ExParamsSchema.compile!(%{count: {:integer, minimum: 1}})
iex> ExParamsSchema.Parser.parse(%{"count" => "2"}, schema)
{:ok, %{count: 2}}
iex> schema = ExParamsSchema.compile!(%{count: {:integer, minimum: 1}})
iex> ExParamsSchema.Parser.parse([], schema)
{:error, :invalid_params}
@spec parse(term(), module(), ExParamsSchema.Schema.t()) :: struct_parse_result()
Casts the result and returns it in the specified struct.
Normally, use parse/1 generated by use ExParamsSchema.
@spec parse_detailed(map(), ExParamsSchema.Schema.t()) :: detailed_parse_result()
Casts and validates an input map, returning errors with paths and constraints on failure.
iex> schema = ExParamsSchema.compile!(%{count: {:integer, minimum: 1, error: :invalid_count}})
iex> {:error, [error]} = ExParamsSchema.Parser.parse_detailed(%{"count" => "0"}, schema)
iex> {error.path, error.keyword, error.reason}
{["count"], :minimum, :invalid_count}
@spec parse_detailed(term(), module(), ExParamsSchema.Schema.t()) :: {:ok, struct()} | {:error, [ExParamsSchema.validation_error()]}
Casts and validates an input map, then returns the result in the specified struct.
Normally, use parse_detailed/1 generated by use ExParamsSchema.