Function: toResultWith()
Call Signature
toResultWith<
T,E,F>(validation,combineErrors):Result<T,F>
Defined in: validation/toResult/index.ts:118
Converts a Validation to a Result, combining all errors into a single error.
Like toResult, but uses a function to combine multiple errors into a single error. This is useful when you need a Result with a single error value instead of an array.
Type Parameters
T
T
E
E
F
F
Parameters
validation
Validation<T, E>
The Validation to convert
combineErrors
(errors) => F
Function to combine errors array into single error
Returns
Result<T, F>
A Result with the value or combined error
Example
// Combine errors into a message
toResultWith(
invalid(['Error 1', 'Error 2', 'Error 3']),
(errors) => errors.join('; ')
)
// => Err('Error 1; Error 2; Error 3')
// Use first error only
toResultWith(
invalid(['Error 1', 'Error 2']),
(errors) => errors[0] ?? 'Unknown error'
)
// => Err('Error 1')
// Real-world: Create structured error
const toApiResult = (validation: Validation<User, string>) =>
toResultWith(validation, (errors) => ({
status: 400,
code: 'VALIDATION_ERROR',
message: 'Validation failed',
details: errors
}))
// Real-world: Format for display
const toDisplayResult = (validation: Validation<Data, FieldError>) =>
toResultWith(validation, (errors) => ({
title: 'Please fix the following errors:',
items: errors.map(e => `${e.field}: ${e.errors.join(', ')}`)
}))
See
- toResult - for keeping errors as array
- fromResult - for converting Result to Validation
Call Signature
toResultWith<
E,F>(combineErrors): <T>(validation) =>Result<T,F>
Defined in: validation/toResult/index.ts:122
Converts a Validation to a Result, combining all errors into a single error.
Like toResult, but uses a function to combine multiple errors into a single error. This is useful when you need a Result with a single error value instead of an array.
Type Parameters
E
E
F
F
Parameters
combineErrors
(errors) => F
Function to combine errors array into single error
Returns
A Result with the value or combined error
<
T>(validation):Result<T,F>
Type Parameters
T
T
Parameters
validation
Validation<T, E>
Returns
Result<T, F>
Example
// Combine errors into a message
toResultWith(
invalid(['Error 1', 'Error 2', 'Error 3']),
(errors) => errors.join('; ')
)
// => Err('Error 1; Error 2; Error 3')
// Use first error only
toResultWith(
invalid(['Error 1', 'Error 2']),
(errors) => errors[0] ?? 'Unknown error'
)
// => Err('Error 1')
// Real-world: Create structured error
const toApiResult = (validation: Validation<User, string>) =>
toResultWith(validation, (errors) => ({
status: 400,
code: 'VALIDATION_ERROR',
message: 'Validation failed',
details: errors
}))
// Real-world: Format for display
const toDisplayResult = (validation: Validation<Data, FieldError>) =>
toResultWith(validation, (errors) => ({
title: 'Please fix the following errors:',
items: errors.map(e => `${e.field}: ${e.errors.join(', ')}`)
}))
See
- toResult - for keeping errors as array
- fromResult - for converting Result to Validation