Hyperlinkv0.8.0-beta.28

Schema

Schema.encodeUnknownEffectfunctioneffect/Schema.ts:1837
<S extends Constraint>(schema: S, options?: SchemaAST.ParseOptions): (
  input: unknown,
  options?: SchemaAST.ParseOptions
) => Effect.Effect<S["Encoded"], SchemaError, S["EncodingServices"]>

Encodes an unknown input against a schema, returning an Effect that succeeds with the encoded value or fails with a SchemaError.

When to use

Use when you need to encode unknown input in an Effect whose failure channel is SchemaError.

Details

Prefer encodeEffect when the value is already typed as the schema's Type. Options may be provided either when creating the encoder or when applying it; application options override creation options.

Example (Encoding a value to a string)

import { Effect, Schema } from "effect"

const NumberFromString = Schema.NumberFromString

Effect.runPromise(Schema.encodeUnknownEffect(NumberFromString)(42)).then(console.log)
// Output: "42"
Source effect/Schema.ts:18379 lines
export function encodeUnknownEffect<S extends Constraint>(schema: S, options?: SchemaAST.ParseOptions) {
  const parser = SchemaParser.encodeUnknownEffect(schema, options)
  return (
    input: unknown,
    options?: SchemaAST.ParseOptions
  ): Effect.Effect<S["Encoded"], SchemaError, S["EncodingServices"]> => {
    return InternalSchema.fromIssueEffect(parser(input, options))
  }
}
Referenced by 3 symbols