Skip to content
Faker logo

Faker

Officialv5.4.7

@kubb/plugin-faker

kubb add plugin-faker

Options

Pass these options to pluginFaker(). Shared options link to Shared plugin options, and this page gives only their default here.

Options overview

OptionPurposeDefault
outputWhere files are written.{ path: 'mocks', barrel: { type: 'named' } }
groupSplit output by tag or path.None
typeModeFactory types from the generated values or the declared model.'inferred'
dateParserLibrary that formats string date and time fields.'faker'
regexGeneratorLibrary that turns a regex pattern into a string.'faker'
localeFaker locale of the generated values.'en'
seedValue passed to faker.seed(...) for repeatable output.None
printerReplace the handler for a schema type.None
includeKeep only operations and schemas that match.None
excludeSkip operations and schemas that match.[]
overrideApply different options per pattern.[]
resolverCustomize generated names and file paths. See resolver members.resolverFaker
macrosRewrite AST nodes before printing.[]

Option details

typeMode

Sets the input and return types of object and intersection factories. Both modes generate the same values and shallow-merge the overrides.

Type'inferred' | 'schema'
Requiredfalse
Default'inferred'
'inferred'
Takes any Partial<Model> and keeps its literal types. Returns the generated value types merged with the override types, so optional and nullable fields keep their concrete value types.
'schema'
Takes a Partial<Model> and returns the declared model type, with its optional and nullable fields. Use it for fixtures you change after creation.
// export function createOrder<TData extends Partial<Order> = object>(data?: TData)
const order = createOrder({ quantity: 2, complete: true })
const complete: true = order.complete

// TypeScript error: false is not assignable to true.
order.complete = false

To pick a mode for some schemas only, use override. A string pattern is a regular expression, so 'Order' also matches OrderItem. This sets typeMode for Order alone:

kubb.config.ts
import { defineConfig } from 'kubb/config'
import { pluginFaker } from '@kubb/plugin-faker'
import { pluginTs } from '@kubb/plugin-ts'

export default defineConfig({
  input: './petStore.yaml',
  output: { path: './src/gen' },
  plugins: [
    pluginTs(),
    pluginFaker({
      override: [{ type: 'schemaName', pattern: '^Order$', options: { typeMode: 'schema' } }],
    }),
  ],
})

dateParser

Library that formats date, time, and date-time fields typed as strings. Install the library you pick in the app.

Type'faker' | 'dayjs' | 'moment' | string
Requiredfalse
Default'faker'
'faker'
Renders a date field as faker.date.anytime().toISOString().substring(0, 10).
'dayjs'
Renders a date field as dayjs(faker.date.anytime()).format("YYYY-MM-DD").
'moment'
Renders a date field as moment(faker.date.anytime()).format("YYYY-MM-DD").

Any other module name works when its default export has the dayjs API. Kubb imports it under its camelCased name, so 'my-date-lib' becomes myDateLib.

A value other than 'faker' fails generation when the adapter types dates as Date, such as with dateType: 'date'.

regexGenerator

Library that generates strings matching a regex pattern in the spec.

Type'faker' | 'randexp'
Requiredfalse
Default'faker'
'faker'
Writes faker.helpers.fromRegExp(/pattern/) and needs no extra dependency. Faker does not support groups, alternation (a|b), or \s.
'randexp'
Writes new RandExp(pattern).gen(), which supports a wider regex grammar. Install randexp in the app.

locale

Faker locale code. Kubb imports the matching fakerXX export from @faker-js/faker as faker, so generated values fit the target region.

Typestring
Requiredfalse
Default'en'
locale: 'de_AT'
import { fakerDE_AT as faker } from '@faker-js/faker'

seed

Kubb calls faker.seed(...) with this value at the top of each generated file, so every run returns the same data. Use it for snapshot tests. A second call to the same factory returns the next values in the sequence.

Typenumber | number[]
Requiredfalse

printer

Replaces the Faker handler for a schema type, such as 'integer' or 'date'. Each handler returns the Faker expression as a string. Inside a handler, this.transform recurses into nested nodes and this.options holds the printer options. See the printer guide.

Type{ nodes?: PrinterFakerNodes }
Requiredfalse
Map integer schemas to a float
import { pluginFaker } from '@kubb/plugin-faker'

pluginFaker({
  printer: {
    nodes: {
      integer() {
        return 'faker.number.float()'
      },
    },
  },
})

Resolver members

A resolver patch can add a Mock suffix so factories do not clash with imported types. Members you leave out of the patch keep resolverFaker. Besides the shared name, file, and imports members of resolver, resolverFaker has these:

Partial override
type ResolverFakerPatch = {
  param?: {
    name?(node: OperationNode, param: ParameterNode): string    // → 'createShowPetByIdPathPetId'
    path?(node: OperationNode, param: ParameterNode): string     // → 'createShowPetByIdPath'
    query?(node: OperationNode, param: ParameterNode): string    // → 'createListPetsQuery'
    headers?(node: OperationNode, param: ParameterNode): string  // → 'createDeletePetHeaders'
    cookies?(node: OperationNode, param: ParameterNode): string  // → 'createGetSessionCookies'
  }
  response?: {
    status?(node: OperationNode, statusCode: StatusCode): string // → 'createListPetsStatus200'
    body?(node: OperationNode): string                           // → 'createCreatePetsBody'
    response?(node: OperationNode): string                       // → 'createListPetsResponse'
    responses?(node: OperationNode): string                      // → 'createListPetsResponses'
  }
}