new Failure()
Extends
Methods
-
staticFailure.from(value){Validation}
-
Creates a new
Failure
from avalue
. If thevalue
is already aValidation
instance, thevalue
is returned unchanged. Otherwise, a newFailure
is made with thevalue
.Name Type Description value
* Value to wrap in a Failure
.Returns:
Type Description Validation Validation
when is thevalue
already wrapped orFailure
wrappedvalue
.Examples
Failure from nothing
Failure.from(); // => Failure([])
Failure from arbitrary value
Failure.from(true); // => Failure([true])
Failure from Success
Failure.from(Success.from(value)); // => Success.from(value)
Failure from another Failure
Failure.from(Failure.from("Error message")); // => Failure(["Error message"])
-
inherited abstractap(other){Validation}
-
Applies the function contained in the instance of a
Success
to the value contained in the providedSuccess
, producing aSuccess
containing the result. If the instance is aFailure
, the result is theFailure
instance. If the instance is aSuccess
and the provided validation isFailure
, the result is the providedFailure
.Name Type Description other
Validation Value to apply to the function wrapped in the Success
.Returns:
Type Description Validation Success
wrapped applied function orFailure
.Example
Success#ap
const createPerson = curryN(4, Person.create); // Person.create(name, birthdate, address, email) Success.from(createPerson) // => Success(createPerson) .ap(validate(name)) // => Success(name) .ap(validate(birthdate)) // => Success(birthdate) .ap(validate(address)) // => Success(address) .ap(validate(email)) // => Success(email) .ifSuccess(console.log) // => Log Person.create() response .orElse(each(console.error)) // => Logs first error since #ap short circuits after the first Failure
-
inherited abstractbimap(failureMap, successMap){Validation}
-
Transforms a
Validation
by applying the first function to the contained value for aFailure
or the second function for aSuccess
. The result of each map is wrapped in the corresponding type.Name Type Description failureMap
function Map to apply to the Failure
.successMap
function Map to apply to the Success
.Returns:
Type Description Validation Validation
wrapped value mapped with the corresponding mapping function.Example
validateRequest(request) .bimap(toBadRequestResponse, PersonModel.create) // ... other actions in workflow
-
inherited abstractchain(method){Validation}
-
Applies the provided function to the value contained for a
Success
. The function should return the value wrapped in aValidation
. If the instance is aFailure
, the function is ignored and then instance is returned unchanged.Name Type Description method
Chain.<Validation> The function to invoke with the value. Returns:
Type Description Validation Validation
wrapped value returned by the providedmethod
.Example
Success#chain
const person = { ... }; const validateResponse = response => HttpStatus.isSuccess(response.statusCode) ? Success(response) : Failure(response.statusMessage); const createPerson = flow(Person.create, validateResponse); // Expects instance of Person const validations = [ validatePersonName(person), // => Success(person) validatePersonBirthdate(person), // => Success(person) validatePersonAddress(person), // => Failure([error1]) validatePersonEmail(person) // => Failure([error2]) ]; Validation.reduce(Validation.concat, Success.empty(), validations) // => Validation
.chain(createPerson) // => Validation .ifSuccess(doSomethingWithResponse) .orElse(each(console.error)); // Log all errors -
inherited abstractconcat(other){Validation}
-
Concatenates another
Validation
instance with the current instance.Name Type Description other
Validation Other Validation
to concatenation.Returns:
Type Description Validation Concatenated validations. Examples
Empty Success with Empty Success
Success.empty().concat(Success.empty()); // => Success.empty()
Empty Success with Success
Success.empty().concat(Success.from(value)); // => Success(value)
Success with Empty Success
Success.from(value).concat(Success.empty()); // => Success(value)
Success1 with Success2
Success.from(value1).concat(Success.from(value2)); // => Success(value1)
Any Success with Failure
anySuccess.concat(Failure.from(error)); // => Failure([error])
Empty Failure with Any Success
Failure.from().concat(anySuccess); // => Failure([])
Failure with Any Success
Failure.from(error).concat(Success); // => Failure([error])
Empty Failure with Empty Failure
Failure.from().concat(Failure.from()); // => Failure([])
Empty Failure with Failure
Failure.from().concat(Failure.from(error)); // => Failure([error])
Failure with Failure
Failure.from(error1).concat(Failure.from(error2)); // => Failure([error1, error2])
-
inherited equals(other){Boolean}
-
Determines whether or not the
other
is equal in value to the current (this
). This is not a reference check.Name Type Description other
* Other value to check. Returns:
Type Description Boolean true
if the two validations are equal;false
if not equal.Examples
Reflexivity
v1.equals(v1) === true; // => true
Symmetry
v1.equals(v2) === v2.equals(v1); // => true
Transitivity
(v1.equals(v2) === v2.equals(v3)) && v1.equals(v3) // => true
-
inherited abstractextend(){Validation}
-
Extends the validation. This is used for workflow continuation where the context has shifted.
Type Description Extend.<Validation> method - The function to invoke with the value. Returns:
Type Description Validation Example
Workflow continuation
// Workflow from makeRequest.js const makeRequest = requestOptions => requestAsPromise(requestOptions) .then(Success.from) .catch(Failure.from); // Workflow from savePerson.js const savePerson = curry((requestOptions, validatedPerson) => { return validatedPerson .map(Person.from) .map(person => set("body", person, requestOptions)) .map(makeRequest); }); // Workflow from processResponse.js const processResponse = validatedResponse => validatedResponse .ifFailure(console.error) .ifSuccess(console.log); validatePerson(person) .extend(savePerson({ method: "POST" })) .extend(processResponse);
-
inherited abstractifFailure(method){Validation}
-
Applies the provided function to the value contain for a
Failure
. Any return value from the function is ignored. If the instance is aSuccess
, the function is ignored and the instance is returned.Name Type Description method
Consumer The function to invoke with the value. Returns:
Type Description Validation Current instance. Examples
Success#ifFailure
Success.from(value).ifFailure(doSomething); // void // => Success(value)
Failure#ifFailure
Failure.from(error).ifFailure(doSomething); // doSomething([error]) // => Failure([error])
-
inherited abstractifSuccess(method){Validation}
-
Applies the provided function to the value contain for a
Success
. Any return value from the function is ignored. If the instance is aFailure
, the function is ignored and the instance is returned.Name Type Description method
Consumer The function to invoke with the value. Returns:
Type Description Validation Current instance. Examples
Success#ifSuccess
Success.from(value).ifSuccess(doSomething); // doSomething(value) // => Success(value)
Failure#ifSuccess
Failure.from(error).ifSuccess(doSomething); // void // => Failure([error])
-
inherited isFailure(){Boolean}
-
Determines whether or not the instance is a
Failure
.Returns:
Type Description Boolean true
if the instance is aFailure
;false
is not.Examples
Success#isFailure
Success.from(value).isFailure(); // => false
Failure#isFailure
Failure.from(error).isFailure(); // => true
-
inherited isSuccess(){Boolean}
-
Determines whether or not the instance is a
Success
.Returns:
Type Description Boolean true
if the instance is aSuccess
;false
is not.Examples
Success
Success.from(value).isFailure(); // => true
Failure#isSuccess
Failure.from(error).isFailure(); // => false
-
inherited abstractmap(method){Validation}
-
Applies the provided function to the value contained for a
Success
which is, in turn, wrapped in aSuccess
. If the instance is aFailure
, the function is ignored and then instance is returned unchanged.Name Type Description method
function The function to invoke with the value. Returns:
Type Description Validation Validation
wrapped value mapped with the providedmethod
.Example
// Using lodash/fp/flow and sort Success.from([1, 3, 2]).map(flow(sort, join(", "))); // => Success("1, 2, 3") Failure.from(error).map(flow(sort, join(", "))); // => Failure([error])
-
- See:
-
inherited abstractorElse(value){*}
-
Returns the value if the instance is a
Success
otherwise returns the value supplied if the instance is aFailure
.Name Type Description value
* Value to use if the instace is a Failure
.Returns:
Type Description * Examples
Success#orElse
Success.from(value).orElse(otherValue); // => value
Failure#orElse
Failure.from(error).orElse(otherValue); // => otherValue
-
inherited abstractorElseGet(method){*}
-
Return the value if the instance is a
Success
otherwise returns the value from the function provided.Name Type Description method
Supplier The function supplying the optional value. Returns:
Type Description * Examples
Success#orElseGet
Success.from(value).orElseGet(getOtherValue); // => value
Failure#orElseGet
Failure.from().orElseGet(getOtherValue); // => otherValue
-
inherited abstractorElseThrow(method)
-
Applies the provided function to the value contain for a
Failure
and throws the resultingError
. If the instance is aSuccess
, the function is ignored and the instance is returned.Name Type Description method
function The function to invoke with the value. Throws:
-
returned by the provided function.
- Type
- Error
Examples
Success#orElseThrow
Success.from(value).orElseThrow(createException); // void // => Success(value)
Failure#orElseThrow
Failure.from(error).orElseThrow(createException); // throw createException([error])
-
-
Converts the validation to an
Either
using the providedEither
implementation.Success
becomes aRight
andFailure
becomes aLeft
.Name Type Description either
Either Either implementation. Returns:
Type Description Either Either
wrappedvalue
.Examples
Success#toEither
Success.from(value).toEither(Either); // => Either.Right(value);
Failure#toEither
Failure.from(error).toEither(Either); // => Either.Left([error]);
-
Converts the validation to an
Maybe
using the providedMaybe
implementation.Success
becomes aJust
andFailure
becomes aNothing
.Name Type Description maybe
Maybe Maybe implementation. Returns:
Type Description Maybe Maybe
wrappedvalue
.Examples
Success#toMaybe
Success.from(value).toMaybe(Maybe); // => Maybe.Just(value);
Failure#toMaybe
Failure.from(error).toMaybe(Maybe); // => Maybe.Nothing();
-
inherited abstracttoPromise(promise){Promise}
-
Converts the validation to a
Promise
using the providedPromise
implementation.Name Type Description promise
Promise Promise implementation. Returns:
Type Description Promise Promise
wrappedvalue
.Examples
Success#toPromise
const Bluebird = require("bluebird"); Success.from(value).toPromise(Bluebird); // => Promise.resolve(value);
Failure#toPromise
const Bluebird = require("bluebird"); Failure.from(error).toPromise(Bluebird); // => Promise.reject([error]);
-
inherited abstracttoString(){String}
-
Returns a
String
representation of theValidation
.Returns:
Type Description String String
representation.Examples
Success#toString
Success.from(1).toString(); // => "Validation.Success(1)"
Failure#toString
Failure.from("Error message").toString(); // => "Validation.Failure('Error message')"