I do, but what I’m using isn’t to be inserted to the database, it’s just a login view. Is there a way to use Ecto to validate incoming parameters, even if you’re not using them for database stuff?
EDIT: Oh shoot, I just found this, and it looks perfect: GitHub - vic/params: Easy parameters validation/casting with Ecto.Schema, akin to Rails' strong parameters.
I had no idea you can use regular Ecto schemas as “serializers” to validate any data, even non-database input.
EDIT 2: Whoa, thanks for pointing me in the right direction! Turns out this can be done with vanilla Ecto.Changesets, you can use them schemaless to validate any data (they don’t hit the database until they touch a repo). This is brilliant!
Here’s the end solution.
- Create a schemaless changeset to validate the input parameters.
def login_changeset(params) do
types = %{username: :string, password: :string}
{%{}, types}
|> cast(params, Map.keys(types))
|> validate_required(:username)
|> validate_required(:password)
end
- Validate the schema in the controller, making sure to return
{:error, changeset}
to your FallbackController.
def login(conn, params) do
changeset = Accounts.User.login_changeset(params)
if changeset.valid? do
%{:username => username, :password => password} = changeset
case Accounts.authenticate_user(username, password) do
{:ok, user} ->
{:ok, jwt, _full_claims} = Guardian.encode_and_sign(user, :api)
render(conn, "token.json", user, jwt)
{:error, _reason} ->
conn
|> put_status(401)
|> render("error.json")
end
else
{:error, changeset}
end
end
- Voila! Your controllers return friendly errors:
HTTP/1.1 422 Unprocessable Entity
cache-control: max-age=0, private, must-revalidate
connection: close
content-length: 116
content-type: application/json; charset=utf-8
date: Sat, 12 Jun 2021 23:20:57 GMT
server: Cowboy
x-request-id: Fof5FK1SQzCkJR8AABFD
{
"errors": {
"password": [
"can't be blank"
],
"username": [
"can't be blank"
]
}
}
I’m so happy right now
This is actually more straightforward than DRF serializers.
Thanks y’all!