new Validation(value){Validation}
| Name | Type | Description |
|---|---|---|
value |
* | Value to wrap. |
Returns:
| Type | Description |
|---|---|
| Validation | Validation wrapped value. |
Examples
Via new
const v1 = new Success(value);
const v2 = new Failure(message);
Via function
const v3 = Success.from(value);
const v4 = Failure.from(message);
Via validation function
const isEmpty = require("lodash/fp/isEmpty");
const isString = require("lodash/fp/isString");
const Validation = require("lodash-fantasy/data/Validation");
function validateStringPresence(value) {
return isString(value) && !isEmpty(value) ?
Validation.Success.from(value) :
Validation.Failure.from("value should be a non-empty string");
}
module.exports = validateStringPresence;
Via abstract validation rule
const Validation = require("lodash-fantasy/data/Validation");
module.exports = (condition, value, message) => condition(value) ?
Validation.Success.from(value) :
Validation.Failure.from(message(value)); // Pass value into the message for possible reference
Members
-
staticValidation.all
-
Returns a
Validationthat resolves all of the validations in the collection into a single validation. UnlikePromise,Validation.allaggregates all of the failures into a single instance ofValidation. However, likePromise,Validation.allcollects all of the values for successes when all items in the collection are aSuccess.Example
const v1 = validationPropertyAIn(context1); // => Success(context1) const v2 = validationPropertyBIn(context2); // => Success(context2) const v3 = validationPropertyCIn(context3); // => Failure("A failure.") const v4 = validationPropertyDIn(context4); // => Failure("B failure.") Validation.all([v1, v2]); // => Success([context1, context2]) Validation.all([v1, v2, v3]); // => Failure(["A failure"]) Validation.all([v1, v2, v3, v4]); // => Failure(["A failure", "B failure"]) -
staticValidation.any
-
Example
const v1 = validationPropertyAIn(context1); // => Success(context1) const v2 = validationPropertyBIn(context2); // => Success(context2) const v3 = validationPropertyCIn(context3); // => Failure("A failure.") const v4 = validationPropertyDIn(context4); // => Failure("B failure.") Validation.any([v1, v2]); // => Success(context1) Validation.any([v1, v2, v3]); // => Failure(["A failure"]) Validation.any([v1, v2, v3, v4]); // => Failure(["A failure", "B failure"]) -
staticValidation.concat
-
Concatenates two
Validationinstances together.Example
Concatenating distinct validations
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); // => Failure([error1, error2]) -
staticValidation.each
-
Iterates over a collection of validations and invokes the
iterateefor eachValidation. Theiterateeis invoked with one argument:(validation). Iteratee functions may exit iteration early by explicitly returning aFailure.Example
const validations = [ validatePerson(person1), // => Success(person1) validatePerson(person2), // => Success(person2) validatePerson(person3), // => Failure([error1, error2]) validatePerson(person4) // => Failure([error2]) ]; Validation.each(validation => validation.orElse(flow(join(", "), console.error)), validations); // => Logs 'error1, error2' then 'error2' // // => validations -
staticValidation.empty
-
Creates an empty
Success.Example
const v1 = Validation.empty(); // => Success() -
staticValidation.equals
-
Determines whether or not the
otheris equal in value to the current (this). This is not a reference check.Examples
Reflexivity
Validation.equals(v1, v1) === true; // => trueSymmetry
Validation(v1, v2) === Validation.equals(v2, v1); // => trueTransitivity
(Validation.equals(v1, v2) === Validation.equals(v2, v3)) && Validation.equals(v1, v3) // => true -
staticValidation.Failure
-
Properties:
Name Type Description FailureFailure Validation failure. -
staticValidation.filter
-
Iterates over a collection of validations, returning an array of all validations the
predicatefor which returns truthy. Thepredicateis invoked with one argument:(validation).Example
Filter and log failures
const validations = [ validatePerson(person1), // => Success(person1) validatePerson(person2), // => Success(person2) validatePerson(person3), // => Failure([error1, error2]) validatePerson(person4) // => Failure([error2]) ]; // Log failures, return successes. Validation.filter(validation => validation.orElse(flow(join(", "), console.error)).isSuccess(), validations); // => Logs 'error1, error2' then 'error2' // // => [Success(person1), Success(person2)] -
staticValidation.from
-
Creates a new
Validationfrom avalue. If thevalueis already aValidationinstance, thevalueis returned unchanged. Otherwise, a newSuccessis created with thevalue. -
staticValidation.isFailure
-
Determines whether or not the value is a
Failure.Example
isFailure(); // => false isFailure(null); // => false isFailure(Success.from(0)); // => false isFailure(Failure.from("Error")); // => true -
staticValidation.isSuccess
-
Determines whether or not the value is a
Success.Example
isSuccess(); // => false isSuccess(null); // => false isSuccess(Success.from(0)); // => true isSuccess(Failure.from("Error")); // => false -
staticValidation.isValidation
-
Determines whether or not the value is a
Validation.Example
isValidation(); // => false isValidation(null); // => false isValidation(Success.from(0)); // => true isValidation(Failure.from("Error")); // => true -
staticValidation.map
-
Creates an array of values by running each
Validationin collection through theiteratee. The iteratee is invoked with one argument:(validation).Example
Mapping all validations to promises
const validations = [ validatePrice(2.10), // => Success(price1) validatePrice(2.25), // => Success(price2) validatePrice("2.50"), // => Failure([error1]) validatePrice("Three dollars") // => Failure([error1]) ]; Validation.map(Validation.toPromise, validations); // => [Promise.resolve(price1), Promise.resolve(price2), Promise.reject([error1]), Promise.reject([error2])] -
staticValidation.mapIn
-
Creates an array of values by invoking
Validation#mapwith theiterateefor eachValidationin the collection. The iteratee is invoked with one argument:(value).Example
Mapping each Validation's value
const validations = [ validatePrice(2.10), // => Success(price1) validatePrice(2.25), // => Success(price2) validatePrice("2.50"), // => Failure([error1]) validatePrice("Three dollars") // => Failure([error1]) ]; Validation.mapIn(Math.floor, validations); // => [Success(2), Success(2), Failure([error1]), Failure([error2])] -
staticValidation.of
-
Example
Validation.of(); // => Success() Validation.of(true); // => Success(true) Validation.of(Success.from(value)); // => Success(Success(value)) Validation.of(Failure.from("Error message")); // => Success(Failure(["Error message"])) -
staticValidation.reduce
-
Reduces collection to a value which is the accumulated result of running each validation in the
validationscollection through theiteratee, where each successive invocation is supplied the return value of the previous. The iteratee is invoked with two arguments:(accumulator, value).Example
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); // => Failure([error1, error2]) -
staticValidation.Success
-
Properties:
Name Type Description SuccessSuccess Validation success. -
staticValidation.toEither
-
Examples
Success to Resolved
Validation.toEither(Either, Success.from(value)); // => Either.Right(value);Failure to Rejected
Validation.toEither(Either, Failure.from(error)); // => Either.Left([error]); -
staticValidation.toMaybe
-
Examples
Success to Resolved
Validation.toMaybe(Maybe, Success.from(value)); // => Maybe.Just(value);Failure to Rejected
Validation.toMaybe(Maybe, Failure.from(error)); // => Maybe.Nothing(); -
staticValidation.toPromise
-
Converts a validation to a
Promiseusing the providedPromiseimplementation.Example
Convert with bluebird's implementation of Promise
const toBluebird = Validation.toPromise(require("bluebird")); toBluebird(Success.from(value)); // => Promise.resolve(value); toBluebird(Failure.from(error)); // => Promise.reject([error]); -
staticValidation.try
-
Tries to invoke a
supplier. The result of thesupplieris returned in aSuccess. If an exception is thrown, the error is returned in aFailure. Thefunctiontakes no arguments.Example
Validation.try(normalFunction); // => Success(returnValue) Validation.try(throwableFunction); // => Failure([error])
Methods
-
abstractap(other){Validation}
-
Applies the function contained in the instance of a
Successto the value contained in the providedSuccess, producing aSuccesscontaining the result. If the instance is aFailure, the result is theFailureinstance. If the instance is aSuccessand the provided validation isFailure, the result is the providedFailure.Name Type Description otherValidation Value to apply to the function wrapped in the Success.Returns:
Type Description Validation Successwrapped 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 -
abstractbimap(failureMap, successMap){Validation}
-
Transforms a
Validationby applying the first function to the contained value for aFailureor the second function for aSuccess. The result of each map is wrapped in the corresponding type.Name Type Description failureMapfunction Map to apply to the Failure.successMapfunction Map to apply to the Success.Returns:
Type Description Validation Validationwrapped value mapped with the corresponding mapping function.Example
validateRequest(request) .bimap(toBadRequestResponse, PersonModel.create) // ... other actions in workflow -
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 methodChain.<Validation> The function to invoke with the value. Returns:
Type Description Validation Validationwrapped 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 -
abstractconcat(other){Validation}
-
Concatenates another
Validationinstance with the current instance.Name Type Description otherValidation Other Validationto 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]) -
equals(other){Boolean}
-
Determines whether or not the
otheris 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 trueif the two validations are equal;falseif not equal.Examples
Reflexivity
v1.equals(v1) === true; // => trueSymmetry
v1.equals(v2) === v2.equals(v1); // => trueTransitivity
(v1.equals(v2) === v2.equals(v3)) && v1.equals(v3) // => true -
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); -
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 methodConsumer 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]) -
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 methodConsumer 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]) -
isFailure(){Boolean}
-
Determines whether or not the instance is a
Failure.Returns:
Type Description Boolean trueif the instance is aFailure;falseis not.Examples
Success#isFailure
Success.from(value).isFailure(); // => falseFailure#isFailure
Failure.from(error).isFailure(); // => true -
isSuccess(){Boolean}
-
Determines whether or not the instance is a
Success.Returns:
Type Description Boolean trueif the instance is aSuccess;falseis not.Examples
Success
Success.from(value).isFailure(); // => trueFailure#isSuccess
Failure.from(error).isFailure(); // => false -
abstractmap(method){Validation}
-
Applies the provided function to the value contained for a
Successwhich is, in turn, wrapped in aSuccess. If the instance is aFailure, the function is ignored and then instance is returned unchanged.Name Type Description methodfunction The function to invoke with the value. Returns:
Type Description Validation Validationwrapped 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]) -
of()
-
- See:
-
abstractorElse(value){*}
-
Returns the value if the instance is a
Successotherwise 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); // => valueFailure#orElse
Failure.from(error).orElse(otherValue); // => otherValue -
abstractorElseGet(method){*}
-
Return the value if the instance is a
Successotherwise returns the value from the function provided.Name Type Description methodSupplier The function supplying the optional value. Returns:
Type Description * Examples
Success#orElseGet
Success.from(value).orElseGet(getOtherValue); // => valueFailure#orElseGet
Failure.from().orElseGet(getOtherValue); // => otherValue -
abstractorElseThrow(method)
-
Applies the provided function to the value contain for a
Failureand throws the resultingError. If the instance is aSuccess, the function is ignored and the instance is returned.Name Type Description methodfunction 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]) -
-
abstracttoEither(either){Either}
-
Converts the validation to an
Eitherusing the providedEitherimplementation.Successbecomes aRightandFailurebecomes aLeft.Name Type Description eitherEither Either implementation. Returns:
Type Description Either Eitherwrappedvalue.Examples
Success#toEither
Success.from(value).toEither(Either); // => Either.Right(value);Failure#toEither
Failure.from(error).toEither(Either); // => Either.Left([error]); -
abstracttoMaybe(maybe){Maybe}
-
Converts the validation to an
Maybeusing the providedMaybeimplementation.Successbecomes aJustandFailurebecomes aNothing.Name Type Description maybeMaybe Maybe implementation. Returns:
Type Description Maybe Maybewrappedvalue.Examples
Success#toMaybe
Success.from(value).toMaybe(Maybe); // => Maybe.Just(value);Failure#toMaybe
Failure.from(error).toMaybe(Maybe); // => Maybe.Nothing(); -
abstracttoPromise(promise){Promise}
-
Converts the validation to a
Promiseusing the providedPromiseimplementation.Name Type Description promisePromise Promise implementation. Returns:
Type Description Promise Promisewrappedvalue.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]); -
abstracttoString(){String}
-
Returns a
Stringrepresentation of theValidation.Returns:
Type Description String Stringrepresentation.Examples
Success#toString
Success.from(1).toString(); // => "Validation.Success(1)"Failure#toString
Failure.from("Error message").toString(); // => "Validation.Failure('Error message')"