SchemaService
The SchemaService validates function inputs against JSON schemas at runtime. Pikku's CLI generates those schemas automatically from your TypeScript types — you don't write schemas by hand. At bootstrap, the generated schemas are compiled into the service; on every function call, input data is validated before your function runs.
You don't call the SchemaService directly — Pikku handles it internally. You just need to choose an implementation and register it in your singleton services.
Choosing an Implementation
@pikku/schema-ajv | @pikku/schema-cfworker | |
|---|---|---|
| Package | AjvSchemaService | CFWorkerSchemaService |
| Backend | Ajv + ajv-formats | @cfworker/json-schema |
| Runtime | Node.js, Bun, Deno | Cloudflare Workers, Node.js |
| Format validation | Built-in (email, date, uri, etc.) | Not included |
| Type coercion | Yes (coerceTypes: true) | No |
| Default values | Yes (useDefaults: true) | No |
| Best for | Node.js servers | Cloudflare Workers (no Node.js APIs) |
AJV Schema Service
The default choice for Node.js environments. Supports format validation, type coercion, and default value injection:
- npm
- Yarn
- pnpm
- Bun
npm install @pikku/schema-ajv
yarn add @pikku/schema-ajv
pnpm add @pikku/schema-ajv
bun add @pikku/schema-ajv
import { AjvSchemaService } from '@pikku/schema-ajv'
const singletonServices = await createSingletonServices(config, {
schemaService: new AjvSchemaService(logger),
})
AJV automatically:
- Coerces types (e.g., string
"42"→ number42) - Applies default values from your schemas
- Validates formats like
email,date-time,uri
Cloudflare Worker Schema Service
For Cloudflare Workers, which don't have access to Node.js APIs that AJV depends on:
- npm
- Yarn
- pnpm
- Bun
npm install @pikku/schema-cfworker
yarn add @pikku/schema-cfworker
pnpm add @pikku/schema-cfworker
bun add @pikku/schema-cfworker
import { CFWorkerSchemaService } from '@pikku/schema-cfworker'
const singletonServices = await createSingletonServices(config, {
schemaService: new CFWorkerSchemaService(logger),
})
CFWorkerSchemaService deep-clones schema values before compilation to avoid mutation issues with the @cfworker/json-schema validator.
Methods
compileSchema(name: string, value: any): Promise<void> | void
Compiles a schema with the provided name and value, making it available for validation.
- Parameters:
name: The unique name for the schemavalue: The schema definition or configuration to be compiled
- Returns: A promise if asynchronous, or void if synchronous
validateSchema(schema: string, data: any): Promise<void> | void
Validates data against a specified schema. Throws UnprocessableContentError if validation fails.
- Parameters:
schema: The name of the schema to validate againstdata: The data to validate against the schema
- Returns: A promise if asynchronous, or void if synchronous
- Throws:
UnprocessableContentErrorif data doesn't match schema
getSchemaNames(): Set<string>
Retrieves a set of all registered schema names.
- Returns: A set containing the names of all available schemas
getSchemaKeys(schemaName: string): string[]
Returns the top-level property keys of a compiled schema. Useful for CLI argument parsing.
- Parameters:
schemaName: The name of the compiled schema
- Returns: Array of property key names, or empty array if schema has no properties
Interface
loading...
Source
AJV Schema Service
loading...
Cloudflare Worker Schema Service
loading...