Validation
Validation:
object
Defined in: Core/Validation.ts:35
Type Declaration
Section titled “Type Declaration”ap: <
E,A>(arg) => <B>(data) =>Validation<E,B>
Applies a function wrapped in a Validation to a value wrapped in a Validation. Accumulates errors from both sides.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Validation<E, A>
Returns
Section titled “Returns”<B>(data) => Validation<E, B>
Example
Section titled “Example”apCustom
Section titled “apCustom”apCustom: <
E1,E2,E3>(concat) => <A>(arg) => <B>(data) =>Validation<E3,B>
Applies a function wrapped in a Validation to a value wrapped in a Validation, using a custom error concatenator function when both sides fail.
Type Parameters
Section titled “Type Parameters”E1
E2
E3
Parameters
Section titled “Parameters”concat
Section titled “concat”(e1, e2) => NonEmptyArr<E3>
Returns
Section titled “Returns”<A>(arg) => <B>(data) => Validation<E3, B>
Example
Section titled “Example”fold: <
E,A,B>(onFailed,onPassed) => (data) =>B
Extracts the value from a Validation by providing handlers for both cases.
Type Parameters
Section titled “Type Parameters”E
A
B
Parameters
Section titled “Parameters”onFailed
Section titled “onFailed”(errors) => B
onPassed
Section titled “onPassed”(a) => B
Returns
Section titled “Returns”(data) => B
Example
Section titled “Example”from:
object
from.Maybe
Section titled “from.Maybe”Maybe: <
E>(onNone) => <A>(maybe) =>Validation<E,A>
Creates a Validation from a Maybe. If the Maybe is None, returns Failed with the error from onNone. Otherwise, returns Passed.
Type Parameters
Section titled “Type Parameters”E
Parameters
Section titled “Parameters”onNone
Section titled “onNone”() => E
Returns
Section titled “Returns”<A>(maybe) => Validation<E, A>
Example
Section titled “Example”from.nullable
Section titled “from.nullable”nullable: <
E>(onNull) => <A>(value) =>Validation<E,A>
Creates a Validation from a nullable value. If the value is null or undefined, returns Failed with the error from onNull. Otherwise, returns Passed.
Type Parameters
Section titled “Type Parameters”E
Parameters
Section titled “Parameters”onNull
Section titled “onNull”() => E
Returns
Section titled “Returns”<A>(value) => Validation<E, A>
Example
Section titled “Example”from.Predicate
Section titled “from.Predicate”Predicate: <
E,A>(pred,onFalse) => (a) =>Validation<E,A>
Creates a Validation from a predicate applied to a value.
Returns Passed if the predicate passes, Failed from onFalse otherwise.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”(a) => boolean
onFalse
Section titled “onFalse”(a) => E
Returns
Section titled “Returns”(a) => Validation<E, A>
Example
Section titled “Example”from.Result
Section titled “from.Result”Result: <
E,A>(data) =>Validation<E,A>
Converts a Result to a Validation. Ok becomes Passed; Err(e) becomes Failed([e]).
Useful when bridging from error-short-circuiting Result pipelines into
error-accumulating Validation pipelines.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Result<E, A>
Returns
Section titled “Returns”Validation<E, A>
Example
Section titled “Example”getOrElse
Section titled “getOrElse”getOrElse: <
B>(defaultValue) => <E,A>(data) =>B|A
Returns the success value or a default value if the Validation is failed.
The default can be a different type, widening the result to A | B.
Type Parameters
Section titled “Type Parameters”B
Parameters
Section titled “Parameters”defaultValue
Section titled “defaultValue”() => B
Returns
Section titled “Returns”<E, A>(data) => B | A
Example
Section titled “Example”is:
object
is.failed
Section titled “is.failed”failed: <
E,A>(data) =>data is Failed<E>=isFailed
Type guard that checks if a Validation is failed.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Validation<E, A>
Returns
Section titled “Returns”data is Failed<E>
Example
Section titled “Example”is.passed
Section titled “is.passed”passed: <
E,A>(data) =>data is Passed<A>=isPassed
Type guard that checks if a Validation is passed.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Validation<E, A>
Returns
Section titled “Returns”data is Passed<A>
Example
Section titled “Example”make:
object
make.failed
Section titled “make.failed”failed: <
E>(error) =>Failed<E> =makeFailed
Creates a failed Validation from a single error.
Type Parameters
Section titled “Type Parameters”E
Parameters
Section titled “Parameters”E
Returns
Section titled “Returns”Failed<E>
Example
Section titled “Example”make.failedAll
Section titled “make.failedAll”failedAll: <
E>(errors) =>Failed<E> =makeFailedAll
Creates a failed Validation from multiple errors.
Type Parameters
Section titled “Type Parameters”E
Parameters
Section titled “Parameters”errors
Section titled “errors”NonEmptyArr<E>
Returns
Section titled “Returns”Failed<E>
Example
Section titled “Example”make.passed
Section titled “make.passed”passed: <
E,A>(value) =>Validation<E,A> =makePassed
Wraps a value in a passed Validation.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”A
Returns
Section titled “Returns”Validation<E, A>
Example
Section titled “Example”map: <
A,B>(f) => <E>(data) =>Validation<E,B>
Transforms the success value inside a Validation.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”(a) => B
Returns
Section titled “Returns”<E>(data) => Validation<E, B>
Example
Section titled “Example”mapError
Section titled “mapError”mapError: <
E,F,A>(f) => (data) =>Validation<F,A>
Transforms the error list inside a Validation.
Type Parameters
Section titled “Type Parameters”E
F
A
Parameters
Section titled “Parameters”(e) => F
Returns
Section titled “Returns”(data) => Validation<F, A>
Example
Section titled “Example”match: <
E,A,B>(cases) => (data) =>B
Pattern matches on a Validation, returning the result of the matching case.
Type Parameters
Section titled “Type Parameters”E
A
B
Parameters
Section titled “Parameters”failed
Section titled “failed”(errors) => B
passed
Section titled “passed”(a) => B
Returns
Section titled “Returns”(data) => B
Example
Section titled “Example”product
Section titled “product”product: <
E,A,B>(first,second) =>Validation<E, readonly [A,B]>
Combines two independent Validation instances into a tuple. If both are Passed, returns Passed with both values as a tuple. If either is Failed, accumulates errors from both sides.
Type Parameters
Section titled “Type Parameters”E
A
B
Parameters
Section titled “Parameters”Validation<E, A>
second
Section titled “second”Validation<E, B>
Returns
Section titled “Returns”Validation<E, readonly [A, B]>
Example
Section titled “Example”productAll
Section titled “productAll”productAll: <
E,A>(data) =>Validation<E, readonlyA[]>
Combines a non-empty list of Validation instances, accumulating all errors. If all are Passed, returns Passed with all values collected into an array. If any are Failed, returns Failed with all accumulated errors.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”NonEmptyArr<Validation<E, A>>
Returns
Section titled “Returns”Validation<E, readonly A[]>
Example
Section titled “Example”recover
Section titled “recover”recover: <
E,B>(fallback) => <A>(data) =>Validation<E,B|A>
Recovers from a Failed state by providing a fallback Validation.
The fallback receives the accumulated error list so callers can inspect which errors occurred.
The fallback can produce a different success type, widening the result to Validation<E, A | B>.
Type Parameters
Section titled “Type Parameters”E
B
Parameters
Section titled “Parameters”fallback
Section titled “fallback”(errors) => Validation<E, B>
Returns
Section titled “Returns”<A>(data) => Validation<E, B | A>
recoverUnless
Section titled “recoverUnless”recoverUnless: <
E,B>(isBlocked,fallback) => <A>(data) =>Validation<E,B|A>
Recovers from a Failed state unless isBlocked returns true for any of the accumulated errors.
The fallback can produce a different success type, widening the result to Validation<E, A | B>.
Type Parameters
Section titled “Type Parameters”E
B
Parameters
Section titled “Parameters”isBlocked
Section titled “isBlocked”(e) => boolean
fallback
Section titled “fallback”() => Validation<E, B>
Returns
Section titled “Returns”<A>(data) => Validation<E, B | A>
Example
Section titled “Example”struct
Section titled “struct”struct: <
E,R>(fields) =>Validation<E,R>
Combines a record of Validations into a single Validation of a record. Accumulates all failed branches’ errors.
Type Parameters
Section titled “Type Parameters”E
R extends Record<string, any>
Parameters
Section titled “Parameters”fields
Section titled “fields”{ [K in string | number | symbol]: Validation<E, R[K]> }
Returns
Section titled “Returns”Validation<E, R>
Example
Section titled “Example”tap: <
E,A>(f) => (data) =>Validation<E,A>
Executes a side effect on the success value without changing the Validation.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”(a) => void
Returns
Section titled “Returns”(data) => Validation<E, A>
Example
Section titled “Example”tapError
Section titled “tapError”tapError: <
E,A>(f) => (data) =>Validation<E,A>
Executes a side effect on the accumulated errors without changing the Validation. Useful for logging or reporting validation failures.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”(errors) => void
Returns
Section titled “Returns”(data) => Validation<E, A>
Example
Section titled “Example”to:
object
to.Maybe
Section titled “to.Maybe”Maybe: <
E,A>(data) =>Maybe<A>
Converts a Validation to a Maybe. Passed becomes Some; Failed becomes None
(errors are discarded).
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Validation<E, A>
Returns
Section titled “Returns”Maybe<A>
Example
Section titled “Example”to.Result
Section titled “to.Result”Result: {<
E1,E2,A>(combineErrors): (val) =>Result<E2,A>; <E,A>(data):Result<NonEmptyArr<E>,A>; } =toResult
Converts a Validation to a Result.
Passed becomes Ok.
Direct call converts Failed to Err with accumulated error list NonEmptyArr<E>.
Curried call converts Failed to Err with combined error E2 via combineErrors.
Call Signature
Section titled “Call Signature”<
E1,E2,A>(combineErrors): (val) =>Result<E2,A>
Type Parameters
Section titled “Type Parameters”E1
E2
A
Parameters
Section titled “Parameters”combineErrors
Section titled “combineErrors”(errors) => E2
Returns
Section titled “Returns”(val) => Result<E2, A>
Call Signature
Section titled “Call Signature”<
E,A>(data):Result<NonEmptyArr<E>,A>
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Validation<E, A>
Returns
Section titled “Returns”Result<NonEmptyArr<E>, A>
Example
Section titled “Example”tryCatch
Section titled “tryCatch”tryCatch: <
E,A>(f,options) =>Validation<E,A>
Creates a Validation from a synchronous thunk that may throw.
Catches any errors and transforms them using the onError function into a Failed validation.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”() => A
options
Section titled “options”onError
Section titled “onError”(e) => E
Returns
Section titled “Returns”Validation<E, A>