<S extends ConstraintEncoder<unknown>>(
schema: S,
options?: SchemaAST.ParseOptions
): (
input: unknown,
options?: SchemaAST.ParseOptions
) => Result_.Result<S["Encoded"], SchemaError>Encodes an unknown input against a schema, returning a Result that
succeeds with the encoded value or fails with a SchemaError for schema
mismatches.
When to use
Use when you do not know the input type statically and want schema mismatches
returned as Result.fail with SchemaError.
Details
For values already typed as the schema's Type use encodeResult.
Options may be provided either when creating the encoder or when applying it;
application options override creation options.
Schema mismatches are returned as Result.fail with SchemaError.
Gotchas
Only causes made entirely of schema issues are returned as Result.fail.
Causes that contain defects, interruptions, or other non-schema reasons throw
instead.
export function function encodeUnknownResult<
S extends ConstraintEncoder<unknown>
>(
schema: S,
options?: SchemaAST.ParseOptions
): (
input: unknown,
options?: SchemaAST.ParseOptions
) => Result_.Result<S["Encoded"], SchemaError>
Encodes an unknown input against a schema, returning a Result that
succeeds with the encoded value or fails with a
SchemaError
for schema
mismatches.
When to use
Use when you do not know the input type statically and want schema mismatches
returned as Result.fail with SchemaError.
Details
For values already typed as the schema's Type use
encodeResult
.
Options may be provided either when creating the encoder or when applying it;
application options override creation options.
Schema mismatches are returned as Result.fail with SchemaError.
Gotchas
Only causes made entirely of schema issues are returned as Result.fail.
Causes that contain defects, interruptions, or other non-schema reasons throw
instead.
encodeUnknownResult<function (type parameter) S in encodeUnknownResult<S extends ConstraintEncoder<unknown>>(schema: S, options?: SchemaAST.ParseOptions): (input: unknown, options?: SchemaAST.ParseOptions) => Result_.Result<S["Encoded"], SchemaError>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 encodeUnknownResult<S extends ConstraintEncoder<unknown>>(schema: S, options?: SchemaAST.ParseOptions): (input: unknown, options?: SchemaAST.ParseOptions) => Result_.Result<S["Encoded"], SchemaError>S, options: SchemaAST.ParseOptionsoptions?: import SchemaASTSchemaAST.ParseOptions) {
const const parser: (
input: unknown,
options?: SchemaAST.ParseOptions
) => Result_.Result<
S["Encoded"],
SchemaIssue.Issue
>
parser = import SchemaParserSchemaParser.function encodeUnknownResult<
S extends Schema.ConstraintEncoder<unknown>
>(
schema: S,
options?: SchemaAST.ParseOptions
): (
input: unknown,
options?: SchemaAST.ParseOptions
) => Result.Result<
S["Encoded"],
SchemaIssue.Issue
>
Creates an encoder for unknown input that reports failure safely as a
Result.
When to use
Use when encoding values from an unknown or dynamically typed boundary
synchronously, and you want SchemaIssue.Issue failures returned as Result
data.
Details
The returned function produces Result.succeed with the schema's Encoded
value on success or Result.fail with a SchemaIssue.Issue on encoding
failure.
Gotchas
This adapter runs synchronously. Causes made entirely of schema issues become
Result.fail, but causes that contain defects, interruptions, or asynchronous
work at this synchronous boundary throw instead.
encodeUnknownResult(schema: S extends ConstraintEncoder<unknown>schema, options: SchemaAST.ParseOptionsoptions)
return (input: unknowninput: unknown, options: SchemaAST.ParseOptionsoptions?: import SchemaASTSchemaAST.ParseOptions): import Result_Result_.type Result_.Result = /*unresolved*/ anyResult<function (type parameter) S in encodeUnknownResult<S extends ConstraintEncoder<unknown>>(schema: S, options?: SchemaAST.ParseOptions): (input: unknown, options?: SchemaAST.ParseOptions) => Result_.Result<S["Encoded"], SchemaError>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> => {
return import Result_Result_.mapError(const parser: (
input: unknown,
options?: SchemaAST.ParseOptions
) => Result_.Result<
S["Encoded"],
SchemaIssue.Issue
>
parser(input: unknowninput, options: SchemaAST.ParseOptionsoptions), (issue: SchemaIssue.Issueissue) => new new SchemaError(issue: SchemaIssue.Issue): SchemaErrorError 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(issue: SchemaIssue.Issueissue))
}
}