moonsugar v0.1.1 Moonsugar.Validation
The Validation module contains functions that help create and interact with the validation type.
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 type
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 types
maps over a validation type, only applies the function to failure types
Helper function to create a success type
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 type
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 types
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 types
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 type
Examples
iex> Validation.success(3)
{:success, 3}