Handling Multiple Effects with Validation in TypeScript

Based on the docs when trying to use Effect.validate I'd expect to get either Effect<[A,B,C]> or Effect<never, [E1,E2,E3]>

What I'm trying to achieve is to run a set of effects and either go the success path or return an array of errors, kind of like a schema validation on some business rules etc. Without using schema how would you do that?

import { Console, Effect } from 'effect'

const e1 = Console.log('e1').pipe(Effect.as(Effect.succeed(1)))
const e2 = Console.log('e2').pipe(Effect.as(Effect.succeed(2)))
const e3 = Console.log('e3').pipe(Effect.as(Effect.fail('error1' as const)))
const e4 = Console.log('e4').pipe(Effect.as(Effect.fail('error2' as const)))
const e5 = Effect.succeed(5)

export const program = e1.pipe(
    Effect.validate(e2),
    Effect.validate(e3),
    Effect.validate(e4),
    Effect.validate(e5),
    Effect.tapError((errors) => Console.error(`Errors: ${errors}`)),
    Effect.tap((successes) => Console.log(`Successes: ${successes}`)),
)

await Effect.runPromise(program)


I'm getting the following output:
console.log
      Successes: {
        "_id": "Exit",
        "_tag": "Success",
        "value": 1
      },{
        "_id": "Exit",
        "_tag": "Success",
        "value": 2
      },{
        "_id": "Exit",
        "_tag": "Failure",
        "cause": {
          "_id": "Cause",
          "_tag": "Fail",
          "failure": "error1"
        }
      },{
        "_id": "Exit",
        "_tag": "Failure",
        "cause": {
          "_id": "Cause",
          "_tag": "Fail",
          "failure": "error2"
        }
      },5
Was this page helpful?