plugins: [pluginFaker()]OpenAPI to Faker mock data generator
@kubb/plugin-faker
Generates a Faker.js mock-data factory for every schema in your OpenAPI spec.
- Downloads569.1K/ monthnpm, last 30 days
- GitHub stars12kubb-labs/plugins
- Unpacked size463.3kBv5.4.5 on npm
- Latest release1d agov5.4.5 · Nov 15, 2026
The Faker plugin generates one factory per schema. Each factory returns Faker.js mock data and takes partial data that overrides the generated values.
Use the factories in tests, Storybook, and local development without a running backend.
Installation
kubb add plugin-fakerbun add -d @kubb/plugin-fakerpnpm add -D @kubb/plugin-fakernpm install --save-dev @kubb/plugin-fakeryarn add -D @kubb/plugin-fakerkubb add installs the version the Kubb registry pins and adds pluginFaker() to kubb.config.ts. It needs a kubb.config.ts, so run kubb init first in a new project.
Setup
- Add
@kubb/plugin-tsfor the factory types. Generation fails without it. - Install
@faker-js/fakerv10 in the app that uses the generated code. - Install
dayjs,moment, orrandexpwhen you pick it indateParserorregexGenerator.
Options worth knowing
| Option | Default | What it does |
|---|---|---|
seed | None | Makes every run return the same data. |
locale | 'en' | Sets the Faker locale, such as 'de'. |
typeMode | 'inferred' | Types factories from the generated values, or from the declared model with 'schema'. |
dateParser | 'faker' | Formats date, time, and date-time strings with faker, dayjs, or moment. |
regexGenerator | 'faker' | Turns a pattern into a string with faker or randexp. |
Example
This config generates the factories with a fixed seed. For a Pet schema, the plugin writes createPet.ts:
import { defineConfig } from 'kubb/config'
import { pluginTs } from '@kubb/plugin-ts'
import { pluginFaker } from '@kubb/plugin-faker'
export default defineConfig({
input: './petStore.yaml',
output: { path: './src/gen' },
plugins: [
pluginTs(),
pluginFaker({ seed: [100] }),
],
})import type { Pet } from '../types/Pet'
import { fakerEN as faker } from '@faker-js/faker'
faker.seed([100])
export function createPet<TData extends Partial<Pet> = object>(data?: TData) {
const defaultFakeData = {
id: faker.number.bigInt(),
name: faker.string.alpha(),
status: faker.helpers.arrayElement(['available', 'pending', 'sold'] as Array<NonNullable<Pet>['status']>),
}
return {
...defaultFakeData,
...(data || {}),
} as unknown as Omit<typeof defaultFakeData, keyof NoInfer<TData>> & NoInfer<TData>
}Call createPet({ name: 'Rex' }) to override one field and keep the generated rest. To serve these mocks from MSW handlers, see @kubb/plugin-msw.
Dependencies
Extensions that must be in the same config as Faker.
Used by
Extensions that list Faker as a dependency.
