Skip to main content

MockPlugin

MockPlugin injects mock data hints (examples, x-faker annotations, defaults) into OpenAPI Schemas during the beforeValidate stage, enabling the keq mock command to generate mock data closer to real business scenarios.

Configuration

.keqrc.ts
import { defineKeqConfig } from "@keq-request/cli"
import { MockPlugin } from '@keq-request/cli/plugins'

export default defineKeqConfig({
  outdir: "./src/apis",
  modules: {
    catService: "./cat-service-swagger.json",
  },
  plugins: [
    new MockPlugin([
      {
        given: '$.components.schemas.Cat.properties.name',
        then: { faker: 'person.firstName' },
      },
      {
        given: '$.components.schemas.Cat.properties.age',
        when: { type: 'number' },
        then: { faker: 'number.int', args: [{ min: 1, max: 20 }] },
      },
      {
        given: '$.components.schemas.Cat.properties.breed',
        then: { example: 'Siamese' },
      },
    ]),
  ],
})

Rule Definition

Each rule consists of three parts: given, when (optional), and then:

FieldTypeRequiredDescription
givenstringYesJSONPath expression to locate the target Schema node
whenobjectNoMatch condition; rule applies only when the Schema satisfies this condition
when.typestring | string[]NoMatches the Schema's type field
when.formatstringNoMatches the Schema's format field
thenobjectYesMock data hint to inject, supporting three modes

Supported then Modes

  • { example: unknown } — Directly specify a fixed example value
  • { faker: string; args?: unknown } — Generate data using json-schema-faker faker methods
  • { default: unknown } — Set a default value
tip

MockPlugin only takes effect on Schema nodes that haven't explicitly defined example, examples, enum, const, or x-faker — it won't override existing values.