Hyperlinkv0.8.0-beta.28

SchemaTransformation

SchemaTransformation.Transformationclasseffect/SchemaTransformation.ts:142
Transformation<T, E, RD, RE>

Represents a bidirectional transformation between a decoded type T and an encoded type E, built from a pair of Getters.

When to use

Use when you need a schema transformation that defines how a schema converts between two representations.

  • You want to compose multiple transformations into a pipeline.
  • You want to flip a transformation to swap decode/encode.

Details

This is the primary building block for Schema.decodeTo, Schema.encodeTo, Schema.decode, Schema.encode, and Schema.link. Each direction is a SchemaGetter.Getter that handles optionality, failure, and Effect services.

  • Immutable — flip() and compose() return new instances.
  • flip() swaps the decode and encode getters.
  • compose(other) chains: this.decode then other.decode for decoding, other.encode then this.encode for encoding.

Example (Composing two transformations)

import { SchemaTransformation } from "effect"

const trimAndLower = SchemaTransformation.trim().compose(
  SchemaTransformation.toLowerCase()
)
// decode: trim then lowercase
// encode: passthrough (both directions)
export class Transformation<in out T, in out E, RD = never, RE = never> {
  readonly [TypeId] = TypeId
  readonly _tag = "Transformation"
  readonly decode: SchemaGetter.Getter<T, E, RD>
  readonly encode: SchemaGetter.Getter<E, T, RE>

  constructor(
    decode: SchemaGetter.Getter<T, E, RD>,
    encode: SchemaGetter.Getter<E, T, RE>
  ) {
    this.decode = decode
    this.encode = encode
  }
  flip(): Transformation<E, T, RE, RD> {
    return new Transformation(this.encode, this.decode)
  }
  compose<T2, RD2, RE2>(other: Transformation<T2, T, RD2, RE2>): Transformation<T2, E, RD | RD2, RE | RE2> {
    return new Transformation(
      this.decode.compose(other.decode),
      other.encode.compose(this.encode)
    )
  }
}
Referenced by 47 symbols