Hyperlinkv0.8.0-beta.28

SchemaGetter

SchemaGetter.checkEffectfunctioneffect/SchemaGetter.ts:447
<T, R = never>(
  f: (
    input: T,
    options: SchemaAST.ParseOptions
  ) => Effect.Effect<undefined | boolean | Schema.FilterIssue, never, R>
): Getter<T, T, R>

Creates a getter that validates a value using an effectful check function.

When to use

Use when you need a schema getter to validate a decoded value (e.g. check a constraint or call an external service).

  • The validation may be asynchronous or require Effect services.

Details

  • Only runs when input is SomeNone passes through.
  • The check function returns a validation result:
    • undefined or true — value is valid, passes through.
    • false or a string — value is invalid, fails with an Issue.
    • An Issue object — fails with that issue directly.
    • { path, issue } — fails with a nested path issue (issue may be a message string or a full SchemaIssue.Issue).
  • Does not transform the value — input and output types are the same.

Example (Validating effectfully)

import { Effect, SchemaGetter } from "effect"

const nonNegative = SchemaGetter.checkEffect<number>((n) =>
  Effect.succeed(n >= 0 ? undefined : "must be non-negative")
)
export function checkEffect<T, R = never>(
  f: (input: T, options: SchemaAST.ParseOptions) => Effect.Effect<
    undefined | boolean | Schema.FilterIssue,
    never,
    R
  >
): Getter<T, T, R> {
  return onSome((t, options) => {
    return f(t, options).pipe(Effect.flatMapEager((out) => {
      const issue = SchemaIssue.makeSingle(t, out)
      return issue ?
        Effect.fail(issue) :
        Effect.succeed(Option.some(t))
    }))
  })
}