Hyperlinkv0.8.0-beta.28

SchemaGetter

SchemaGetter.onNonefunctioneffect/SchemaGetter.ts:335
<T, E extends T = T, R = never>(
  f: (
    options: SchemaAST.ParseOptions
  ) => Effect.Effect<Option.Option<T>, SchemaIssue.Issue, R>
): Getter<T, E, R>

Creates a getter that handles the case when the input is absent (Option.None).

When to use

Use when you need a schema getter to provide a fallback or computed value for missing struct keys.

  • Building custom "default value" logic more complex than withDefault.

Details

  • When input is None, calls f to produce the result.
  • When input is Some, passes it through unchanged.
  • f receives the parse options and may return None to keep the value absent.

Example (Providing a default timestamp for a missing field)

import { Effect, Option, SchemaGetter } from "effect"

const withTimestamp = SchemaGetter.onNone<number>(() =>
  Effect.succeed(Option.some(Date.now()))
)
export function onNone<T, E extends T = T, R = never>(
  f: (options: SchemaAST.ParseOptions) => Effect.Effect<Option.Option<T>, SchemaIssue.Issue, R>
): Getter<T, E, R> {
  return new Getter((ot, options) => Option.isNone(ot) ? f(options) : Effect.succeed(ot))
}
Referenced by 1 symbols