EpochtalkServerWeb.Helpers.Validate (epochtalk_server v0.1.0)

Helper for validating and casting query parameters not associated with a model.

Note: Changesets are used to validate query parameters that are associated with a model.

Link to this section Summary

Functions

Helper used to validate and cast request parameters directly out of the incoming paylod map (usually a controller function's attrs parameter) to the specified type. Will raise an EpochtalkServerWeb.CustomErrors.InvalidPayload exception if map value does not pass validation and casting.

Helper used to validate and cast request parameters. Takes in a String.t() and casts it to the specified type. Will raise an EpochtalkServerWeb.CustomErrors.InvalidPayload exception if string does not pass validation and casting.

Link to this section Functions

Link to this function

cast(attrs, key, type, opts \\ [])

@spec cast(attrs :: map(), key :: String.t(), type :: atom(),
  required: boolean(),
  min: integer(),
  max: integer()
) :: any()

Helper used to validate and cast request parameters directly out of the incoming paylod map (usually a controller function's attrs parameter) to the specified type. Will raise an EpochtalkServerWeb.CustomErrors.InvalidPayload exception if map value does not pass validation and casting.

valid-types

Valid Types

typesupported options
:integer:required, :key, :min, :max
:boolean:required, :key

valid-options

Valid Options

optiondescription
:requiredtrue will raise an exception if casting nil
:minmin of value being cast to :integer
:maxmax of value being cast to :integer

example

Example

iex> alias EpochtalkServerWeb.Helpers.Validate
iex> attrs = %{"page" => "42", "extended" => "true", "debug" => "false"}
iex> Validate.cast(attrs, "page", :integer, min: 1, max: 99, required: true)
42
iex> Validate.cast(attrs, "limit", :integer, min: 1)
nil
iex> Validate.cast(attrs, "debug", :boolean)
false
iex> Validate.cast(attrs, "extended", :unsupported) # returns input if type not supported
"true"
iex> Validate.cast(attrs, "post_count", :integer, required: true)
** (EpochtalkServerWeb.CustomErrors.InvalidPayload) Invalid payload, key 'post_count' should be of type 'integer'
Link to this function

cast_str(str, type, opts \\ [])

@spec cast_str(str :: String.t(), type :: atom(),
  key: String.t(),
  required: boolean(),
  min: integer(),
  max: integer()
) :: any()

Helper used to validate and cast request parameters. Takes in a String.t() and casts it to the specified type. Will raise an EpochtalkServerWeb.CustomErrors.InvalidPayload exception if string does not pass validation and casting.

valid-types

Valid Types

typesupported options
:integer:required, :key, :min, :max
:boolean:required, :key

valid-options

Valid Options

optiondescription
:keyreference name of the value attempting to be cast
:requiredtrue will raise an exception if casting nil
:minmin of value being cast to :integer
:maxmax of value being cast to :integer

example

Example

iex> alias EpochtalkServerWeb.Helpers.Validate
iex> Validate.cast_str("15", :integer, key: "page", min: 1, max: 99, required: true)
15
iex> Validate.cast_str(nil, :integer, key: "limit", min: 1)
nil
iex> Validate.cast_str("false", :boolean)
false
iex> Validate.cast_str("true", :unsupported) # returns input if type not supported
"true"
iex> Validate.cast_str(nil, :integer, key: "post_count", required: true)
** (EpochtalkServerWeb.CustomErrors.InvalidPayload) Invalid payload, key 'post_count' should be of type 'integer'