middlewareEncoding<S, RE>Intercepts the encoding pipeline of a schema.
Details
The provided function receives the current encoding Effect and ParseOptions,
and returns a new Effect — potentially adding service requirements (RE),
recovering from errors, or augmenting the result.
Example (Logging encode failures)
import { Effect, Schema } from "effect"
const Logged = Schema.String.pipe(
Schema.middlewareEncoding((effect) =>
Effect.tapError(effect, (issue) => Effect.log("encode failed", issue))
)
)export interface interface middlewareEncoding<S extends Constraint, RE>Intercepts the encoding pipeline of a schema.
Details
The provided function receives the current encoding Effect and ParseOptions,
and returns a new Effect — potentially adding service requirements (RE),
recovering from errors, or augmenting the result.
Example (Logging encode failures)
import { Effect, Schema } from "effect"
const Logged = Schema.String.pipe(
Schema.middlewareEncoding((effect) =>
Effect.tapError(effect, (issue) => Effect.log("encode failed", issue))
)
)
Type-level representation returned by
middlewareEncoding
.
middlewareEncoding<function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S extends Constraint, function (type parameter) RE in middlewareEncoding<S extends Constraint, RE>RE> extends
interface BottomLazy<out Ast extends SchemaAST.AST, out Rebuild extends Top, in out TypeParameters extends ReadonlyArray<Constraint> = readonly [], out TypeMutability extends Mutability = "readonly", out TypeOptionality extends Optionality = "required", out TypeConstructorDefault extends ConstructorDefault = "no-default", out EncodedMutability extends Mutability = "readonly", out EncodedOptionality extends Optionality = "required">Lazy Bottom variant for schema implementations that compute their public
views on demand.
When to use
Use as an implementation base for schema interfaces that must expose
Bottom behavior without forcing TypeScript to eagerly evaluate expensive
Type, Encoded, or service views.
Details
The laziness is purely type-level; runtime behavior is unchanged.
BottomLazy keeps the structural operations inherited from Bottom, but
erases the expensive schema views to unknown. Concrete schema interfaces can
then redeclare the precise views they expose. This keeps wide schemas such as
Struct and Union cheaper when generic code reads a single view, while
preserving their exact public types.
BottomLazy<
function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S["ast"],
interface middlewareEncoding<S extends Constraint, RE>Intercepts the encoding pipeline of a schema.
Details
The provided function receives the current encoding Effect and ParseOptions,
and returns a new Effect — potentially adding service requirements (RE),
recovering from errors, or augmenting the result.
Example (Logging encode failures)
import { Effect, Schema } from "effect"
const Logged = Schema.String.pipe(
Schema.middlewareEncoding((effect) =>
Effect.tapError(effect, (issue) => Effect.log("encode failed", issue))
)
)
Type-level representation returned by
middlewareEncoding
.
middlewareEncoding<function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S, function (type parameter) RE in middlewareEncoding<S extends Constraint, RE>RE>,
function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S["~type.parameters"],
function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S["~type.mutability"],
function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S["~type.optionality"],
function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S["~type.constructor.default"],
function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S["~encoded.mutability"],
function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S["~encoded.optionality"]
>
{
readonly "Type": function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S["Type"]
readonly "Encoded": function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S["Encoded"]
readonly "DecodingServices": function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S["DecodingServices"]
readonly "EncodingServices": function (type parameter) RE in middlewareEncoding<S extends Constraint, RE>RE
readonly "~type.make.in": function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S["~type.make.in"]
readonly "~type.make": function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S["~type.make"]
readonly "Iso": function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S["Iso"]
readonly middlewareEncoding<S extends Constraint, RE>.schema: S extends Constraintschema: function (type parameter) S in middlewareEncoding<S extends Constraint, RE>S
}
/**
* Intercepts the encoding pipeline of a schema.
*
* **Details**
*
* The provided function receives the current encoding `Effect` and `ParseOptions`,
* and returns a new `Effect` — potentially adding service requirements (`RE`),
* recovering from errors, or augmenting the result.
*
* **Example** (Logging encode failures)
*
* ```ts
* import { Effect, Schema } from "effect"
*
* const Logged = Schema.String.pipe(
* Schema.middlewareEncoding((effect) =>
* Effect.tapError(effect, (issue) => Effect.log("encode failed", issue))
* )
* )
* ```
*
* @see {@link catchEncoding} for a simpler error-recovery variant
* @category encoding
* @since 4.0.0
*/
export function function middlewareEncoding<
S extends Constraint,
RE
>(
encode: (
effect: Effect.Effect<
Option_.Option<S["Encoded"]>,
SchemaIssue.Issue,
S["EncodingServices"]
>,
options: SchemaAST.ParseOptions
) => Effect.Effect<
Option_.Option<S["Encoded"]>,
SchemaIssue.Issue,
RE
>
): (schema: S) => middlewareEncoding<S, RE>
Intercepts the encoding pipeline of a schema.
Details
The provided function receives the current encoding Effect and ParseOptions,
and returns a new Effect — potentially adding service requirements (RE),
recovering from errors, or augmenting the result.
Example (Logging encode failures)
import { Effect, Schema } from "effect"
const Logged = Schema.String.pipe(
Schema.middlewareEncoding((effect) =>
Effect.tapError(effect, (issue) => Effect.log("encode failed", issue))
)
)
middlewareEncoding<function (type parameter) S in middlewareEncoding<S extends Constraint, RE>(encode: (effect: Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, S["EncodingServices"]>, options: SchemaAST.ParseOptions) => Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, RE>): (schema: S) => middlewareEncoding<S, RE>S extends Constraint, function (type parameter) RE in middlewareEncoding<S extends Constraint, RE>(encode: (effect: Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, S["EncodingServices"]>, options: SchemaAST.ParseOptions) => Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, RE>): (schema: S) => middlewareEncoding<S, RE>RE>(
encode: (
effect: Effect.Effect<
Option_.Option<S["Encoded"]>,
SchemaIssue.Issue,
S["EncodingServices"]
>,
options: SchemaAST.ParseOptions
) => Effect.Effect<
Option_.Option<S["Encoded"]>,
SchemaIssue.Issue,
RE
>
encode: (
effect: Effect.Effect<
Option_.Option<S["Encoded"]>,
SchemaIssue.Issue,
S["EncodingServices"]
>
(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect: import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<import Option_Option_.type Option_.Option = /*unresolved*/ anyOption<function (type parameter) S in middlewareEncoding<S extends Constraint, RE>(encode: (effect: Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, S["EncodingServices"]>, options: SchemaAST.ParseOptions) => Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, RE>): (schema: S) => middlewareEncoding<S, RE>S["Encoded"]>, import SchemaIssueSchemaIssue.type Issue =
| SchemaIssue.Leaf
| SchemaIssue.Filter
| SchemaIssue.Encoding
| SchemaIssue.Pointer
| SchemaIssue.Composite
| SchemaIssue.AnyOf
The root discriminated union of all validation error nodes.
When to use
Use when typing the error channel in Effect<A, Issue, R> results from
schema parsing, or when writing custom formatters or issue-tree walkers.
Details
Every node has a _tag field for pattern-matching. The union includes both
terminal
Leaf
types and composite types that wrap inner issues:
Filter
,
Encoding
,
Pointer
,
Composite
,
AnyOf
. All Issue instances have a toString() that delegates to
the default formatter, so String(issue) produces a human-readable message.
Issue, function (type parameter) S in middlewareEncoding<S extends Constraint, RE>(encode: (effect: Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, S["EncodingServices"]>, options: SchemaAST.ParseOptions) => Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, RE>): (schema: S) => middlewareEncoding<S, RE>S["EncodingServices"]>,
options: SchemaAST.ParseOptions(parameter) options: {
errors: "first" | "all" | undefined;
onExcessProperty: "ignore" | "error" | "preserve" | undefined;
propertyOrder: "none" | "original" | undefined;
disableChecks: boolean | undefined;
concurrency: number | "unbounded" | undefined;
}
options: import SchemaASTSchemaAST.ParseOptions
) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<import Option_Option_.type Option_.Option = /*unresolved*/ anyOption<function (type parameter) S in middlewareEncoding<S extends Constraint, RE>(encode: (effect: Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, S["EncodingServices"]>, options: SchemaAST.ParseOptions) => Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, RE>): (schema: S) => middlewareEncoding<S, RE>S["Encoded"]>, import SchemaIssueSchemaIssue.type Issue =
| SchemaIssue.Leaf
| SchemaIssue.Filter
| SchemaIssue.Encoding
| SchemaIssue.Pointer
| SchemaIssue.Composite
| SchemaIssue.AnyOf
The root discriminated union of all validation error nodes.
When to use
Use when typing the error channel in Effect<A, Issue, R> results from
schema parsing, or when writing custom formatters or issue-tree walkers.
Details
Every node has a _tag field for pattern-matching. The union includes both
terminal
Leaf
types and composite types that wrap inner issues:
Filter
,
Encoding
,
Pointer
,
Composite
,
AnyOf
. All Issue instances have a toString() that delegates to
the default formatter, so String(issue) produces a human-readable message.
Issue, function (type parameter) RE in middlewareEncoding<S extends Constraint, RE>(encode: (effect: Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, S["EncodingServices"]>, options: SchemaAST.ParseOptions) => Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, RE>): (schema: S) => middlewareEncoding<S, RE>RE>
) {
return (schema: S extends Constraintschema: function (type parameter) S in middlewareEncoding<S extends Constraint, RE>(encode: (effect: Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, S["EncodingServices"]>, options: SchemaAST.ParseOptions) => Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, RE>): (schema: S) => middlewareEncoding<S, RE>S): interface middlewareEncoding<S extends Constraint, RE>Intercepts the encoding pipeline of a schema.
Details
The provided function receives the current encoding Effect and ParseOptions,
and returns a new Effect — potentially adding service requirements (RE),
recovering from errors, or augmenting the result.
Example (Logging encode failures)
import { Effect, Schema } from "effect"
const Logged = Schema.String.pipe(
Schema.middlewareEncoding((effect) =>
Effect.tapError(effect, (issue) => Effect.log("encode failed", issue))
)
)
Type-level representation returned by
middlewareEncoding
.
middlewareEncoding<function (type parameter) S in middlewareEncoding<S extends Constraint, RE>(encode: (effect: Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, S["EncodingServices"]>, options: SchemaAST.ParseOptions) => Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, RE>): (schema: S) => middlewareEncoding<S, RE>S, function (type parameter) RE in middlewareEncoding<S extends Constraint, RE>(encode: (effect: Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, S["EncodingServices"]>, options: SchemaAST.ParseOptions) => Effect.Effect<Option_.Option<S["Encoded"]>, SchemaIssue.Issue, RE>): (schema: S) => middlewareEncoding<S, RE>RE> =>
const make: <S extends Constraint>(
ast: S["ast"],
options?: object
) => S
Creates a schema from an AST (Abstract Syntax Tree) node.
Details
This is the fundamental constructor for all schemas in the Effect Schema
library. It takes an AST node and wraps it in a fully-typed schema that
preserves all type information and provides the complete schema API.
The make function is used internally to create all primitive schemas like
String, Number, Boolean, etc., as well as more complex schemas. It's
the bridge between the untyped AST representation and the strongly-typed
schema.
make(
import SchemaASTSchemaAST.function middlewareEncoding(
ast: AST,
middleware: SchemaTransformation.Middleware<
any,
any,
any,
any,
any,
any
>
): AST
middlewareEncoding(schema: S extends Constraintschema.Constraint["ast"]: SchemaAST.ASTast, new import SchemaTransformationSchemaTransformation.constructor Middleware<any, any, any, any, any, any>(decode: (effect: Effect.Effect<StandardJSONSchemaV1<any>, SchemaIssue.Issue, any>, options: SchemaAST.ParseOptions) => Effect.Effect<StandardJSONSchemaV1<any>, SchemaIssue.Issue, any>, encode: (effect: Effect.Effect<StandardJSONSchemaV1<any>, SchemaIssue.Issue, any>, options: SchemaAST.ParseOptions) => Effect.Effect<StandardJSONSchemaV1<any>, SchemaIssue.Issue, any>): SchemaTransformation.Middleware<...>Middleware that wraps the entire parsing Effect pipeline for both
decode and encode directions.
When to use
Use when you need a schema middleware to catch or recover from parsing
errors (e.g. Schema.catchDecoding), run side effects around the parsing
pipeline, or access the full Effect rather than a single decoded value.
Details
Unlike Transformation, which operates on individual values via Getter,
Middleware receives the full Effect produced by the inner schema and can
intercept, modify, retry, or replace it.
decode receives an Effect<Option<E>, Issue, RDE> and returns
Effect<Option<T>, Issue, RDT>.
encode receives an Effect<Option<T>, Issue, RET> and returns
Effect<Option<E>, Issue, REE>.
flip() swaps the decode and encode functions, producing a
Middleware<E, T, ...>.
Typically constructed indirectly via Schema.middlewareDecoding or
Schema.middlewareEncoding rather than instantiating this class directly.
Example (Creating a middleware that falls back on decode failure)
import { Effect, Option, SchemaTransformation } from "effect"
const fallback = new SchemaTransformation.Middleware(
(effect) => Effect.catch(effect, () => Effect.succeed(Option.some("fallback"))),
(effect) => effect
)
Middleware(import identityidentity, encode: (
effect: Effect.Effect<
Option_.Option<S["Encoded"]>,
SchemaIssue.Issue,
S["EncodingServices"]
>,
options: SchemaAST.ParseOptions
) => Effect.Effect<
Option_.Option<S["Encoded"]>,
SchemaIssue.Issue,
RE
>
encode)),
{ schema: S extends Constraintschema }
)
}