Skip to content

Latest commit

 

History

History
148 lines (103 loc) · 3.59 KB

message_validation.livemd

File metadata and controls

148 lines (103 loc) · 3.59 KB

Message Validation

Mix.install([
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:tested_cell, github: "brooklinjazz/tested_cell"},
  {:utils, path: "#{__DIR__}/../utils"}
])

Navigation

Return Home Report An Issue

Message 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}
  """

  @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

Run the following in your command line from the beta_curriculum folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish message validation exercise"

Up Next

Previous Next
Guards Math With Guards