Validation
Validation:
object
Defined in: Core/Validation.ts:42
Type Declaration
Section titled “Type Declaration”apply: {<
E2,A>(arg): <B,E1>(validation) =>Validation<E2|E1,B>; <E1,E2,A,E3>(arg,options): <B>(validation) =>Validation<E3,B>; }
Applies a function wrapped in a Validation to a value wrapped in a Validation.
Accumulates errors from both sides if both fail, using optional combineErrors
or default concatenation.
Call Signature
Section titled “Call Signature”<
E2,A>(arg): <B,E1>(validation) =>Validation<E2|E1,B>
Type Parameters
Section titled “Type Parameters”E2 = never
A = unknown
Parameters
Section titled “Parameters”Validation<E2, A>
Returns
Section titled “Returns”<B, E1>(validation) => Validation<E2 | E1, B>
Call Signature
Section titled “Call Signature”<
E1,E2,A,E3>(arg,options): <B>(validation) =>Validation<E3,B>
Type Parameters
Section titled “Type Parameters”E1
E2
A
E3
Parameters
Section titled “Parameters”Validation<E2, A>
options
Section titled “options”combineErrors
Section titled “combineErrors”(e1, e2) => NonEmptyArr<E3>
Returns
Section titled “Returns”<B>(validation) => Validation<E3, B>
Validation.struct to combine record validations.
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,NonNullable<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, NonNullable<A>>
Example
Section titled “Example”from.Predicate
Section titled “from.Predicate”Predicate: {<
A,B,E>(refinement,onFalse): (value) =>Validation<E,B>; <A,E>(predicate,onFalse): (value) =>Validation<E,A>; }
Creates a Validation from a predicate applied to a value.
Returns Passed if the predicate passes, Failed from onFalse otherwise.
Call Signature
Section titled “Call Signature”<
A,B,E>(refinement,onFalse): (value) =>Validation<E,B>
Type Parameters
Section titled “Type Parameters”A
B
E
Parameters
Section titled “Parameters”refinement
Section titled “refinement”(value) => value is B
onFalse
Section titled “onFalse”(value) => E
Returns
Section titled “Returns”(value) => Validation<E, B>
Call Signature
Section titled “Call Signature”<
A,E>(predicate,onFalse): (value) =>Validation<E,A>
Type Parameters
Section titled “Type Parameters”A
E
Parameters
Section titled “Parameters”predicate
Section titled “predicate”(value) => boolean
onFalse
Section titled “onFalse”(value) => E
Returns
Section titled “Returns”(value) => Validation<E, A>
Example
Section titled “Example”from.Result
Section titled “from.Result”Result: <
E,A>(result) =>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
Section titled “result”Result<E, A>
Returns
Section titled “Returns”Validation<E, A>
Example
Section titled “Example”getOrElse
Section titled “getOrElse”getOrElse: <
B>(fallback) => <A,E>(validation) =>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”fallback
Section titled “fallback”() => B
Returns
Section titled “Returns”<A, E>(validation) => B | A
Validation.match to handle both the passed and failed cases.
Example
Section titled “Example”is:
object
is.failed
Section titled “is.failed”failed: <
E,A>(validation) =>validation 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
Section titled “validation”Validation<E, A>
Returns
Section titled “Returns”validation is Failed<E>
Validation.is.passed to check if a Validation is passed.
Example
Section titled “Example”is.passed
Section titled “is.passed”passed: <
E,A>(validation) =>validation 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
Section titled “validation”Validation<E, A>
Returns
Section titled “Returns”validation is Passed<A>
Validation.is.failed to check if a Validation is failed.
Example
Section titled “Example”keyed:
object
keyed.getErrors
Section titled “keyed.getErrors”getErrors: <
T>(keyed) =>Maybe<KeyedErrors<{ [K in string | number | symbol]: T[K] extends Validation<unknown, A> ? A : never },T[keyofT] extendsValidation<E,unknown> ?E:never>>
Extracts keyed field errors if any field failed.
Symmetrical to getPassed: returns Some(errors) when there are failures,
or None if every field passed.
Type Parameters
Section titled “Type Parameters”T extends Record<string, Validation<unknown, unknown>>
Parameters
Section titled “Parameters”T
Returns
Section titled “Returns”Maybe<KeyedErrors<{ [K in string | number | symbol]: T[K] extends Validation<unknown, A> ? A : never }, T[keyof T] extends Validation<E, unknown> ? E : never>>
Validation.keyed.getPassed to extract keyed values when all fields pass.
Example
Section titled “Example”keyed.getPassed
Section titled “keyed.getPassed”getPassed: <
T,E>(keyed) =>Maybe<T>
Extracts all validated field values if every field passed.
Returns Some(data) if all passed, None if any field failed.
Type Parameters
Section titled “Type Parameters”T extends Record<string, unknown>
E
Parameters
Section titled “Parameters”{ readonly [K in string | number | symbol]: Validation<E, T[K]> }
Returns
Section titled “Returns”Maybe<T>
Validation.keyed.getErrors to extract keyed field errors when failures occur.
Example
Section titled “Example”keyed.is
Section titled “keyed.is”is:
object
keyed.is.failed
Section titled “keyed.is.failed”failed: <
T,E>(keyed) =>boolean
Checks if at least one keyed field is Failed.
Type Parameters
Section titled “Type Parameters”T extends Record<string, unknown>
E
Parameters
Section titled “Parameters”{ readonly [K in string | number | symbol]: Validation<E, T[K]> }
Returns
Section titled “Returns”boolean
Validation.keyed.is.passed to check if all keyed fields passed.
Example
Section titled “Example”keyed.is.passed
Section titled “keyed.is.passed”passed: <
T,E>(keyed) => keyed is { readonly [K in string | number | symbol]: Passed<T[K]> }
Type guard checking if every keyed field is Passed.
Type Parameters
Section titled “Type Parameters”T extends Record<string, unknown>
E
Parameters
Section titled “Parameters”{ readonly [K in string | number | symbol]: Validation<E, T[K]> }
Returns
Section titled “Returns”keyed is { readonly [K in string | number | symbol]: Passed<T[K]> }
Validation.keyed.is.failed to check if any keyed field failed.
Example
Section titled “Example”keyed.make
Section titled “keyed.make”make: <
T,E>(validators) => (input) =>KeyedResult<T,E>
Creates a keyed validator function from a schema of field validators. Evaluates each validator against its corresponding field in the input object, returning a record where every key holds its own Validation outcome.
Type Parameters
Section titled “Type Parameters”T extends Record<string, unknown>
E = unknown
Parameters
Section titled “Parameters”validators
Section titled “validators”{ readonly [K in string | number | symbol]: (val: T[K]) => Validation<E, T[K]> }
Returns
Section titled “Returns”(input) => KeyedResult<T, E>
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: <
A>(value) =>Passed<A> =makePassed
Wraps a value in a passed Validation.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”A
Returns
Section titled “Returns”Passed<A>
Example
Section titled “Example”map: <
A,B>(transform) => <E>(validation) =>Validation<E,B>
Transforms the success value inside a Validation.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”transform
Section titled “transform”(value) => B
Returns
Section titled “Returns”<E>(validation) => Validation<E, B>
- Validation.mapError to transform accumulated errors.
- Validation.apply to combine multiple validations.
Example
Section titled “Example”mapError
Section titled “mapError”mapError: <
E,F>(transform) => <A>(validation) =>Validation<F,A>
Transforms the error list inside a Validation.
Type Parameters
Section titled “Type Parameters”E
F
Parameters
Section titled “Parameters”transform
Section titled “transform”(error) => F
Returns
Section titled “Returns”<A>(validation) => Validation<F, A>
Validation.map to transform the success value.
Example
Section titled “Example”match: <
E,A,B>(cases) => (validation) =>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”(value) => B
Returns
Section titled “Returns”(validation) => B
Validation.getOrElse to extract the success value with a default fallback.
Example
Section titled “Example”recover
Section titled “recover”recover: <
E1,B,E2>(fallback) => <A>(validation) =>Validation<E2,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 or resolve with a different error type.
Type Parameters
Section titled “Type Parameters”E1
B
E2 = never
Parameters
Section titled “Parameters”fallback
Section titled “fallback”(errors) => Validation<E2, B>
Returns
Section titled “Returns”<A>(validation) => Validation<E2, B | A>
Validation.recoverUnless to conditionally recover based on accumulated errors.
recoverUnless
Section titled “recoverUnless”recoverUnless: <
E1,B,E2>(isBlocked,fallback) => <A>(validation) =>Validation<E1|E2,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<E1 | E2, A | B>.
Type Parameters
Section titled “Type Parameters”E1
B
E2 = never
Parameters
Section titled “Parameters”isBlocked
Section titled “isBlocked”(error) => boolean
fallback
Section titled “fallback”(errors) => Validation<E2, B>
Returns
Section titled “Returns”<A>(validation) => Validation<E1 | E2, B | A>
Validation.recover for unconditional error recovery.
Example
Section titled “Example”struct
Section titled “struct”struct: <
T>(fields) =>Validation<T[keyofT] extendsValidation<E,unknown> ?E:never, { [K in string | number | symbol]: T[K] extends Validation<unknown, A> ? A : never }>
Combines a record of Validations into a single Validation of a record. Accumulates all failed branches’ errors.
Type Parameters
Section titled “Type Parameters”T extends Record<string, Validation<unknown, unknown>>
Parameters
Section titled “Parameters”fields
Section titled “fields”T
Returns
Section titled “Returns”Validation<T[keyof T] extends Validation<E, unknown> ? E : never, { [K in string | number | symbol]: T[K] extends Validation<unknown, A> ? A : never }>
Example
Section titled “Example”tap: <
A>(sideEffect) => <E>(validation) =>Validation<E,A>
Executes a side effect on the success value without changing the Validation.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”sideEffect
Section titled “sideEffect”(value) => void
Returns
Section titled “Returns”<E>(validation) => Validation<E, A>
Validation.tapError to perform a side effect on accumulated errors.
Example
Section titled “Example”tapError
Section titled “tapError”tapError: <
E>(sideEffect) => <A>(validation) =>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
Parameters
Section titled “Parameters”sideEffect
Section titled “sideEffect”(errors) => void
Returns
Section titled “Returns”<A>(validation) => Validation<E, A>
Validation.tap to perform a side effect on the success value.
Example
Section titled “Example”to:
object
to.Maybe
Section titled “to.Maybe”Maybe: <
E,A>(validation) =>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
Section titled “validation”Validation<E, A>
Returns
Section titled “Returns”Maybe<A>
Example
Section titled “Example”to.Result
Section titled “to.Result”Result: {<
E1,E2,A>(combineErrors): (validation) =>Result<E2,A>; <E,A>(validation):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): (validation) =>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”(validation) => Result<E2, A>
Call Signature
Section titled “Call Signature”<
E,A>(validation):Result<NonEmptyArr<E>,A>
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”validation
Section titled “validation”Validation<E, A>
Returns
Section titled “Returns”Result<NonEmptyArr<E>, A>
Example
Section titled “Example”tryCatch
Section titled “tryCatch”tryCatch: <
E,A>(fn,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”(error) => E
Returns
Section titled “Returns”Validation<E, A>