Skip to main content
The @hey-api/schemas plugin generates runtime JSON Schema objects from your OpenAPI specification. These schemas are useful for runtime validation, form generation, and documentation.

Installation

The Schemas plugin is included when you install @hey-api/openapi-ts:

Configuration

Add the plugin to your openapi-ts.config.ts:

What It Generates

The Schemas plugin generates a schemas.gen.ts file containing JavaScript objects that represent your OpenAPI schemas:

Configuration Options

type

Choose the schema type to generate.
'json' | 'form'
default:"'json'"
  • 'json' - Full JSON schemas with descriptions (default)
  • 'form' - Minimal schemas without descriptions (smaller bundle size)
Generates complete schemas with descriptions:
Use type: 'form' if you’re primarily using schemas for form validation and want to reduce bundle size by excluding descriptions.

nameBuilder

Customize the generated schema names.
string | function
default:"'{{name}}Schema'"
Pattern or function to transform schema names. The name variable is a valid JavaScript/TypeScript identifier.
If your schema name is “Foo-Bar” in the OpenAPI spec, the name parameter will be “FooBar” (a valid JavaScript identifier).

Use Cases

Runtime Validation with AJV

Form Validation with React Hook Form

API Response Validation

Dynamic Form Generation

Common Patterns

Minimal Schemas for Forms

Custom Schema Naming

Combined with Validators

Schema Structure

The generated schemas follow the JSON Schema specification and include:
  • Type definitions - type, format, enum
  • Validation constraints - required, minLength, maxLength, pattern, minimum, maximum
  • Descriptions - Schema and property descriptions (when type: 'json')
  • References - $ref for referencing other schemas
  • Composition - allOf, anyOf, oneOf for schema composition
  • Arrays - items for array item schemas
  • Objects - properties, additionalProperties for object schemas

Bundle Size Considerations

Schemas add to your bundle size. Consider these strategies:
  1. Use type: 'form' - Removes descriptions to reduce size
  2. Code splitting - Import schemas only where needed
  3. Tree shaking - Use named imports to ensure unused schemas are removed

Export Configuration

By default, schemas are not included in the entry file (index.ts). To include them:

Next Steps

Zod Validator

Generate Zod schemas for type-safe validation.

Valibot Validator

Generate Valibot schemas for lightweight validation.

SDK Plugin

Use schemas with SDK validation.

Transformers

Transform data at runtime.