Aller au contenu principal

asZod()

asZod<T>(schema): Adapter<T>

Opt-in Zod v4-style facade around a Kanon schema.

Does not mutate the original schema; tree-shakable and minimal.


Type Parameters

T: T

The output type of the wrapped schema.


Parameters

schema: Schema<T>

The Kanon schema to wrap.


Returns: Adapter<T>

A Zod-compatible adapter with safeParse, parse, and other Zod methods.


Since

2.0.0

Adapter<T>

Type

Adapter<T> = object

Zod v4-compatible adapter wrapping a Kanon schema.

Exposes parsing, wrapper, refinement, and operator methods that mirror the Zod API surface.


Since

2.0.0


Type Parameters

T: T

The output type produced by this adapter.


Methods

parse()

Parses a value and returns the result or throws on failure.

value: unknown — The value to validate.
Returns: T — The validated and possibly coerced value.

Since

2.0.0

safeParse()

Parses a value and returns a discriminated result without throwing.

value: unknown — The value to validate.
Returns: ZodResult<T> — A ZodResult with success flag and data or error.

Since

2.0.0

parseAsync()

Async variant of parse.

value: unknown — The value to validate.
Returns: Promise<T> — Promise resolving to the validated value.

Throws

Zod-compatible error object when validation fails.

Since

2.0.0

safeParseAsync()

Async variant of safeParse.

value: unknown — The value to validate.
Returns: Promise<ZodResult<T>> — Promise resolving to a ZodResult.

Since

2.0.0

optional()

Wraps the schema to also accept undefined.

Returns: Adapter<T | undefined>

A new adapter accepting T | undefined.

Since

2.0.0

nullable()

Wraps the schema to also accept null.

message?: string — Optional custom error message.
Returns: Adapter<T | null> — A new adapter accepting T | null.

Since

2.0.0

nullish()

Wraps the schema to also accept null and undefined.

Returns: Adapter<T | null | undefined>

A new adapter accepting T | null | undefined.

Since

2.0.0

default()

Provides a default value when parsing fails.

value: Static value or factory function.T | () => T
Returns: Adapter<T> — A new adapter that falls back to the default.

Since

2.0.0

catch()

Catches validation errors and returns a fallback value.

value: Static value or factory function.T | () => T
Returns: Adapter<T> — A new adapter that catches errors.

Since

2.0.0

array()

Wraps the schema into an array schema.

Returns: Adapter<T[]>

A new adapter validating T[].

Since

2.0.0

readonly()

Marks the schema output as readonly.

Returns: Adapter<T>

A new adapter with readonly semantics.

Since

2.0.0

refine()

Adds a refinement check to the adapter.

check: (value) => boolean — Predicate returning true if the value is valid.
message?: string — Optional error message on failure.
Returns: Adapter<T> — A new adapter with the refinement applied.

Since

2.0.0

superRefine()

Adds a super-refinement with context for complex validations.

fn: SuperRefineFn<T> — Refinement function receiving the value and a context.
Returns: Adapter<T> — A new adapter with the super-refinement applied.

Since

2.0.0

transform()

Applies a transform to the validated value.

Type Parameters

NewOut: NewOut

The output type after transformation.

fn: (value) => NewOut — Transform function.
Returns: Adapter<NewOut> — A new adapter producing the transformed type.

Since

2.0.0

or()

Creates a union of this adapter with another.

Type Parameters

U: U

The output type of the other adapter.

other: Adapter<U> — The adapter to union with.
Returns: Adapter<T | U> — A new adapter accepting T | U.

Since

2.0.0

and()

Creates an intersection of this adapter with another.

Type Parameters

U: U

The output type of the other adapter.

other: Adapter<U> — The adapter to intersect with.
Returns: Adapter<T & U> — A new adapter producing T & U.

Since

2.0.0