Message 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 WithWith PointsMessage Validation
You’re creating a messaging application where users can send each other messages as a string.
Message.send("hello!")
"hello!"
Use guards to ensure Message.send/1 does not allow invalid data types.
Message.send(123)
** (FunctionClauseError) no function clause matching in Message.send/1
The following arguments were given to Message.send/1:
# 1
123
Users can also send each other messages as a Message struct which has a :body key.
Make sure you define a message struct, and use guards to create a Message.send/1 clause which can handle Message structs. Message.send/1 should return the :body field of the message.
Message.send(%Message{body: "hello!"})
Only allow messages that have a string in their :body field.
Message.send(%Message{body: 123})
** (FunctionClauseError) no function clause matching in Message.send/1
The following arguments were given to Message.send/1:
# 1
%Message{body: 123}
Example Solution
defmodule Message do
defstruct [:body]
def send(message) when is_binary(message) do
message
end
def send(message) when is_binary(message.body) do
message.body
end
end
Implement the Message module as documented below.
defmodule Message do
@moduledoc """
Documentation for `Message`
## Examples
iex> %Message{}
%Message{body: nil}
"""
defstruct []
@doc """
Send messages between users.
Returns a string of the message if provided valid input.
## Examples
iex> Message.send("hello!")
"hello!"
iex> Message.send("hi!")
"hi!"
iex> Message.send(%Message{body: "hello!"})
"hello!"
iex> Message.send(%Message{body: "hi!"})
"hi!"
iex> Message.send(123)
** (FunctionClauseError) no function clause matching in Message.send/1
iex> Message.send(%{})
** (FunctionClauseError) no function clause matching in Message.send/1
iex> Message.send({})
** (FunctionClauseError) no function clause matching in Message.send/1
iex> Message.send(%Message{body: nil})
** (FunctionClauseError) no function clause matching in Message.send/1
iex> Message.send(%Message{body: {}})
** (FunctionClauseError) no function clause matching in Message.send/1
"""
def send(message) do
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 Message 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.