API Overview
This reference documents the complete public API of Valchecker. Each validation step is designed to be composable, type-safe, and runtime-focused.
Import Strategies
Valchecker provides two ways to import validation steps:
All Steps (Convenience)
ts
import { allSteps, createValchecker } from 'valchecker'
const v = createValchecker({ steps: allSteps })Bundles every built-in step into your valchecker instance. Best for:
- Rapid prototyping
- CLI tools
- Applications where bundle size isn't critical
Selective Imports (Tree-Shaking)
ts
import { createValchecker, number, object, string } from 'valchecker'
const v = createValchecker({ steps: [string, number, object] })Import only what you need for optimal bundle size. Recommended for:
- Production web applications
- Libraries shipped to npm
- Any size-sensitive deployment
Complete Step Reference
All 46 built-in steps, organized by category:
Primitive Type Validators
string()- Validates string typenumber()- Validates finite number typeboolean()- Validates boolean typebigint()- Validates bigint typesymbol()- Validates symbol typeliteral(value)- Validates exact value matchunknown()- Passthrough validator (accepts any)any()- Passthrough withanytypenever()- Always fails validation
Nullish Type Validators
null_()- Validates nullundefined_()- Validates undefined
Structure Validators
object(shape)- Validates object with specific properties (unknown keys allowed)strictObject(shape)- Validates object and rejects unknown keyslooseObject(shape)- Alias forobject()(explicitly allows unknown keys)array(elementSchema)- Validates each array elementunion(schemas)- Tries each schema, returns first successintersection(schemas)- Merges multiple object schemasinstance(constructor)- Validates class instance
Constraint Validators
min(value)- Validates minimum (for number, bigint, or length)max(value)- Validates maximum (for number, bigint, or length)integer()- Validates integer (no decimals)empty()- Validates empty (length === 0)startsWith(prefix)- Validates string prefixendsWith(suffix)- Validates string suffix
Data Transformation Steps
transform(fn)- Custom transformation functiontoTrimmed()- Trim whitespace from both endstoTrimmedStart()- Trim whitespace from starttoTrimmedEnd()- Trim whitespace from endtoUppercase()- Convert to uppercasetoLowercase()- Convert to lowercasetoString()- Convert to stringtoSorted(fn?)- Sort arraytoFiltered(predicate)- Filter array elementstoSliced(start, end?)- Slice arraytoSplitted(separator)- Split string into arraytoLength()- Replace array with its lengthtoAsync()- Force async operation modeparseJSON()- Parse JSON string to valuestringifyJSON()- Stringify value to JSONjson()- Validate JSON string (no parse)
Flow Control Steps
check(predicate)- Custom validation checkfallback(getValue)- Provide default on failureuse(schema)- Delegate to another schemaas<T>()- Type cast (runtime no-op)generic<T>(factory)- Recursive schema support
Loose Variants
looseNumber()- Coerce strings to numberslooseObject(shape)- Object with unknown keys allowed
Execution API
Result Type
Every schema returns results of this shape:
ts
type ExecutionResult<T>
= | { value: T }
| { issues: ExecutionIssue[] }
interface ExecutionIssue {
code: string // e.g., 'string:expected_string'
message: string // Human-readable error
path: PropertyKey[] // Location in data: ['user', 'email']
payload: unknown // Issue-specific data
}Execution Methods
ts
// Sync when possible, async if needed
const result = schema.execute(input)
// Returns: ExecutionResult<T> | Promise<ExecutionResult<T>>
// Always async
const result = await schema.execute(input)
// Returns: Promise<ExecutionResult<T>>API Categories
Navigate to specific category pages for detailed documentation:
- Primitives - Base type validators
- Structures - Compound type validators
- Transforms - Data reshaping steps
- Helpers - Flow control and utilities
Method Chaining
All steps support method chaining to build complex pipelines:
ts
const schema = v.string()
.toTrimmed() // Transform
.check(s => s.length > 0, 'Required') // Validate
.toLowercase() // Transform
.check(s => /^[a-z]+$/.test(s)) // ValidateStandard Schema Compliance
Valchecker implements Standard Schema V1, enabling interoperability with compatible libraries and tools.