Here is when I am a little puzzled: PUT. I send a PUT request where none of the parameters in the body are correct and I get status 200OK, but the product does not change at all.
Shouldn’t that return an error?
I tried changing Repo.update() to Repo.update!() in my_app/operations/operations.ex but it fails even with the correct data now.
How can I make sure that atleast one parameter (if not all that are supplied) are correct? How could I enforce all required parameters being passed in the PUT request?
What is the best practise?
Yes, you are right. When I use incorrect data types it works (I mean it throws errors as expected).
I will try not using casting for the update when I am back home.
Yeah, sorry, you are correct.
I guess I can enforce the product_name key being present with pattern matching in my-app/operations/operations.ex:
%{"product_name" => _product_name} = attrs
It is not the same as throwing an error when some wrong parameters are parsed, but that should be ok.
However, I am not sure how I could make to re-route a specific error page, like 400 if the match fails.
Yeah, that makes sense.
I think I am getting there, just one piece missing.
I attempted to validate the attrs as follows:
def update_product(%Product{} = product, attrs) do
new_update_validation = %Product{} |> Product.changeset(attrs)
product
|> Product.changeset(attrs)
|> Repo.update()
end
Variable new_update_validation correctly contains errors if I do not specify a required parameter. How should I proceed? I was thinking about using add error to the product. Is there any smarter way to do it? Thanks
You will need to make your custom validation in that case, because it’s not the default behaviour…
Those 2 points are valid in the Phoenix way, because, when updating, You rely on existing data. And it is also the case in Rails, and probably more frameworks…