Overview
Contract Kit provides automatic validation at runtime using your schema library of choice. All validation happens transparently, ensuring type safety without manual checks.How Validation Works
- Contract Definition: You define schemas in your contracts
- Request Validation: Server automatically validates incoming requests
- Response Validation: Optionally validate responses in development
- Type Inference: TypeScript infers types from validated data
Request Validation
Automatic Validation
When a request arrives, Contract Kit automatically validates:- Path parameters: Extracted from the URL path
- Query parameters: Parsed from the query string
- Request body: Parsed from JSON
Validation Errors
If validation fails, Contract Kit automatically returns a 400 error:Schema Validation
Basic Types
Validation Rules
Optional and Nullable
Transformations
Response Validation (Client)
Contract Kit’s client validates successful responses against your contract schemas:Error Schemas
Defining Error Responses
Using Error Schemas
Schema Libraries
Contract Kit supports any Standard Schema library:- Zod
- Valibot
- ArkType
Validation Best Practices
Validate at the contract level
Validate at the contract level
Define all validation rules in your contracts, not in your handlers.
Use specific error messages
Use specific error messages
Provide clear, actionable error messages for validation failures.
Keep schemas DRY
Keep schemas DRY
Reuse common schemas across multiple contracts.
Test edge cases
Test edge cases
Test boundary conditions and invalid inputs in your tests.
Document validation rules
Document validation rules
Use schema descriptions to document requirements for API consumers.