-
-
Save shdblowers/0c32ea5f73608ba9673c73a5fcb503d8 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 User do | |
defstruct [:name, :email] | |
def create(params) do | |
with {:ok, name} <- validate_name(params["Name"]), | |
{:ok, email} <- validate_email(params["Email"]) | |
do | |
{:ok, %__MODULE__{name: name, email: email}} # create user model with validated params | |
else | |
{:error, message} -> | |
{:error, message} # pass the error back to the caller | |
end | |
end | |
# basic validation, which should be added too | |
defp validate_name(nil), do: {:error, "Tell me your name!"} | |
defp validate_name(name), do: {:ok, name} | |
# basic validation, which should be added too | |
defp validate_email(nil), do: {:error, "Tell me you email address!"} | |
defp validate_email(email), do: {:ok, email} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment