<S extends ConstraintEncoder<unknown>>(
schema: S,
options?: SchemaAST.ParseOptions
): (
input: unknown,
options?: SchemaAST.ParseOptions
) => Promise<S["Encoded"]>Encodes an unknown input against a schema, returning a Promise that
resolves with the encoded value or rejects with a SchemaError for
schema mismatches.
When to use
Use when you need encoding of unknown input to return a JavaScript Promise
that rejects with SchemaError for schema mismatches.
Details
For values already typed as the schema's Type use encodePromise.
Options may be provided either when creating the encoder or when applying it;
application options override creation options.
Gotchas
Non-schema failures may reject with a runtime failure instead of
SchemaError.
export function function encodeUnknownPromise<
S extends ConstraintEncoder<unknown>
>(
schema: S,
options?: SchemaAST.ParseOptions
): (
input: unknown,
options?: SchemaAST.ParseOptions
) => Promise<S["Encoded"]>
Encodes an unknown input against a schema, returning a Promise that
resolves with the encoded value or rejects with a
SchemaError
for
schema mismatches.
When to use
Use when you need encoding of unknown input to return a JavaScript Promise
that rejects with SchemaError for schema mismatches.
Details
For values already typed as the schema's Type use
encodePromise
.
Options may be provided either when creating the encoder or when applying it;
application options override creation options.
Gotchas
Non-schema failures may reject with a runtime failure instead of
SchemaError.
encodeUnknownPromise<function (type parameter) S in encodeUnknownPromise<S extends ConstraintEncoder<unknown>>(schema: S, options?: SchemaAST.ParseOptions): (input: unknown, options?: SchemaAST.ParseOptions) => Promise<S["Encoded"]>S extends interface ConstraintEncoder<out E, out RE = never>Lightweight structural constraint for APIs that need encoder type views but
do not need the full schema protocol.
When to use
Use when you need to preserve a schema's encoded type and encoding services,
but the API does not constrain the decoded type, decoding services, or call
schema methods such as annotate, check, rebuild, make, or
makeEffect.
ConstraintEncoder<unknown>>(
schema: S extends ConstraintEncoder<unknown>schema: function (type parameter) S in encodeUnknownPromise<S extends ConstraintEncoder<unknown>>(schema: S, options?: SchemaAST.ParseOptions): (input: unknown, options?: SchemaAST.ParseOptions) => Promise<S["Encoded"]>S,
options: SchemaAST.ParseOptionsoptions?: import SchemaASTSchemaAST.ParseOptions
) {
const const parser: (
input: unknown,
options?: SchemaAST.ParseOptions
) => Effect.Effect<
S["Encoded"],
SchemaError,
S["EncodingServices"]
>
parser = 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(schema: S extends ConstraintEncoder<unknown>schema, options: SchemaAST.ParseOptionsoptions)
return (input: unknowninput: unknown, options: SchemaAST.ParseOptionsoptions?: import SchemaASTSchemaAST.ParseOptions): interface Promise<T>Represents the completion of an asynchronous operation
Promise<function (type parameter) S in encodeUnknownPromise<S extends ConstraintEncoder<unknown>>(schema: S, options?: SchemaAST.ParseOptions): (input: unknown, options?: SchemaAST.ParseOptions) => Promise<S["Encoded"]>S["Encoded"]> => {
return function runSchemaErrorPromise<A>(
self: Effect.Effect<A, SchemaError>
): Promise<A>
runSchemaErrorPromise(const parser: (
input: unknown,
options?: SchemaAST.ParseOptions
) => Effect.Effect<
S["Encoded"],
SchemaError,
S["EncodingServices"]
>
parser(input: unknowninput, options: SchemaAST.ParseOptionsoptions))
}
}