Created
March 1, 2018 13:01
-
-
Save erez-rabih/3884e90a4335a6ed5301eb58f654a6d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Plug.Support.Validators do | |
import Plug.Conn | |
def validate_integer(v) do | |
case Integer.parse(v) do | |
:error -> {:error, "could not parse #{v} as integer"} | |
other -> other | |
end | |
end | |
def validate_boolean(v) do | |
case v do | |
nil -> false | |
"true" -> true | |
"false" -> false | |
_other -> {:error, "could not parse #{v} as boolean"} | |
end | |
end | |
def on_error_fn(conn, errors) do | |
json_resp(conn, 422, errors) |> halt | |
end | |
def json_resp(conn, status, body) do | |
conn |> put_resp_header("content-type", "application/json") |> send_resp(status, Poison.encode!(body)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment