effect-io-ai

Package: effect
Module: Schema

Schema.decodeUnknownSync

Decodes an unknown input against a schema synchronously, returning the decoded value or throwing a SchemaError for schema mismatches.

When to use

Use when you need to validate unknown data at a synchronous boundary and want schema mismatches to throw SchemaError.

Details

For input already typed as the schema’s Encoded type use decodeSync. Only service-free schemas can be decoded synchronously. For alternatives that do not throw on schema mismatches, see decodeUnknownOption, decodeUnknownExit, or decodeUnknownEffect. Options may be provided either when creating the decoder or when applying it; application options override creation options.

Gotchas

Non-schema failures may throw a runtime failure instead of SchemaError.

Example (Decoding with a transformation schema)

import { Schema } from "effect"

const NumberFromString = Schema.NumberFromString

console.log(Schema.decodeUnknownSync(NumberFromString)("42"))
// Output: 42

Schema.decodeUnknownSync(NumberFromString)("not a number")
// throws SchemaError: NumberFromString
//   └─ Encoded side transformation failure
//      └─ NumberFromString
//         └─ Expected a numeric string, actual "not a number"

See

Signature

declare const decodeUnknownSync: <S extends ConstraintDecoder<unknown>>(schema: S, options?: SchemaAST.ParseOptions) => (input: unknown, options?: SchemaAST.ParseOptions) => S["Type"]

Source

Since v4.0.0