calcinator v5.0.0 Calcinator.Resources.Page View Source

Page in Calcinator.Resources.query_options

Link to this section Summary

Types

t()
  • number - the page number. 1-based.

    • size - the size of each page

Link to this section Types

Link to this type t() View Source
t() :: %Calcinator.Resources.Page{number: pos_integer, size: pos_integer}
  • number - the page number. 1-based.
  • size - the size of each page.

Link to this section Functions

Parses t out of params

No pagination

If there is no "page" key, then there is no pagination.

iex> Calcinator.Resources.Page.from_params(%{})
{:ok, nil}

Pagination

If there is a “page” key with "number" and "size" children, then there is pagination.

iex> Calcinator.Resources.Page.from_params(
...>   %{
...>     "page" => %{
...>       "number" => 1,
...>       "size" => 2
...>     }
...>   }
...> )
{:ok, %Calcinator.Resources.Page{number: 1, size: 2}}

In addition to be decoded JSON, the params can also be the raw %{String.t => String.t} and the quoted integers will be decoded.

iex> Calcinator.Resources.Page.from_params(
...>   %{
...>     "page" => %{
...>       "number" => "1",
...>       "size" => "2"
...>     }
...>   }
...> )
{:ok, %Calcinator.Resources.Page{number: 1, size: 2}}

Errors

A page number can’t be given as the "page" parameter alone because no default page size is assumed.

iex> Calcinator.Resources.Page.from_params(%{"page" => 1})
{
  :error,
  %Alembic.Document{
    errors: [
      %Alembic.Error{
        detail: "`/page` type is not object",
        meta: %{
          "type" => "object"
        },
        source: %Alembic.Source{
          pointer: "/page"
        },
        status: "422",
        title: "Type is wrong"
      }
    ]
  }
}

Required parameters

Likewise, the "page" map can’t have only a "number" parameter because no default page size is assumed.

iex> Calcinator.Resources.Page.from_params(
...>   %{
...>     "page" => %{
...>       "number" => 1
...>     }
...>   }
...> )
{
  :error,
  %Alembic.Document{
    errors: [
      %Alembic.Error{
        detail: "`/page/size` is missing",
        meta: %{
          "child" => "size"
        },
        source: %Alembic.Source{
          pointer: "/page"
        },
        status: "422",
        title: "Child missing"
      }
    ]
  }
}

The page number is not assumed to be 1 when not given.

iex> Calcinator.Resources.Page.from_params(
...>   %{
...>     "page" => %{
...>       "size" => 10
...>     }
...>   }
...> )
{
  :error,
  %Alembic.Document{
    errors: [
      %Alembic.Error{
        detail: "`/page/number` is missing",
        meta: %{
          "child" => "number"
        },
        source: %Alembic.Source{
          pointer: "/page"
        },
        status: "422",
        title: "Child missing"
      }
    ]
  }
}

Number format

"page" "number" must be a positive integer. It is 1-based. The first page is "1"

iex> Calcinator.Resources.Page.from_params(
...>   %{
...>     "page" => %{
...>       "number" => 0,
...>       "size" => 10
...>     }
...>   }
...> )
{
  :error,
  %Alembic.Document{
    errors: [
      %Alembic.Error{
        detail: "`/page/number` type is not positive integer",
        meta: %{
          "type" => "positive integer"
        },
        source: %Alembic.Source{
          pointer: "/page/number"
        },
        status: "422",
        title: "Type is wrong"
      }
    ]
  }
}

Size format

"page" "size" must be a positive integer.

iex> Calcinator.Resources.Page.from_params(
...>   %{
...>     "page" => %{
...>       "number" => 1,
...>       "size" => 0
...>     }
...>   }
...> )
{
  :error,
  %Alembic.Document{
    errors: [
      %Alembic.Error{
        detail: "`/page/size` type is not positive integer",
        meta: %{
          "type" => "positive integer"
        },
        source: %Alembic.Source{
          pointer: "/page/size"
        },
        status: "422",
        title: "Type is wrong"
      }
    ]
  }
}
Link to this function positive_integer_from_json(child, error_template) View Source