Params v2.2.0 Params View Source
Functions for processing params and transforming their changesets.
use Params
provides a defparams
macro, allowing you to define
functions that process parameters according to some schema
Example
defmodule MyApp.SessionController do
use Params
defparams login_params(%{email!: :string, :password!: :string})
def create(conn, params) do
case login_params(params) do
%Ecto.Changeset{valid?: true} = ch ->
login = Params.data(ch)
User.authenticate(login.email, login.password)
# ...
_ -> text(conn, "Invalid parameters")
end
end
end
Link to this section Summary
Functions
Transforms an Ecto.Changeset into a struct
Transforms an Ecto.Changeset into a Map with atom keys
Link to this section Functions
Link to this function
data(ch)
View Source
data(ch)
View Source
data(Ecto.Changeset.t()) :: struct()
data(Ecto.Changeset.t()) :: struct()
Transforms an Ecto.Changeset into a struct.
Recursively traverses and transforms embedded changesets.
For example if the LoginParams
module was defined like:
defmodule LoginParams do
use Params.Schema, %{login!: :string, password!: :string}
end
You can transform the changeset returned by from
into an struct like:
data = LoginParams.from(%{"login" => "foo"}) |> Params.data
data.login # => "foo"
Link to this function
to_map(ch)
View Source
to_map(ch)
View Source
to_map(Ecto.Changeset.t()) :: map()
to_map(Ecto.Changeset.t()) :: map()
Transforms an Ecto.Changeset into a Map with atom keys.
Recursively traverses and transforms embedded changesets and skips keys that was not part of params given to changeset