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>β
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>β AZodResultwithsuccessflag 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 aZodResult.
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 acceptingT | 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 returningtrueif 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 acceptingT | 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 producingT & U.
Sinceβ
2.0.0