Phoenix path validation?

lets say i have path like follow http://localhost4000/:dept_id/show. I want to put some validation like dept_id which must be a integer or regex pattern , how can i do this?

You might do it in your controller maybe.

For checking that it’s an integer

def show(conn, %{"dept_id" => dept_id}) when is_integer(dept_id) do
  ...
end

For regex stuff this thread might be helpful Simulate Regex match guards in functions

As has been said, the way to validate it is in the controller. Maru can validate params even in phoenix, just take a look:

1 Like

Maru seems interesting, but what is the response in case of a validation error and can you override the default response?

If you use only Maru, the response is 400, as far as I known, and it is possible to change the default response by treating the exceptions. For phoenix params parser, I never tried to rescue from exceptions.

Thanks for the info. I also looked up what it does when using the Phoenix params parser and Maru just raises an exception, which is fine for me in combination with Phoenix’s custom errors functionality