Class: Failure

Failure

new Failure()

Extends

Methods

staticFailure.from(value){Validation}

Creates a new Failure from a value. If the value is already a Validation instance, the value is returned unchanged. Otherwise, a new Failure is made with the value.
Name Type Description
value * Value to wrap in a Failure.
Returns:
Type Description
Validation Validation when is the value already wrapped or Failure wrapped value.
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"])
Applies the function contained in the instance of a Success to the value contained in the provided Success, producing a Success containing the result. If the instance is a Failure, the result is the Failure instance. If the instance is a Success and the provided validation is Failure, the result is the provided Failure.
Name Type Description
other Validation Value to apply to the function wrapped in the Success.
Returns:
Type Description
Validation Success wrapped applied function or Failure.
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 a Failure or the second function for a Success. 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
Applies the provided function to the value contained for a Success. The function should return the value wrapped in a Validation. If the instance is a Failure, 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 provided method.
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
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
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 a Success, 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 a Failure, 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 a Failure; 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 a Success; false is not.
Examples

Success

Success.from(value).isFailure();
// => true

Failure#isSuccess

Failure.from(error).isFailure();
// => false
Applies the provided function to the value contained for a Success which is, in turn, wrapped in a Success. If the instance is a Failure, 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 provided method.
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 a Failure.
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 resulting Error. If the instance is a Success, 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])

inherited abstracttoEither(either){Either}

Converts the validation to an Either using the provided Either implementation. Success becomes a Right and Failure becomes a Left.
Name Type Description
either Either Either implementation.
Returns:
Type Description
Either Either wrapped value.
Examples

Success#toEither

Success.from(value).toEither(Either);
// => Either.Right(value);

Failure#toEither

Failure.from(error).toEither(Either);
// => Either.Left([error]);

inherited abstracttoMaybe(maybe){Maybe}

Converts the validation to an Maybe using the provided Maybe implementation. Success becomes a Just and Failure becomes a Nothing.
Name Type Description
maybe Maybe Maybe implementation.
Returns:
Type Description
Maybe Maybe wrapped value.
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 provided Promise implementation.
Name Type Description
promise Promise Promise implementation.
Returns:
Type Description
Promise Promise wrapped value.
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 the Validation.
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')"