extract v0.1.1 Parsers.FloatParser

Parse input data for real numbers

Link to this section Summary

Link to this section Functions

Link to this function parse(struct, param_name, params, restrictions \\ []) (since 0.1.0)
parse(Map | Tuple, String | Atom, Map, Keywords | nil) ::
  {:ok, [], struct()} | {:error, [String], struct()}

Parse input data for float.

Returns a tuple with :ok or :error, a list of messages (used only if returns :error) and struct.

struct is previous struct. Can be map or tuple, anything else do nothing. param_name is key name. Can be atom or string. params is map with input data. Need to be a map. restrictions is keywords with restrictions of input. Can contains:

  • :default - will put input data with this value if input data is nil or empty string.
  • :max - the max value. Input data need to be equals or upper of this value. Need to be float or integer number.
  • :min - the min value. Input data need to be equals or lower of this value. Need to be float or integer number.
  • :required - if params map needs to have input value with valid float.

Examples

Just call:

iex> Parsers.FloatParser.parse(%{}, :float, %{float: "1.0"})
{:ok, [], %{float: 1.0}}

# With integer value
iex> Parsers.FloatParser.parse(%{}, :float, %{float: "1"})
{:ok, [], %{float: 1}}

# With default value
iex> Parsers.FloatParser.parse(%{other: 123}, :float, %{float: ""}, default: "1.0")
{:ok, [], %{float: 1.0, other: 123}}

# If input data is invalid
iex> Parsers.FloatParser.parse(%{other: 123}, :float, %{float: "asdsa"})
{:error, ["invalid_float"], %{other: 123}}

# If input data is required and not provider
iex> Parsers.FloatParser.parse(%{other: 123}, :float, %{}, required: true)
{:error, ["float_not_provided"], %{other: 123}}