ExParamsSchema.Parser (ex_params_schema v0.1.1)

Copy Markdown View Source

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

detailed_parse_result()

@type detailed_parse_result() :: ExParamsSchema.detailed_parse_result()

map_parse_result()

@type map_parse_result() :: ExParamsSchema.parse_result()

struct_parse_result()

@type struct_parse_result() ::
  {:ok, params :: struct()} | {:error, reason :: ExParamsSchema.error_reason()}

Functions

parse(params, schema)

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}

parse(params, module, schema)

Casts the result and returns it in the specified struct.

Normally, use parse/1 generated by use ExParamsSchema.

parse_detailed(params, schema)

@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}

parse_detailed(params, module, schema)

@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.