Schema definition type
Schema definition object
The same schema definition with preserved types
import { schema, t } from 'kitojs';
const userSchema = schema({
params: t.object({
id: t.str().uuid()
}),
query: t.object({
limit: t.num().min(1).max(100).default(10)
}),
body: t.object({
name: t.str().min(1),
email: t.str().email()
}),
headers: t.object({
authorization: t.str()
})
});
app.post('/users/:id', ({ req, res }) => {
// All request parts are type-safe and validated
const id = req.params.id; // string (UUID validated)
const limit = req.query.limit; // number
const name = req.body.name; // string
}, userSchema);
Converts a JSON Schema definition to internal schema format
JSON Schema definition type
JSON Schema definition object
Converted schema definition with preserved types
import { schema } from 'kitojs';
const userSchema = schema.json({
params: {
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' }
},
required: ['id']
},
body: {
type: 'object',
properties: {
name: { type: 'string', minLength: 1 },
email: { type: 'string', format: 'email' }
},
required: ['name', 'email']
}
});
app.post('/users/:id', ({ req, res }) => {
// types are inferred from JSON Schema
const id = req.params.id; // string
const name = req.body.name; // string
}, userSchema);
Creates a typed schema definition for request validation. This is a type helper that preserves schema structure for type inference.