Skip to main content

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