moonsugar v0.1.2 Moonsugar.Validation
The Validation module contains functions that help create and interact with the validation type.
The Validation type is represented as either {:success, value}
or {:failure, reasons}
Link to this section Summary
Functions
Combines an array of validation types
Combines validation types. Failures are concatenated. Concatenating two success types returns the last one
Helper function to create a failure tuple
Converts a variable from a maybe type to a validation type
Converts a variable that might be nil to a validation type
Converts a variable from a result type to a validation type
Maps over a validation type, only applies the function to success tuples
Maps over a validation type, only applies the function to failure tuples
Helper function to create a success tuple
Link to this section Functions
Combines an array of validation types.
Examples
iex> Validation.collect([{:failure, ["not long enough"]}, {:failure, ["not enough special chars"]}, {:failure, ["not enough capital letters"]}])
{:failure, ["not long enough", "not enough special chars", "not enough capital letters"]}
Combines validation types. Failures are concatenated. Concatenating two success types returns the last one.
Examples
iex> Validation.concat({:failure, ["not enough chars"]}, {:failure, ["not long enough"]})
{:failure, ["not enough chars", "not long enough"]}
iex> Validation.concat({:failure, ["Game Crashed"]}, {:success, 3})
{:failure, ["Game Crashed"]}
iex> Validation.concat({:success, 2}, {:success, 3})
{:success, 3}
Helper function to create a failure tuple.
Examples
iex> Validation.failure(["Goat is floating"])
{:failure, ["Goat is floating"]}
Converts a variable from a maybe type to a validation type.
Examples
iex> Validation.from_maybe({:just, 3}, ["Not a number"])
{:success, 3}
iex> Validation.from_maybe(:nothing, ["Not a number"])
{:failure, ["Not a number"]}
Converts a variable that might be nil to a validation type.
Examples
iex> Validation.from_nilable("khajiit has wares", ["khajiit does not have wares"])
{:success, "khajiit has wares"}
iex> Validation.from_nilable(nil, ["khajiit does not have wares"])
{:failure, ["khajiit does not have wares"]}
Converts a variable from a result type to a validation type.
Examples
iex> Validation.from_result({:ok, "Dragon Slayed"})
{:success, "Dragon Slayed"}
iex> Validation.from_result({:error, "You Died"})
{:failure, ["You Died"]}
Maps over a validation type, only applies the function to success tuples.
Examples
iex> Validation.map({:success, 3}, fn(x) -> x * 2 end)
{:success, 6}
iex> Validation.map({:failure, ["Dwarves"]}, fn(x) -> x * 2 end)
{:failure, ["Dwarves"]}
Maps over a validation type, only applies the function to failure tuples.
Examples
iex> Validation.mapFailure({:success, 3}, fn(x) -> x * 2 end)
{:success, 3}
iex> Validation.mapFailure({:failure, ["Dwarves"]}, &String.upcase/1)
{:failure, ["DWARVES"]}
Helper function to create a success tuple.
Examples
iex> Validation.success(3)
{:success, 3}