k
constk:object
Kanon namespace object providing a single entry point for all schemas.
Similar to Zod's z object.
Type Declarationβ
string(): (message?) => StringConstraintβ
Creates a string schema.
String schema with 0 overhead after tsup + Terser build.
Usage identical to before:
- string()
- string("Custom error")
- string().minLength(5)
- string().email()
message?:
stringβ Custom error message (optional).
Returns:StringConstraintβ StringConstraint with all constraints.
Sinceβ
2.0.0
number(): (message?) => NumberConstraintβ
Creates a number schema.
Number schema with 0 overhead after tsup + Terser build.
Usage identical to before:
- number()
- number("Custom error")
- number().min(5)
- number().positive()
message?:
stringβ Custom error message (optional).
Returns:NumberConstraintβ NumberConstraint with all constraints.
Sinceβ
2.0.0
boolean(): (message?) => BooleanSchemaβ
Creates a boolean schema.
Boolean schema with 0 overhead after tsup + Terser build.
Usage identical to before:
- boolean()
- boolean("Custom error")
message?:
stringβ Custom error message (optional).
Returns:BooleanSchemaβ BooleanSchema
Sinceβ
2.0.0
date(): (message?) => DateConstraintβ
Creates a Date schema.
Date schema with 0 overhead after tsup + Terser build.
Usage identical to before:
- date()
- date("Custom error")
- date().min(new Date())
- date().max(new Date())
message?:
stringβ Custom error message (optional).
Returns:DateConstraintβ DateConstraint with all constraints.
Sinceβ
2.0.0
bigint(): (message?) => BigIntConstraintβ
Creates a bigint schema.
BigInt schema with 0 overhead after tsup + Terser build.
Usage identical to before:
- bigint()
- bigint("Custom error")
- bigint().min(5n)
- bigint().positive()
message?:
stringβ Custom error message (optional).
Returns:BigIntConstraintβ BigIntSchema with all constraints.
Sinceβ
2.0.0
symbol(): (message?) => SymbolSchemaβ
Creates a symbol schema.
Symbol schema - validates symbol values.
message?:
stringβ Custom error message (optional).
Returns:SymbolSchemaβ SymbolSchema
Sinceβ
2.0.0
int(): (message?) => IntSchemaβ
Creates an integer schema.
Integer schema - validates integer numbers.
message?:
stringβ Custom error message (optional).
Returns:IntSchemaβ IntSchema
Sinceβ
2.0.0
null(): (message?) => NullSchemaβ
Creates a null schema.
Null schema with 0 overhead after tsup + Terser build.
Usage identical to before:
- null_()
- null_("Custom error")
message?:
stringβ Custom error message (optional).
Returns:NullSchemaβ NullSchema
Sinceβ
2.0.0
undefined(): (message?) => UndefinedSchemaβ
Creates an undefined schema.
Undefined schema with 0 overhead after tsup + Terser build.
Usage identical to before:
- undefined_()
- undefined_("Custom error")
message?:
stringβ Custom error message (optional).
Returns:UndefinedSchemaβ UndefinedSchema
Sinceβ
2.0.0
void(): (message?) => VoidSchemaβ
Creates a void schema.
Void schema with 0 overhead after tsup + Terser build.
Usage identical to before:
- void_()
- void_("Custom error")
The void type accepts only undefined - used for functions with no return value.
message?:
stringβ Custom error message (optional).
Returns:VoidSchemaβ VoidSchema
Sinceβ
2.0.0
never(): (message?) => NeverSchemaβ
Creates a never schema.
Never schema - always rejects any value.
Useful for impossible branches or dead code.
message?:
stringβ Custom error message.
Returns:NeverSchemaβ NeverSchema that rejects any value
Sinceβ
2.0.0
any(): (message?) => AnySchemaβ
Creates a schema accepting any value.
Any schema - accepts any value.
The any type accepts any value without validation.
A custom message can be provided for API consistency and introspection,
but it will never be used by the validator since any always accepts all values.
message?:
stringβ Custom message (stored for API consistency and introspection, but never used by the validator sinceanyalways accepts all values)
Returns:AnySchemaβ AnySchema that accepts any value
Sinceβ
2.0.0
unknown(): (message?) => UnknownSchemaβ
Creates a schema accepting unknown values.
Unknown schema - accepts any value (type-safe variant of any).
The unknown type accepts any value without validation.
A custom message can be provided for API consistency and introspection,
but it will never be used by the validator since unknown always accepts all values.
message?:
stringβ Custom message (stored for API consistency and introspection, but never used by the validator sinceunknownalways accepts all values)
Returns:UnknownSchemaβ UnknownSchema that accepts any value
Sinceβ
2.0.0
literal(): <T>(value, message?) => LiteralSchema<T>β
Creates a literal schema for a specific value.
Literal schema - accepts only a specific value.
This is equivalent to object(entries).strict(). Use strictObject() to create
a strict object directly, or object().strict() for method chaining.
Type Parametersβ
T: T extends string | number | booleanβ
value:
Tβ The literal value to validate against.
message?:stringβ Custom error message.
Returns:LiteralSchema<T>β Schema that validates only this specific value.
Sinceβ
2.0.0
enum(): <T>(values, message?) => EnumSchema<T[number]>β
Creates a string enum schema.
Enum schema - accepts only string enumeration values
Type Parametersβ
T: T extends readonly [string, string]β
values:
Tβ Array of allowed string values
message?:stringβ Custom error message
Returns:EnumSchema<T[number]>β Schema that validates only the string enum values
Sinceβ
2.0.0
numberEnum(): <T>(values, message?) => EnumSchema<T[number]>β
Creates a number enum schema.
Number enum schema - enumeration of numbers
Type Parametersβ
T: T extends readonly [number, number]β
values:
Tβ Array of allowed number values
message?:stringβ Custom error message
Returns:EnumSchema<T[number]>β Schema that validates only the number enum values
Sinceβ
2.0.0
booleanEnum(): <T>(values, message?) => EnumSchema<T[number]>β
Creates a boolean enum schema.
Boolean enum schema - enumeration of booleans
Type Parametersβ
T: T extends readonly [boolean, boolean]β
values:
Tβ Array of allowed boolean values
message?:stringβ Custom error message
Returns:EnumSchema<T[number]>β Schema that validates only the boolean enum values
Sinceβ
2.0.0
mixedEnum(): <T>(values, message?) => EnumSchema<T[number]>β
Creates a mixed enum schema.
Mixed enum schema - enumeration of mixed types
Type Parametersβ
T: T extends readonly [EnumValue, EnumValue]β
values:
Tβ Array of allowed mixed values (string | number | boolean)
message?:stringβ Custom error message
Returns:EnumSchema<T[number]>β Schema that validates only the mixed enum values
Sinceβ
2.0.0
nativeEnum(): <T>(enumObj, message?) => NativeEnumSchema<T[keyof T], T>β
Creates a schema from a TypeScript native enum.
NativeEnum schema - validates native TypeScript enums.
Supports string, number and mixed enums:
- enum StringEnum { A = "a", B = "b" }
- enum NumericEnum { A = 0, B = 1 }
- enum MixedEnum { A = 0, B = "b" }
Type Parametersβ
T: T extends Record<string, string | number>β
enumObj:
Tβ TypeScript enum object.
message?:stringβ Custom error message.
Returns:NativeEnumSchema<T[keyof T], T>β Schema that validates native enum values.
Sinceβ
2.0.0
object(): <T>(entries, message?) => ObjectConstraint<T>β
Creates an object schema from a shape.
Object schema with composition and constraints.
Type Parametersβ
T: T extends Record<string, AnySchema>β
entries:
Tβ Object entries schema definition.
message?:stringβ Custom error message (optional).
Returns:ObjectConstraint<T>β ObjectConstraint with all constraints.
Sinceβ
2.0.0
strictObject(): <T>(entries, message?) => ObjectConstraint<T>β
Creates a strict object schema that rejects unknown keys.
Strict object schema - strictly validates defined properties.
Type Parametersβ
T: T extends Record<string, AnySchema>β
entries:
Tβ Object entries schema definition.
message?:stringβ Custom error message (optional).
Returns:ObjectConstraint<T>β ObjectConstraint with all constraints.
Sinceβ
2.0.0
looseObject(): <T>(entries, message?) => ObjectConstraint<T>β
Creates a loose object schema that allows unknown keys.
Object schema with composition and constraints.
Type Parametersβ
T: T extends Record<string, AnySchema>β
entries:
Tβ Object entries schema definition.
message?:stringβ Custom error message (optional).
Returns:ObjectConstraint<T>β ObjectConstraint with all constraints.
Sinceβ
2.0.0
array(): <ItemSchema>(item, message?) => ArrayConstraint<ItemSchema>β
Creates an array schema.
Array schema with composition and constraints.
Type Parametersβ
ItemSchema: ItemSchema extends GenericSchemaβ
item:
ItemSchemaβ Schema for array items.
message?:stringβ Custom error message (optional).
Returns:ArrayConstraint<ItemSchema>β ArrayConstraint with all constraints.
Sinceβ
2.0.0
tuple(): <T>(schemas, message?) => TupleSchema<T>β
Creates a tuple schema from a list of schemas.
Tuple schema - validates an array with specific types at each position.
Type Parametersβ
T: T extends readonly GenericSchema[]β
schemas:
Tβ Array of schemas for each tuple position.
message?:stringβ Custom error message.
Returns:TupleSchema<T>β Schema that validates a tuple with the specified types.
Sinceβ
2.0.0
tupleOf(): <S1, S2>(schema1, schema2, message?) => TupleSchema<readonly [S1, S2]>β
Creates a typed 2-element tuple.
Tuple schema with specific types for better ergonomics.
Type Parametersβ
S1: S1 extends GenericSchemaβ
S2: S2 extends GenericSchemaβ
schema1:
S1β First schema.
schema2:S2β Second schema.
message?:stringβ Custom error message (optional).
Returns:TupleSchema<readonly [S1, S2]>β TupleSchema with two elements.
Sinceβ
2.0.0
tupleOf3(): <S1, S2, S3>(schema1, schema2, schema3, message?) => TupleSchema<readonly [S1, S2, S3]>β
Creates a typed 3-element tuple.
Tuple schema with three specific types.
Type Parametersβ
S1: S1 extends GenericSchemaβ
S2: S2 extends GenericSchemaβ
S3: S3 extends GenericSchemaβ
schema1:
S1β First schema.
schema2:S2β Second schema.
schema3:S3β Third schema.
message?:stringβ Custom error message (optional).
Returns:TupleSchema<readonly [S1, S2, S3]>β TupleSchema with three elements.
Sinceβ
2.0.0
tupleOf4(): <S1, S2, S3, S4>(schema1, schema2, schema3, schema4, message?) => TupleSchema<readonly [S1, S2, S3, S4]>β
Creates a typed 4-element tuple.
Tuple schema with four specific types.
Type Parametersβ
S1: S1 extends GenericSchemaβ
S2: S2 extends GenericSchemaβ
S3: S3 extends GenericSchemaβ
S4: S4 extends GenericSchemaβ
schema1:
S1β First schema.
schema2:S2β Second schema.
schema3:S3β Third schema.
schema4:S4β Fourth schema.
message?:stringβ Custom error message (optional).
Returns:TupleSchema<readonly [S1, S2, S3, S4]>β TupleSchema with four elements.
Sinceβ
2.0.0
tupleWithRest(): <T, R>(schemas, restSchema, message?) => TupleWithRestSchema<T, R>β
Creates a tuple schema with a rest element.
Tuple schema with variable length (rest).
Type Parametersβ
T: T extends readonly GenericSchema[]β
R: R extends GenericSchemaβ
schemas:
Tβ Array of schemas for fixed tuple positions.
restSchema:Rβ Schema for rest elements.
message?:stringβ Custom error message (optional).
Returns:TupleWithRestSchema<T, R>β TupleWithRestSchema with variable length.
Sinceβ
2.0.0
record(): <KeySchema, ValueSchema>(keySchema, valueSchema, message?) => RecordSchema<KeySchema, ValueSchema>β
Creates a record schema with key and value schemas.
Record schema - validates an object with typed keys and values.
Type Parametersβ
KeySchema: KeySchema extends GenericSchemaβ
ValueSchema: ValueSchema extends GenericSchemaβ
keySchema:
KeySchemaβ Schema to validate keys (must beSchema<string>).
valueSchema:ValueSchemaβ Schema to validate values.
message?:stringβ Custom error message.
Returns:RecordSchema<KeySchema, ValueSchema>β Schema that validates an object with typed keys/values.
Sinceβ
2.0.0
map(): <KeySchema, ValueSchema>(keySchema, valueSchema, message?) => MapConstraint<KeySchema, ValueSchema>β
Creates a Map schema.
Map schema - validates a Map with typed keys and values.
Type Parametersβ
KeySchema: KeySchema extends GenericSchemaβ
ValueSchema: ValueSchema extends GenericSchemaβ
keySchema:
KeySchemaβ Schema to validate keys.
valueSchema:ValueSchemaβ Schema to validate values.
message?:stringβ Custom error message.
Returns:MapConstraint<KeySchema, ValueSchema>β Schema that validates a Map with typed keys/values and constraints.
Sinceβ
2.0.0
set(): <ItemSchema>(itemSchema, message?) => SetConstraint<ItemSchema>β
Creates a Set schema.
Set schema - validates a Set with typed elements.
Type Parametersβ
ItemSchema: ItemSchema extends GenericSchemaβ
itemSchema:
ItemSchemaβ Schema to validate each item.
message?:stringβ Custom error message.
Returns:SetConstraint<ItemSchema>β Schema that validates a Set with typed items and constraints.
Sinceβ
2.0.0
union(): <S1, S2>(schema1, schema2, message?) => UnionSchema<readonly [S1, S2]>β
Creates a union schema. Alias for unionOf.
Union schema with specific types for better ergonomics.
Type Parametersβ
S1: S1 extends GenericSchemaβ
S2: S2 extends GenericSchemaβ
schema1:
S1β First schema.
schema2:S2β Second schema.
message?:stringβ Custom error message (optional).
Returns:UnionSchema<readonly [S1, S2]>β UnionSchema with two schemas.
Sinceβ
2.0.0
unionOf(): <S1, S2>(schema1, schema2, message?) => UnionSchema<readonly [S1, S2]>β
Creates a union of 2 schemas.
Union schema with specific types for better ergonomics.
Type Parametersβ
S1: S1 extends GenericSchemaβ
S2: S2 extends GenericSchemaβ
schema1:
S1β First schema.
schema2:S2β Second schema.
message?:stringβ Custom error message (optional).
Returns:UnionSchema<readonly [S1, S2]>β UnionSchema with two schemas.
Sinceβ
2.0.0
unionOf3(): <S1, S2, S3>(schema1, schema2, schema3, message?) => UnionSchema<readonly [S1, S2, S3]>β
Creates a union of 3 schemas.
Union schema with three specific types.
Type Parametersβ
S1: S1 extends GenericSchemaβ
S2: S2 extends GenericSchemaβ
S3: S3 extends GenericSchemaβ
schema1:
S1β First schema.
schema2:S2β Second schema.
schema3:S3β Third schema.
message?:stringβ Custom error message (optional).
Returns:UnionSchema<readonly [S1, S2, S3]>β UnionSchema with three schemas.
Sinceβ
2.0.0
unionOf4(): <S1, S2, S3, S4>(schema1, schema2, schema3, schema4, message?) => UnionSchema<readonly [S1, S2, S3, S4]>β
Creates a union of 4 schemas.
Union schema with four specific types.
Type Parametersβ
S1: S1 extends GenericSchemaβ
S2: S2 extends GenericSchemaβ
S3: S3 extends GenericSchemaβ
S4: S4 extends GenericSchemaβ
schema1:
S1β First schema.
schema2:S2β Second schema.
schema3:S3β Third schema.
schema4:S4β Fourth schema.
message?:stringβ Custom error message (optional).
Returns:UnionSchema<readonly [S1, S2, S3, S4]>β UnionSchema with four schemas.
Sinceβ
2.0.0
intersection(): <S1, S2>(schema1, schema2, message?) => IntersectionSchema<readonly [S1, S2]>β
Creates an intersection of 2 schemas.
Intersection schema - validates that the value satisfies multiple schemas.
Type Parametersβ
S1: S1 extends GenericSchemaβ
S2: S2 extends GenericSchemaβ
schema1:
S1β First schema.
schema2:S2β Second schema.
message?:stringβ Custom error message.
Returns:IntersectionSchema<readonly [S1, S2]>β Schema that validates the intersection of both schemas.
Sinceβ
2.0.0
intersection3(): <S1, S2, S3>(schema1, schema2, schema3, message?) => IntersectionSchema<readonly [S1, S2, S3]>β
Creates an intersection of 3 schemas.
Intersection schema with 3 schemas.
Type Parametersβ
S1: S1 extends GenericSchemaβ
S2: S2 extends GenericSchemaβ
S3: S3 extends GenericSchemaβ
schema1:
S1β First schema.
schema2:S2β Second schema.
schema3:S3β Third schema.
message?:stringβ Custom error message (optional).
Returns:IntersectionSchema<readonly [S1, S2, S3]>β IntersectionSchema with three schemas.
Sinceβ
2.0.0
partial(): <T>(schema, message?) => PartialSchema<ObjectSchema<T>>β
Makes all properties of an object schema optional.
Partial transform - makes all properties optional.
Type Parametersβ
T: T extends Record<string, GenericSchema>β
schema:
Object schema to make partial.βObjectSchema<T> |ObjectConstraint<T> | {entries:T; }
message?:stringβ Custom error message.
Returns:PartialSchema<ObjectSchema<T>>β Schema with all properties optional.
Sinceβ
2.0.0
required(): <T>(schema, message?) => RequiredSchema<ObjectSchema<T>>β
Makes all properties of an object schema required.
Required transform - makes all properties required.
Type Parametersβ
T: T extends Record<string, GenericSchema>β
schema:
Object schema to make required.βObjectSchema<T> |ObjectConstraint<T> | {entries:T; }
message?:stringβ Custom error message.
Returns:RequiredSchema<ObjectSchema<T>>β Schema with all properties required.
Sinceβ
2.0.0
pick(): <T, K>(schema, keys, message?) => PickSchema<ObjectSchema<T>, K>β
Picks a subset of properties from an object schema.
Pick transform - selects specific properties.
Type Parametersβ
T: T extends Record<string, GenericSchema>β
K: K extends string | number | symbolβ
schema:
Source object schema.βObjectSchema<T> |ObjectConstraint<T> | {entries:T; }
keys:readonly K[]β Keys to select.
message?:stringβ Custom error message.
Returns:PickSchema<ObjectSchema<T>, K>β Schema with only selected properties.
Sinceβ
2.0.0
omit(): <T, K>(schema, keys, message?) => OmitSchema<ObjectSchema<T>, K>β
Omits a subset of properties from an object schema.
Omit transform - excludes specific properties.
Type Parametersβ
T: T extends Record<string, GenericSchema>β
K: K extends string | number | symbolβ
schema:
Source object schema.βObjectSchema<T> |ObjectConstraint<T> | {entries:T; }
keys:readonly K[]β Keys to exclude.
message?:stringβ Custom error message.
Returns:OmitSchema<ObjectSchema<T>, K>β Schema without excluded properties.
Sinceβ
2.0.0
keyof(): <T>(objectSchema, message?) => KeyofSchema<ObjectSchema<T>>β
Extracts the string keys of an object schema.
Keyof schema - validates that the value is a key of an object.
Type Parametersβ
T: T extends Record<string, GenericSchema>β
objectSchema:
Object schema whose keys to validate.βObjectSchema<T> |ObjectConstraint<T> | {entries:T; }
message?:stringβ Custom error message.
Returns:KeyofSchema<ObjectSchema<T>>β Schema that validates the object keys.
Sinceβ
2.0.0
optional(): <T>(schema) => OptionalSchema<Schema<T>>β
Wraps a schema to also accept undefined.
Makes a schema optional (accepts undefined).
Type Parametersβ
T: Tβ
schema:
Schema<T>β The schema to make optional
Returns:OptionalSchema<Schema<T>>β Schema that accepts the original type or undefined
Sinceβ
2.0.0
nullable(): <T>(schema, message?) => NullableSchema<Schema<T>>β
Wraps a schema to also accept null.
Makes a schema nullable (accepts null).
Type Parametersβ
T: Tβ
schema:
Schema<T>β The schema to make nullable
message?:stringβ Custom error message
Returns:NullableSchema<Schema<T>>β Schema that accepts the original type or null
Sinceβ
2.0.0
default(): <Inner>(schema, defaultValue, message?) => DefaultSchema<Inner>β
Provides a default value when validation fails.
Default schema - provides a default value if undefined.
Type Parametersβ
Inner: Inner extends GenericSchemaβ
schema:
Innerβ Base schema.
defaultValue:Default value or function that returns the default value.βInfer<Inner> | () =>Infer<Inner>
message?:stringβ Custom error message.
Returns:DefaultSchema<Inner>β Schema that uses the default value if undefined.
Sinceβ
2.0.0
readonly(): <Inner>(schema, message?) => ReadonlySchema<Inner>β
Marks a schema output as readonly.
Readonly schema - makes properties read-only (TypeScript semantics).
Type Parametersβ
Inner: Inner extends GenericSchemaβ
schema:
Innerβ Base schema.
message?:stringβ Custom error message.
Returns:ReadonlySchema<Inner>β Schema that marks properties as readonly.
Sinceβ
2.0.0
lazy(): <T>(getter, message?) => LazySchema<T>β
Creates a lazy schema for recursive types.
Lazy schema - allows creating recursive types by deferring evaluation.
Type Parametersβ
T: Tβ
getter:
() => Schema<T>β Function that returns the schema to evaluate.
message?:stringβ Custom error message.
Returns:LazySchema<T>β Lazy schema that evaluates the schema on demand.
Sinceβ
2.0.0
coerce: objectβ
Namespace for coercion schemas.
coerce.string(): > readonly string: (message?) => StringConstraint = coerceStringβ
Coerces input to string before validating.
Coerce String schema - converts anything to string with constraints.
message?:
stringβ Custom error message.
Returns:StringConstraintβ Schema that coerces to string with chainable constraints.
Sinceβ
2.0.0
Exampleβ
// Basic coercion
coerceString()
// With constraints
coerceString().minLength(2).maxLength(100)
coerceString().email()
coerce.number(): > readonly number: (message?) => NumberConstraint = coerceNumberβ
Coerces input to number before validating.
Coerce Number schema - converts to number with constraints.
message?:
stringβ Custom error message.
Returns:NumberConstraintβ Schema that coerces to number with chainable constraints.
Sinceβ
2.0.0
Exampleβ
// Basic coercion
coerceNumber()
// With constraints
coerceNumber().min(0).max(100)
coerceNumber().int().positive()
coerce.boolean(): > readonly boolean: (message?) => BooleanSchema = coerceBooleanβ
Coerces input to boolean before validating.
Coerce Boolean schema - converts to boolean.
message?:
stringβ Custom error message (optional).
Returns:BooleanSchemaβ Schema that coerces to boolean.
Sinceβ
2.0.0
coerce.date(): > readonly date: (message?) => DateConstraint = coerceDateβ
Coerces input to Date before validating.
Coerce Date schema - converts to Date with constraints.
message?:
stringβ Custom error message.
Returns:DateConstraintβ Schema that coerces to Date with chainable constraints.
Sinceβ
2.0.0
Exampleβ
// Basic coercion
coerceDate()
// With constraints
coerceDate().min(new Date("2020-01-01"))
coerceDate().max(new Date()).before(new Date("2030-01-01"))
coerce.bigint(): > readonly bigint: (message?) => BigIntConstraint = coerceBigIntβ
Coerces input to bigint before validating.
Coerce BigInt schema - converts values to bigint with constraints.
Handles conversion from number, string, boolean, and other types to BigInt. Returns the coerced value directly for optimal performance.
message?:
stringβ Custom error message
Returns:BigIntConstraintβ Schema that converts to bigint with chainable constraints.
Sinceβ
2.0.0
Exampleβ
// Basic coercion
coerceBigInt()
// With constraints
coerceBigInt().min(0n).max(100n)
coerceBigInt().positive()
parse(): {<T>(schema, input): { success: true; data: T; } | { success: false; error: string; }; <S>(schema, input): { success: true; data: Infer<S>; } | { success: false; error: string; }; }β
Validates a value against a schema.
Call Signature: > <T>(schema, input): { success: true; data: T; } | { success: false; error: string; }β
Core parsing logic for Kanon V3 validation system.
Type Parametersβ
T: Tβ
The expected output type of the schema.
schema:
Schema<T>β Schema to validate against.
input:unknownβ Value to validate.
Returns:{ success: true; data: T; } | { success: false; error: string; }β Result object with success flag and data or error.
Sinceβ
2.0.0
Performanceβ
Optimization: Fast paths for success/error cases.
Call Signature: > <S>(schema, input): { success: true; data: Infer<S>; } | { success: false; error: string; }β
Overload accepting a union of schemas (e.g. fields[name]).
Infers the output type from the union.
Type Parametersβ
S: S extends GenericSchemaβ
schema:
S
input:unknown
Returns:{ success: true; data: Infer<S>; } | { success: false; error: string; }
Sinceβ
2.2.0
parseBulk(): {<T>(schema, values, options?): { success: true; data: T[]; } | { success: false; errors: string[]; }; <S>(schema, values, options?): { success: true; data: Infer<S>[]; } | { success: false; errors: string[]; }; }β
Validates an array of values against a schema.
Call Signature: > <T>(schema, values, options?): { success: true; data: T[]; } | { success: false; errors: string[]; }β
Bulk validation with two modes:
- Early abort: stops at first validation failure (fastest).
- Complete: collects all errors (comprehensive).
Type Parametersβ
T: Tβ
The expected output type of the schema.
schema:
Schema<T>β Schema to validate against.
values:unknown[]β Array of values to validate.
options?:ParseBulkOptionsβ Bulk validation options.
Returns:{ success: true; data: T[]; } | { success: false; errors: string[]; }β Result object with success flag and data array or errors array.
Sinceβ
2.0.0
Performanceβ
Optimization: Pre-allocation and early abort mode.
Call Signature: > <S>(schema, values, options?): { success: true; data: Infer<S>[]; } | { success: false; errors: string[]; }β
Overload accepting a union of schemas. Infers the output type from the union.
Type Parametersβ
S: S extends GenericSchemaβ
schema:
S
values:unknown[]
options?:ParseBulkOptions
Returns:{ success: true; data: Infer<S>[]; } | { success: false; errors: string[]; }
Sinceβ
2.2.0
Noteβ
Tree-shaking warning: Importing k includes ALL Kanon schemas
in your bundle, even if you only use a subset. For optimal bundle size,
prefer direct imports.
Exampleβ
import { k } from '@kanon/helpers/k';
const userSchema = k.object({
name: k.string().minLength(1),
age: k.number().min(0),
email: k.string().email(),
});
const result = k.parse(userSchema, input);