User Validation
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Home Report An Issue Message ValidationMath With GuardsUser Validation
You’re going to use with
to create a User
module that can validate a list of user structs. Handle :missing_name
and :not_a_user
errors. Return {:ok, user}
if the user is valid.
valid_user = %User{name: "Mohsin"}
invalid_user1 = %User{}
invalid_user2 = "Yusef"
[{:ok, %User{name: "Mohsin"}}, {:error, :missing_name}, {:error, :not_a_user}] =
User.validate_users([valid_user, invalid_user1, invalid_user2])
defmodule User do
@moduledoc """
Send messages between users.
Returns a string of the message if provided valid input.
## Examples
iex> User.validate_users([%User{name: "valid user"}])
[{:ok, %User{name: "valid user"}}]
iex> User.validate_users([%User{name: nil}])
[{:error, :missing_name}]
iex> User.validate_users(["invalid user", "invalid user", "invalid user"])
[{:error, :not_a_user}, {:error, :not_a_user}, {:error, :not_a_user}]
"""
defstruct [:name]
def validate_users(users) do
Enum.map(users, fn user ->
with %User{name: name} <- user, true <- {:missing_name, is_binary(name)} do
{:ok, user}
else
false ->
{:error, :missing_name}
_ ->
{:e}
IO.inspect(thing)
:error
end
end)
end
end
Commit Your Progress
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status
to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum
folder to commit your progress.
$ git add .
$ git commit -m "finish User Validation exercise"
$ git push
We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.