Converts a Maybe
to a Validation
. Nothing
s map to Failure
s, Just
s map
to Success
es.
forall a, b:
(Maybe a, b) => Validation b a
Converts a Maybe
to a Validation
. Nothing
s map to Failure
s, Just
s map
to Success
es.
Note that since Maybe
failures can't hold a value in the Nothing
tag, you
must provide one for the validation.
const maybeToValidation = require('folktale/conversions/maybe-to-validation');
const { Failure, Success } = require('folktale/validation');
const { Nothing, Just } = require('folktale/maybe');
maybeToValidation(Nothing(), 2); // ==> Failure(2)
maybeToValidation(Just(1), 2); // ==> Success(1)
(aMaybe, failureValue) =>
aMaybe.matchWith({
Nothing: () => Failure(failureValue),
Just: ({ value }) => Success(value)
})