Skip to content

Cast error when filtering field of type :id with a string containing non-numeric characters. #294

Answered by woylie
linusdm asked this question in Q&A
Discussion options

You must be logged in to vote

You found the right issue. Right now, Flop relies on Ecto when it comes to filter values. Until 0.19, there was no way to tell Flop the Ecto type for join fields. Now that that is in place, and after adding the ecto_type option to alias fields as well, we have all the necessary information to validate filter values before they are passed to Ecto. Until that has been implemented, I see two workarounds. One would be the solution with custom fields you described, which admittedly is a bit cumbersome, and the other would be to catch the CastError:

def list_pets(params \\ %{}) do
  Flop.validate_and_run(Pet, params, for: Pet)
rescue
  Ecto.Query.CastError -> {:ok, {[], %Flop.Meta{}}}
end

You'l…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@linusdm
Comment options

Answer selected by linusdm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants