extract v0.1.1 Parsers.FloatParser
Parse input data for real numbers
Link to this section Summary
Functions
Parse input data for float
Link to this section Functions
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
- ifparams
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}}