Created
June 13, 2024 18:25
-
-
Save joeljuca/7c21bd9d043e627bac0b8f80b107dfea to your computer and use it in GitHub Desktop.
This file contains 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 characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment