Effect CommunityEC
Effect Community3y ago
5 replies
fucory

Mapping Effect Schema to Error

What's the cleanest way to map an effect schema to an error? I got schema types like this:

export const SUINT8 = bigintFromSelf.pipe(
nonNegativeBigint(),
lessThanOrEqualToBigint(UINT8_MAX),
)

and I'm currently mapping it to errors like this:

/
* Safely parses a UINT8 into an Effect.
* @template TUINT8 extends bigint
* @param {TUINT8} uint8
* @returns {Effect.Effect<never, InvalidUINTError, TUINT8>}
*/
export const parseUINT8Safe = (uint8) => {
const out =
/
@type {Effect.Effect<never, InvalidUINTError, TUINT8>} */
(
parseEither(SUINT8)(uint8).pipe(
mapError(
({ errors: cause }) =>
new InvalidUINTError({
uint: /** @type bigint */ (uint8),
size: 8,
cause,
}),
),
)
)
return out
}

Feels overly verbose though is there an easier way create specific validation errors? I want to throw a custom error for all 3 cases (not a big int, negative big int, overflowed)
Was this page helpful?