Hyperlinkv0.8.0-beta.28

SchemaTransformation

SchemaTransformation.transformOrFailfunctioneffect/SchemaTransformation.ts:286
<T, E, RD = never, RE = never>(options: {
  readonly decode: (
    e: E,
    options: SchemaAST.ParseOptions
  ) => Effect.Effect<T, SchemaIssue.Issue, RD>
  readonly encode: (
    t: T,
    options: SchemaAST.ParseOptions
  ) => Effect.Effect<E, SchemaIssue.Issue, RE>
}): Transformation<T, E, RD, RE>

Creates a Transformation from effectful decode and encode functions that can fail with Issue.

When to use

Use when you need a schema transformation that may fail or require Effect services.

Details

  • Each function receives the input value and ParseOptions.
  • Must return an Effect that succeeds with the output or fails with Issue.
  • Skips None inputs (missing keys) — functions are only called on present values.

Example (Parsing a date string that can fail)

import { Effect, Option, Schema, SchemaIssue, SchemaTransformation } from "effect"

const DateFromString = Schema.String.pipe(
  Schema.decodeTo(
    Schema.Date,
    SchemaTransformation.transformOrFail({
      decode: (s) => {
        const d = new Date(s)
        return isNaN(d.getTime())
          ? Effect.fail(new SchemaIssue.InvalidValue(Option.some(s), { message: "Invalid date" }))
          : Effect.succeed(d)
      },
      encode: (d) => Effect.succeed(d.toISOString())
    })
  )
)
export function transformOrFail<T, E, RD = never, RE = never>(options: {
  readonly decode: (e: E, options: SchemaAST.ParseOptions) => Effect.Effect<T, SchemaIssue.Issue, RD>
  readonly encode: (t: T, options: SchemaAST.ParseOptions) => Effect.Effect<E, SchemaIssue.Issue, RE>
}): Transformation<T, E, RD, RE> {
  return new Transformation(
    SchemaGetter.transformOrFail(options.decode),
    SchemaGetter.transformOrFail(options.encode)
  )
}
Referenced by 11 symbols