<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"export function function encodeUnknownEffect<
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"
encodeUnknownEffect<function (type parameter) S in encodeUnknownEffect<S extends Constraint>(schema: S, options?: SchemaAST.ParseOptions): (input: unknown, options?: SchemaAST.ParseOptions) => Effect.Effect<S["Encoded"], SchemaError, S["EncodingServices"]>S extends Constraint>(schema: S extends Constraintschema: function (type parameter) S in encodeUnknownEffect<S extends Constraint>(schema: S, options?: SchemaAST.ParseOptions): (input: unknown, options?: SchemaAST.ParseOptions) => Effect.Effect<S["Encoded"], SchemaError, S["EncodingServices"]>S, options: SchemaAST.ParseOptionsoptions?: import SchemaASTSchemaAST.ParseOptions) {
const const parser: (
input: unknown,
options?: SchemaAST.ParseOptions
) => Effect.Effect<
S["Encoded"],
SchemaIssue.Issue,
S["EncodingServices"]
>
parser = import SchemaParserSchemaParser.function encodeUnknownEffect<
S extends Schema.Constraint
>(
schema: S,
options?: SchemaAST.ParseOptions
): (
input: unknown,
options?: SchemaAST.ParseOptions
) => Effect.Effect<
S["Encoded"],
SchemaIssue.Issue,
S["EncodingServices"]
>
Creates an effectful encoder for unknown input.
When to use
Use when you need to encode untyped boundary input in an Effect whose
failure channel is SchemaIssue.Issue, while preserving service
requirements.
Details
The returned function succeeds with the schema's Encoded value or fails with a
SchemaIssue.Issue. Encoding service requirements are preserved in the returned
Effect. Parse options may be provided when creating the encoder and overridden
when applying it.
encodeUnknownEffect(schema: S extends Constraintschema, options: SchemaAST.ParseOptionsoptions)
return (
input: unknowninput: unknown,
options: SchemaAST.ParseOptionsoptions?: import SchemaASTSchemaAST.ParseOptions
): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) S in encodeUnknownEffect<S extends Constraint>(schema: S, options?: SchemaAST.ParseOptions): (input: unknown, options?: SchemaAST.ParseOptions) => Effect.Effect<S["Encoded"], SchemaError, S["EncodingServices"]>S["Encoded"], class SchemaErrorclass SchemaError {
message: string;
toString: () => string;
name: string;
stack: string;
cause: unknown;
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; <…;
toJSON: () => unknown;
_tag: Tag;
issue: SchemaIssue.Issue;
}
Error thrown (or returned as the error channel value) when schema decoding
or encoding fails.
Details
The issue field contains a structured
Issue
tree describing
every validation failure, including the path to the problematic value,
expected types, and actual values received. message renders the issue tree
as a human-readable string.
Use
isSchemaError
to narrow an unknown value to SchemaError.
Example (Catching a SchemaError)
import { Schema } from "effect"
try {
Schema.decodeUnknownSync(Schema.Number)("not a number")
} catch (err) {
if (Schema.isSchemaError(err)) {
console.log(err.message)
// Expected number, actual "not a number"
}
}
SchemaError, function (type parameter) S in encodeUnknownEffect<S extends Constraint>(schema: S, options?: SchemaAST.ParseOptions): (input: unknown, options?: SchemaAST.ParseOptions) => Effect.Effect<S["Encoded"], SchemaError, S["EncodingServices"]>S["EncodingServices"]> => {
return import InternalSchemaInternalSchema.function fromIssueEffect<A, R>(
self: Effect.Effect<A, Issue, R>
): Effect.Effect<A, SchemaError, R>
fromIssueEffect(const parser: (
input: unknown,
options?: SchemaAST.ParseOptions
) => Effect.Effect<
S["Encoded"],
SchemaIssue.Issue,
S["EncodingServices"]
>
parser(input: unknowninput, options: SchemaAST.ParseOptionsoptions))
}
}