elixir-maru / maru

Elixir RESTful Framework
https://maru.readme.io
BSD 3-Clause "New" or "Revised" License
1.32k stars 84 forks source link

Automatic validation when using a type #20

Closed venkatd closed 8 years ago

venkatd commented 8 years ago

Hi Falood, I have created a param type for Time in my project like so:

defmodule Maru.ParamType.Time do
  def from(string) do
    Timex.parse!(string, "{ISO:Extended}")
  end
end

If someone passes in an incorrectly formatted time, I'd like the request to fail with an error message. What's the best way to do this with maru? Should I raise an exception from within my from implementation?

Thanks!

falood commented 8 years ago

Just raise an exception within from and rescue it by rescue_from. By default, Maru.Exceptions.InvalidFormatter[reason: :illegal]will be raised if there's an ArgumentError