0

Parameter Validation

I want to be able to do in the Phoenix Router the validation of the :date parameter for the route /todos/:date, but I am not able to find any documentation or library to achieve this in the Phoenix Routing docs, but I found the Plug Validator library, that looks like what I need, but doesn't work with the Phoenix Framework, just with Elixir projects.

The Code

The Router:

defmodule TasksWeb.Router do
  use TasksWeb, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug :put_root_layout, {TasksWeb.LayoutView, :root}
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", TasksWeb do
    pipe_through :browser

    get "/", PageController, :index
    live "/todos", TodoLive

    # THIS IS THE ROUTE I WANT TO VALIDATE THE PARAMETER :date
    live "/todos/:date", TodoLive
  end
end

The validator I am using currently elsewhere in the code:

defmodule Utils.Validators.Date do

  def valid_iso8601?(%Ecto.Changeset{} = changeset, field) when is_atom(field) do
    date = changeset |> Ecto.Changeset.get_field(field)

    case Date.from_iso8601(date) do
      {:ok, _valid_date} ->
        changeset

      {:error, :invalid_format} ->
        Ecto.Changeset.add_error(changeset, field, "Date '#{date}' has an invalid format - Valid format: YYYY-MM-DD")

      {:error, :invalid_date} ->
        Ecto.Changeset.add_error(changeset, field, "Date '#{date}' is invalid - e.g. 2020-04-31 (April only have 30 days)")
    end
  end

  def valid_iso8601?(date) when is_binary(date) do
    case Date.from_iso8601(date) do
      {:ok, _valid_date} ->
        :ok

      {:error, :invalid_format} ->
        {:invalid_format, "Date '#{date}' has an invalid format - Valid format: YYYY-MM-DD"}

      {:error, :invalid_date} ->
        {:invalid_date, "Date '#{date}' is invalid - e.g. 2020-04-31 (April only have 30 days)"}
    end
  end
end

The Goal is to Validate at the Edge

My goal is to be able to call Utils.Validators.Date.valid_iso8601?(date) from the Phoenix Router, but only when the Route is matched.

I know that a lot of libraries exist to validate this in the Controller or from anywhere else, but what I want is to validate the :date at the edge, aka when the route is matched, not inside my application logic.

So my question is if anyone knows a library that allows to achieve this in the Phoenix Router or how I can write a plug that only is invoked when the route matches, because I know I can write one that inspect all requests, but that is not optimal, and a waste of computer resources.

I would love to be able to do like this:

live "/todos/:date", TodoLive, plug: MyValidatorPlug

Or in alternative as I describe below in the Pipeline section.

Pipelines

I forgot to mention them in my original question, but @fhdhsni kindly remembered me of them and pointed me to a possible solution, but while that may achieve the goal for one route, during my thinking about it I found it to not scale when my router grows, because I will need to wrap each route in a scope for that pipeline.

What I want to mean by scoping the routes to use the pipeline is:

  scope "/", TasksWeb do
    pipe_through :browser
    get "/", PageController, :index

    scope "/todos" do
      live "/", TodoLive

      pipe_through :validator
      live "/:date", TodoLive
      # NOW ANY ROUTE ADDED HERE WILL ALSO BE EXECUTED THROUGH THE VALIDATOR PIPELINE
      # And this is why I don't want to go down this path...
    end
  end

But if was possible to do like this:

live "/todos/:date", TodoLive, pipe_through: :my-validator-pipeline

Then my problem would be solved ;)

SOLUTION

Once this was marked has duplicated when is not a duplicate question, the only alternative to post the solution is editing the answer:

defmodule TasksWeb.Router do
  use TasksWeb, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    #plug :fetch_flash
    plug :fetch_live_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug :put_root_layout, {TasksWeb.LayoutView, :root}

    # *** NEW CODE TO ENABLE THE PLUG VALIDATOR CHECK ***
    plug Plug.Validator, on_error: &TasksWeb.Router.validation_error_callback/2
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", TasksWeb do
    pipe_through :browser
    get "/", PageController, :index

    live "/todos", TodoLive

    # *** NEW CODE TO ENABLE THE PLUG VALIDATOR CHECK ***
    live "/todos/:date", TodoLive, private: %{validate: %{date: &Utils.Validators.Date.valid_iso8601?/1}}
  end

  # *** CALLBACK TO HANDLE THE ERRORS RETURNED BY THE PLUG VALIDATOR CHECK ***
  def validation_error_callback(conn, _errors) do
    conn
    |> put_status(:not_found)
    |> put_view(TasksWeb.ErrorView)
    |> render("404.html")
    |> halt()
  end

  # Other scopes may use custom stacks.
  # scope "/api", TasksWeb do
  #   pipe_through :api
  # end
end

And in mix.ex add to your dependencies {:plug_validator, "~> 0.1.0"},.

Exadra37
  • 11,244
  • 3
  • 43
  • 57
  • 1
    Does this answer your question? [Is there a way to have a Phoenix Plug just for one route?](https://stackoverflow.com/questions/60996561/is-there-a-way-to-have-a-phoenix-plug-just-for-one-route) – fhdhsni Apr 09 '20 at 22:54
  • I already though of using a pipeline, but forgot to mention it in my question. The reason I don't want to use one is because I have to put the route I want to validate inside it's own scope, and then when I have more routes to validate for different params it quickly gets a mess in the Router, unless it exist a way of passing parameters to the pipeline plug... But I will give it another try and see If I discover some new path. Thanks for the suggestion :) – Exadra37 Apr 09 '20 at 23:07
  • check out this answer https://stackoverflow.com/a/60999153/2576218 – fhdhsni Apr 09 '20 at 23:13
  • Thanks for the links, but as I state in my question I don't want to trigger the validation outside the Router, because that is what I am already doing with the Utils Validator. I don't get why this so simple feature available in other frameworks is not available by default in an Elixir Framework. – Exadra37 Apr 10 '20 at 00:01
  • I think you have your answer :) Just put the `pipe_through` above the routes you want to use your validator plug. Again as stated in this answer https://stackoverflow.com/a/60996632/2576218 – fhdhsni Apr 10 '20 at 00:16
  • the plug will be invoked for all the routes after it – fhdhsni Apr 10 '20 at 00:18
  • Exactly why I said it needs to be scoped, and that's why I don't like the solution. – Exadra37 Apr 10 '20 at 00:25
  • I closed it as a duplicate because the linked answer lists both available possibilities to approach the problem. – Aleksei Matiushkin Apr 10 '20 at 04:00
  • But as I said in my answer that doesn't solve my problem. – Exadra37 Apr 10 '20 at 08:43
  • Correct solution is now added into the question. – Exadra37 Apr 10 '20 at 22:41

0 Answers0