Created
June 13, 2024 18:25
-
-
Save joeljuca/7c21bd9d043e627bac0b8f80b107dfea to your computer and use it in GitHub Desktop.
Revisions
-
joeljuca created this gist
Jun 13, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ defmodule MyApp.Accounts do import Ecto.Changeset def sign_up(%{} = params) do params_schema = %{ name: :string, email: :string, phone: :string, password: :string } changeset = {%{}, params_schema} |> cast(params, Map.keys(params_schema)) |> validate_required([:name, :email, :password]) |> validate_length(:password, min: 24) |> validate_confirmation(:password, required: true) # Aqui valido com um `apply_action/2` nomeado, e recebo uma # tupla se a validação der bom 👇 with {:ok, user_params} <- apply_action(changeset, :sign_up), # Com os argumentos externos já validados e filtrados, # chamo a(s) função(ões) adequadas com certa segurança {:ok, user} <- create_user(user_params) do {:ok, user} end end end