Kizuna.model

Give a Zod schema a name so the OpenAPI document and every generated client produce named types instead of inline definitions.

Give a Zod schema a name, so the OpenAPI document and every generated client emit a named type for it.

pnpm add kizunajs@beta
import { Kizuna } from 'kizunajs';

Parameters

Kizuna.model(options: ModelOptions): ZodType
OptionTypeRequiredDescription
titlestringYesThe name used in generated output (User, Event, etc.)
descriptionstringNoDescription included in the OpenAPI spec
schemaZodTypeYesThe Zod schema defining the shape and validation

Returns

The Zod schema with metadata attached, usable anywhere a Zod schema is.

Example

users.ts
import { Kizuna } from 'kizunajs';
import { z } from 'zod';

export const UserSchema = Kizuna.model({
    title: 'User',
    description: 'A user in the system',
    schema: z.object({
        id: z.string(),
        name: z.string(),
        email: z.email(),
    }),
});

Effect on generators

With Kizuna.model:

  • OpenAPI: the schema is extracted into components.schemas.User and every usage becomes a $ref reference
  • TypeScript: the schema becomes export type User in the API namespace, and every route that uses it references the name
  • Swift: an object becomes public struct User, a z.enum becomes public enum EventKind, and a z.discriminatedUnion becomes an enum with one case per branch, shared across all routes that reference it
  • Kotlin: an object becomes @Serializable data class User, a z.enum becomes enum class EventKind, and a z.discriminatedUnion becomes a sealed class, shared across all routes that reference it

Extracting TypeScript types

Export a type alongside the schema with z.infer:

types.ts
export type User = z.infer<typeof UserSchema>;

See the Routes guide for more details.