@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 youropenapi-ts.config.ts:
What It Generates
The Schemas plugin generates aschemas.gen.ts file containing JavaScript objects that represent your OpenAPI schemas:
Configuration Options
type
Choose the schema type to generate.
'json'- Full JSON schemas with descriptions (default)'form'- Minimal schemas without descriptions (smaller bundle size)
- JSON (default)
- Form
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.
Pattern or function to transform schema names. The
name variable is a valid JavaScript/TypeScript identifier.- String Pattern
- Function
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 -
$reffor referencing other schemas - Composition -
allOf,anyOf,oneOffor schema composition - Arrays -
itemsfor array item schemas - Objects -
properties,additionalPropertiesfor object schemas
Bundle Size Considerations
- Use
type: 'form'- Removes descriptions to reduce size - Code splitting - Import schemas only where needed
- 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.