@unthrown/standard-schema
@unthrown/standard-schema
Type Aliases
SchemaIssues
type SchemaIssues = readonly StandardSchemaV1.Issue[];Defined in: index.ts:21
The error channel both entry points produce: a schema's validation issues.
Functions
fromSchema()
function fromSchema<S>(schema): (input) => Result<InferOutput<S>, SchemaIssues>;Defined in: index.ts:51
Turn a synchronous Standard Schema into a validator returning a Result.
Type Parameters
| Type Parameter | Description |
|---|---|
S extends StandardSchemaV1<unknown, unknown> | the schema type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
schema | S | a Standard Schema validator. |
Returns
a function mapping an input to Result<Output, SchemaIssues>.
(input) => Result<InferOutput<S>, SchemaIssues>
Remarks
Validation issues are the modeled error E — Result<Output, SchemaIssues> — because a failed validation is an anticipated outcome, not a Defect. Works with any Standard Schema implementation (Zod, Valibot, ArkType, …).
A validator that throws (rather than returning issues) becomes a Defect — the same boundary behaviour as fromThrowable, so an unexpected crash never escapes as a raw exception. If the schema validates asynchronously (its validate returns a Promise), a synchronous Result cannot represent the pending work, so this throws a TypeError — a deliberate usage error; use fromSchemaAsync instead.
Example
import { fromSchema } from "@unthrown/standard-schema";
const parse = fromSchema(z.string());
parse("hi").unwrap(); // "hi"
parse(42).unwrapErr(); // the issues arrayfromSchemaAsync()
function fromSchemaAsync<S>(schema): (input) => AsyncResult<InferOutput<S>, SchemaIssues>;Defined in: index.ts:97
Turn a Standard Schema (sync or async) into a validator returning an AsyncResult.
Type Parameters
| Type Parameter | Description |
|---|---|
S extends StandardSchemaV1<unknown, unknown> | the schema type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
schema | S | a Standard Schema validator. |
Returns
a function mapping an input to AsyncResult<Output, SchemaIssues>.
(input) => AsyncResult<InferOutput<S>, SchemaIssues>
Remarks
The async counterpart of fromSchema: it awaits the schema's validate, so it accepts both synchronous and asynchronous schemas. As with every AsyncResult, the returned value never rejects — a validator that throws (rather than returning issues) becomes a Defect.
Example
import { fromSchemaAsync } from "@unthrown/standard-schema";
const parse = fromSchemaAsync(asyncSchema);
(await parse(input)).match({ ok, err, defect });