Skip to content

Instantly share code, notes, and snippets.

@shdblowers
Last active November 21, 2016 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shdblowers/0c32ea5f73608ba9673c73a5fcb503d8 to your computer and use it in GitHub Desktop.
Save shdblowers/0c32ea5f73608ba9673c73a5fcb503d8 to your computer and use it in GitHub Desktop.
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