ApiCommons.Parameter (ApiCommons v0.1.0) View Source

Handle parameters in path, body and query. Provide checks for availability, types and constraints.

Link to this section Summary

Functions

Validate the given parameter against received body, path or query parameters. The parameter can be either Atom | Map | Ecto.Schema. This function is a generic interface and dispatches to single/3, like_schema/3, like_map/3.

Check received parameters against multiple definitions contained in map.

Check received parameters against an Ecto.Schema. All fields of the schema by default are required, optional fields need to be marked.

Parse a single parameter from received data

Link to this section Types

Specs

error() :: [{atom(), String.t()}]

Specs

t() :: %ApiCommons.Parameter{
  error: error(),
  name: atom(),
  opts: keyword(),
  type: atom(),
  valid?: boolean(),
  value: any()
}

Link to this section Functions

Link to this function

check(conn, parameter, opts)

View Source

Specs

check(Plug.Conn.t(), atom() | map() | Ecto.Schema.schema(), keyword()) ::
  Plug.Conn.t()

Validate the given parameter against received body, path or query parameters. The parameter can be either Atom | Map | Ecto.Schema. This function is a generic interface and dispatches to single/3, like_schema/3, like_map/3.

Optional parameters depending the type of parameter. Check single/3, like_schema/3, like_map/3 for more information.

Link to this function

like_map(conn, map_def, opts)

View Source

Specs

like_map(Plug.Conn.t(), map(), keyword()) :: Plug.Conn.t()

Check received parameters against multiple definitions contained in map.

Opts

  • :position - One of [:body, :path, :query, :all], indicating where to take the parameter from
  • :exclude - A list of parameter names as atoms to exclude
  • :optional - A list of parameter names as atoms to mark as optional deprecated
  • :endpoint - Set parsing of associations in preload mode. Meaning only keys are required as input by default deprectaed
  • :depth - The maximum depth to which to resolve associations (default: 2)
Link to this function

like_schema(conn, schema, opts)

View Source

Specs

like_schema(Plug.Conn.t(), module(), keyword()) :: Plug.Conn.t()

Check received parameters against an Ecto.Schema. All fields of the schema by default are required, optional fields need to be marked.

TODO

  • [ ] Take required?/optional from the schema definition?
  • [ ] References for development
  • [ ] Use error information provided by changeset function?

Opts

  • :position - One of [:body, :path, :query, :all], indicating where to take the parameter from
  • :exclude - A list of parameter names as atoms to exclude
  • :optional - A list of parameter names as atoms to mark as optional deprecated
  • :endpoint - Set parsing of associations in preload mode. Meaning only keys are required as input by default deprectaed
  • :depth - The maximum depth to which to resolve associations (default: 2)
Link to this function

single(conn, parameter, opts \\ [])

View Source

Specs

single(Plug.Conn.t(), atom(), keyword()) :: Plug.Conn.t()

Parse a single parameter from received data

Opts

  • ':position- The position of the parameters, one of [:path, :query, :body, :all]. *:acc- Atom, accumulate data under this atom, can be later accessed by this key with Request.get(request, acc_key). *:required?- boolean Wether or not the parameter to check is required. Defaults to (:false) *:default- Any value that should be used as the default value *:check- Function that performs additional checks for the parameter *:type- Atom representing the expected type of the parameter *:min- Integer *:max- Integer Applicable for integer, float and string values (value <= limit) *:on_of` - List A list of values to check against ## Examples You can use parameters located in conn.query_params, conn.path_params and conn.body_params to check parameters at differnt location. iex> conn.body_params |> Parameter.check(:id, )