Skip to content

@unthrown/standard-schema


@unthrown/standard-schema

Type Aliases

SchemaIssues

ts
type SchemaIssues = readonly StandardSchemaV1.Issue[];

Defined in: index.ts:21

The error channel both entry points produce: a schema's validation issues.

Functions

fromSchema()

ts
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 ParameterDescription
S extends StandardSchemaV1<unknown, unknown>the schema type.

Parameters

ParameterTypeDescription
schemaSa 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 EResult<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

ts
import { fromSchema } from "@unthrown/standard-schema";
const parse = fromSchema(z.string());
parse("hi").unwrap(); // "hi"
parse(42).unwrapErr(); // the issues array

fromSchemaAsync()

ts
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 ParameterDescription
S extends StandardSchemaV1<unknown, unknown>the schema type.

Parameters

ParameterTypeDescription
schemaSa 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

ts
import { fromSchemaAsync } from "@unthrown/standard-schema";
const parse = fromSchemaAsync(asyncSchema);
(await parse(input)).match({ ok, err, defect });

Released under the MIT License.