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

Link to this function collect(validators)

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"]}
Link to this function concat(arg1, arg2)

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}
Link to this function failure(reasons)

Helper function to create a failure tuple.

Examples

iex> Validation.failure(["Goat is floating"])
{:failure, ["Goat is floating"]}
Link to this function from_maybe(result, failure)

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"]}
Link to this function from_nilable(val, failure)

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"]}
Link to this function from_result(result)

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"]}
Link to this function map(result, fun)

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"]}
Link to this function mapFailure(validation, fun)

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}