Hyperlinkv0.8.0-beta.28

SchemaTransformation

SchemaTransformation.optionFromNullOrfunctioneffect/SchemaTransformation.ts:1159
<T>(): Transformation<Option.Option<T>, T | null>

Decodes T | null into Option<T> and encodes Option<T> back to T | null.

When to use

Use when you need a schema transformation to convert nullable API fields to Option.

Details

Decoding maps null to Option.none() and non-null values to Option.some(value). Encoding maps Option.none() to null and Option.some(value) to value. The transformation is pure and synchronous.

Example (Converting nullable values to an Option)

import { Schema, SchemaTransformation } from "effect"

const schema = Schema.NullOr(Schema.String).pipe(
  Schema.decodeTo(
    Schema.Option(Schema.String),
    SchemaTransformation.optionFromNullOr()
  )
)
export function optionFromNullOr<T>(): Transformation<Option.Option<T>, T | null> {
  return transform({
    decode: Option.fromNullOr,
    encode: Option.getOrNull
  })
}
Referenced by 1 symbols