Skip to main content

k

const k: 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 since any always 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 since unknown always 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.

note

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 be Schema<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; }โ€‹

Validates a value against a schema.

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: <a id="parseBulk"></a>โ€‹

Optimization: Fast paths for success/error cases.

parseBulk(): <T>(schema, values, options?) => { success: true; data: T[]; } | { success: false; errors: string[]; }โ€‹

Validates an array of values against a schema.

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.


Sinceโ€‹

2.0.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);