Hyperlinkv0.8.0-beta.28

SchemaTransformation

SchemaTransformation.transformOptionalfunctioneffect/SchemaTransformation.ts:386
<T, E>(options: {
  readonly decode: (input: Option.Option<E>) => Option.Option<T>
  readonly encode: (input: Option.Option<T>) => Option.Option<E>
}): Transformation<T, E>

Creates a Transformation where decode and encode operate on Option values, giving full control over missing-key handling.

When to use

Use when you need a schema transformation to produce or consume Option.None for absent keys.

  • You are working with optional struct fields.

Details

  • Each function receives Option<input> and returns Option<output>.
  • Option.None input means the key is absent; returning Option.None omits the key from the output.
  • Pure and synchronous.

Example (Converting an optional key to Option)

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

const schema = Schema.Struct({
  a: Schema.optionalKey(Schema.Number).pipe(
    Schema.decodeTo(
      Schema.Option(Schema.Number),
      SchemaTransformation.transformOptional({
        decode: Option.some,
        encode: Option.flatten
      })
    )
  )
})
export function transformOptional<T, E>(options: {
  readonly decode: (input: Option.Option<E>) => Option.Option<T>
  readonly encode: (input: Option.Option<T>) => Option.Option<E>
}): Transformation<T, E> {
  return new Transformation(
    SchemaGetter.transformOptional(options.decode),
    SchemaGetter.transformOptional(options.encode)
  )
}
Referenced by 3 symbols