Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help: validate cursors? #127

Open
ceolinrenato opened this issue Jun 14, 2021 · 0 comments
Open

help: validate cursors? #127

ceolinrenato opened this issue Jun 14, 2021 · 0 comments
Labels

Comments

@ceolinrenato
Copy link

ceolinrenato commented Jun 14, 2021

I'm using an Ecto embedded_schema in order to validate pagination params being passed into the query string of my API.

However I'm wondering how can I check if a cursor is indeed valid and without having to rescue ArgumentError from Paginator.paginate/4


  defp changeset(schema \\ %__MODULE__{}, params) do
    schema
    |> cast(params, @optional)
    |> validate_cursor()
    |> validate_number(:limit, greater_than: 0, message: "must be greater than 0")
  end

  defp validate_cursor(changeset) do
    cursor_before = get_field(changeset, :before)
    cursor_after = get_field(changeset, :after)

    case {cursor_before, cursor_after} do
      {nil, nil} ->
        changeset

      {_cursor_before, nil} ->
        changeset

      {nil, _cursor_after} ->
        changeset

      {_cursor_before, _cursor_after} ->
        changeset
        |> add_error(:before, "use either 'before' or 'after'")
        |> add_error(:after, "use either 'before' or 'after'")
    end
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants