|
1 | 1 | --- |
2 | | -title: Faker |
3 | | -description: Faker plugin for Hey API. Compatible with all our features. |
| 2 | +title: Faker Plugin |
| 3 | +description: Generate realistic mock data factories from OpenAPI with the Faker plugin for openapi-ts. Compatible with Faker v9 and v10. |
4 | 4 | --- |
5 | 5 |
|
6 | 6 | <script setup lang="ts"> |
7 | | -import FeatureStatus from '@components/FeatureStatus.vue'; |
| 7 | +import Heading from '@components/Heading.vue'; |
8 | 8 | </script> |
9 | 9 |
|
10 | | -# Faker <span data-badge>vote</span> |
11 | | - |
12 | | -<FeatureStatus issueNumber=1485 name="Faker" /> |
| 10 | +<Heading> |
| 11 | + <h1>Faker<span class="sr-only"> v9 & v10</span></h1> |
| 12 | +</Heading> |
13 | 13 |
|
14 | 14 | ### About |
15 | 15 |
|
16 | 16 | [Faker](https://fakerjs.dev) is a popular library that generates fake (but reasonable) data that can be used for things such as unit testing, performance testing, building demos, and working without a completed backend. |
17 | 17 |
|
| 18 | +The Faker plugin for Hey API generates factory functions from your OpenAPI spec that return realistic mock data using `@faker-js/faker`. |
| 19 | + |
| 20 | +## Features |
| 21 | + |
| 22 | +- Faker v9 and v10 support |
| 23 | +- seamless integration with `@hey-api/openapi-ts` ecosystem |
| 24 | +- factory functions for reusable schema definitions, operation requests, and operation responses |
| 25 | +- smart property name inference for realistic data (e.g. `email` → `faker.internet.email()`) |
| 26 | +- constraint and format awareness (min/max, string formats, array bounds) |
| 27 | +- optional property and default value handling |
| 28 | +- dependency injection for custom faker instances (locale, seed) |
| 29 | +- minimal learning curve thanks to extending the underlying technology |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +In your [configuration](/openapi-ts/get-started), add `@faker-js/faker` to your plugins and you'll be ready to generate faker factories. :tada: |
| 34 | + |
| 35 | +```js |
| 36 | +export default { |
| 37 | + input: 'hey-api/backend', // sign up at app.heyapi.dev |
| 38 | + output: 'src/client', |
| 39 | + plugins: [ |
| 40 | + // ...other plugins |
| 41 | + '@faker-js/faker', // [!code ++] |
| 42 | + ], |
| 43 | +}; |
| 44 | +``` |
| 45 | + |
| 46 | +## Output |
| 47 | + |
| 48 | +The Faker plugin will generate the following artifacts, depending on the input specification. |
| 49 | + |
| 50 | +## Definitions |
| 51 | + |
| 52 | +A factory function is generated for every reusable definition from your input. |
| 53 | + |
| 54 | +::: code-group |
| 55 | + |
| 56 | +```ts [example] |
| 57 | +import { faker, type Faker } from '@faker-js/faker'; |
| 58 | + |
| 59 | +import type { Bar, Foo } from '../types.gen'; |
| 60 | + |
| 61 | +export const fakeFoo = (options?: Options): Foo => ({ |
| 62 | + name: ensureFaker(options).string.sample(), |
| 63 | + age: ensureFaker(options).number.int({ min: 1, max: 120 }), |
| 64 | +}); |
| 65 | + |
| 66 | +export const fakeBar = (options?: Options): Bar => |
| 67 | + ensureFaker(options).helpers.arrayElement(['baz', 'qux']); |
| 68 | +``` |
| 69 | + |
| 70 | +```js [config] |
| 71 | +export default { |
| 72 | + input: 'hey-api/backend', // sign up at app.heyapi.dev |
| 73 | + output: 'src/client', |
| 74 | + plugins: [ |
| 75 | + // ...other plugins |
| 76 | + { |
| 77 | + name: '@faker-js/faker', |
| 78 | + definitions: true, // [!code ++] |
| 79 | + }, |
| 80 | + ], |
| 81 | +}; |
| 82 | +``` |
| 83 | + |
| 84 | +::: |
| 85 | + |
| 86 | +You can customize the naming and casing pattern for `definitions` factories using the `.name` and `.case` options. |
| 87 | + |
| 88 | +## Requests |
| 89 | + |
| 90 | +A single factory function is generated per operation for request data. It combines the request body, path parameters, query parameters, and headers into one object. Only keys with actual data are included. |
| 91 | + |
| 92 | +::: code-group |
| 93 | + |
| 94 | +```ts [example] |
| 95 | +// body + path + query combined into one factory |
| 96 | +export const fakeUpdatePetRequest = (options?: Options): Omit<UpdatePetData, 'url'> => { |
| 97 | + const f = options?.faker ?? faker; |
| 98 | + return { |
| 99 | + body: { |
| 100 | + name: f.string.sample(), |
| 101 | + tag: fakeTag(options), |
| 102 | + }, |
| 103 | + path: { |
| 104 | + id: f.string.uuid(), |
| 105 | + }, |
| 106 | + query: { |
| 107 | + dryRun: f.datatype.boolean(), |
| 108 | + }, |
| 109 | + }; |
| 110 | +}; |
| 111 | + |
| 112 | +// body only |
| 113 | +export const fakeCreatePetRequest = (options?: Options): Omit<CreatePetData, 'url'> => { |
| 114 | + const f = options?.faker ?? faker; |
| 115 | + return { |
| 116 | + body: { |
| 117 | + name: f.string.sample(), |
| 118 | + }, |
| 119 | + }; |
| 120 | +}; |
| 121 | +``` |
| 122 | + |
| 123 | +```js [config] |
| 124 | +export default { |
| 125 | + input: 'hey-api/backend', // sign up at app.heyapi.dev |
| 126 | + output: 'src/client', |
| 127 | + plugins: [ |
| 128 | + // ...other plugins |
| 129 | + { |
| 130 | + name: '@faker-js/faker', |
| 131 | + requests: true, // [!code ++] |
| 132 | + }, |
| 133 | + ], |
| 134 | +}; |
| 135 | +``` |
| 136 | + |
| 137 | +::: |
| 138 | + |
| 139 | +You can customize the naming and casing pattern for `requests` factories using the `.name` and `.case` options. |
| 140 | + |
| 141 | +## Responses |
| 142 | + |
| 143 | +A factory function is generated for operation responses. When an operation has a single success response, the factory is unsuffixed. When multiple responses exist, each factory is suffixed with the status code. |
| 144 | + |
| 145 | +::: code-group |
| 146 | + |
| 147 | +```ts [example] |
| 148 | +// single success response — unsuffixed |
| 149 | +export const fakeCreatePetResponse = (options?: Options): CreatePetResponse => fakePet(options); |
| 150 | + |
| 151 | +// multiple responses — suffixed with status code |
| 152 | +export const fakeGetPetResponse200 = (options?: Options): GetPetResponses[200] => fakePet(options); |
| 153 | + |
| 154 | +export const fakeGetPetResponse404 = (options?: Options): GetPetErrors[404] => fakeError(options); |
| 155 | +``` |
| 156 | + |
| 157 | +```js [config] |
| 158 | +export default { |
| 159 | + input: 'hey-api/backend', // sign up at app.heyapi.dev |
| 160 | + output: 'src/client', |
| 161 | + plugins: [ |
| 162 | + // ...other plugins |
| 163 | + { |
| 164 | + name: '@faker-js/faker', |
| 165 | + responses: true, // [!code ++] |
| 166 | + }, |
| 167 | + ], |
| 168 | +}; |
| 169 | +``` |
| 170 | + |
| 171 | +::: |
| 172 | + |
| 173 | +You can customize the naming and casing pattern for `responses` factories using the `.name` and `.case` options. |
| 174 | + |
| 175 | +## Smart Inference |
| 176 | + |
| 177 | +The plugin infers semantically appropriate faker methods from property names, producing more realistic mock data without requiring explicit schema formats or annotations. |
| 178 | + |
| 179 | +```ts |
| 180 | +// property name "email" → faker.internet.email() |
| 181 | +// property name "firstName" → faker.person.firstName() |
| 182 | +// property name "city" → faker.location.city() |
| 183 | +// property name "id" → faker.string.uuid() |
| 184 | +// property name "age" → faker.number.int({ min: 1, max: 120 }) |
| 185 | +``` |
| 186 | + |
| 187 | +Ancestor context is also used for disambiguation. For example, `name` under a `User` schema produces `faker.person.fullName()`, while `name` under a `Company` schema produces `faker.company.name()`. |
| 188 | + |
| 189 | +Explicit schema annotations (format, pattern, constraints) always take priority over name-based inference. |
| 190 | + |
| 191 | +## Formats and Constraints |
| 192 | + |
| 193 | +The plugin respects OpenAPI schema formats and constraints to generate appropriately bounded data. |
| 194 | + |
| 195 | +**String formats:** |
| 196 | + |
| 197 | +- `email` → `faker.internet.email()` |
| 198 | +- `date-time` → `faker.date.recent().toISOString()` |
| 199 | +- `date` → `faker.date.recent().toISOString().slice(0, 10)` |
| 200 | +- `uuid` → `faker.string.uuid()` |
| 201 | +- `uri` / `url` → `faker.internet.url()` |
| 202 | +- `ipv4` → `faker.internet.ipv4()` |
| 203 | +- `ipv6` → `faker.internet.ipv6()` |
| 204 | +- `pattern` → `faker.helpers.fromRegExp(pattern)` |
| 205 | + |
| 206 | +**Numeric constraints:** |
| 207 | + |
| 208 | +- `minimum` / `maximum` → `faker.number.int({ min, max })` |
| 209 | +- `exclusiveMinimum` / `exclusiveMaximum` → adjusted bounds |
| 210 | + |
| 211 | +**String constraints:** |
| 212 | + |
| 213 | +- `minLength` / `maxLength` → `faker.string.alpha({ length: { min, max } })` |
| 214 | + |
| 215 | +**Array constraints:** |
| 216 | + |
| 217 | +- `minItems` / `maxItems` → controls count in `faker.helpers.multiple()` |
| 218 | + |
| 219 | +## Optional Properties |
| 220 | + |
| 221 | +Required properties are always included. Optional properties are controlled by the `includeOptional` option at runtime. |
| 222 | + |
| 223 | +```ts |
| 224 | +const user = fakeFoo(); // includes optional properties by default |
| 225 | +const user = fakeFoo({ includeOptional: false }); // excludes optional properties |
| 226 | +const user = fakeFoo({ includeOptional: 0.5 }); // 50% probability per optional property |
| 227 | +``` |
| 228 | + |
| 229 | +## Default Values |
| 230 | + |
| 231 | +When a schema defines default values, you can control whether to use them instead of generating fake data via the `useDefault` option. |
| 232 | + |
| 233 | +```ts |
| 234 | +const data = fakeFoo(); // always generates fake data |
| 235 | +const data = fakeFoo({ useDefault: true }); // uses schema defaults when available |
| 236 | +const data = fakeFoo({ useDefault: 0.5 }); // 50% probability of using defaults |
| 237 | +``` |
| 238 | + |
| 239 | +## Circular References |
| 240 | + |
| 241 | +Schemas with circular references are automatically detected, whether self-referencing (a schema that refers to itself) or mutually recursive (two or more schemas that refer to each other). The generated factories use a depth counter to prevent infinite recursion, returning empty arrays or omitting optional properties once the limit is reached. |
| 242 | + |
| 243 | +The maximum depth defaults to `10` and can be configured via the `maxCallDepth` option. |
| 244 | + |
| 245 | +## Locale |
| 246 | + |
| 247 | +You can configure the locale for generated faker factories. When set, the generated code imports the faker instance from the locale-specific build of `@faker-js/faker`. |
| 248 | + |
| 249 | +::: code-group |
| 250 | + |
| 251 | +```ts [output] |
| 252 | +import { faker } from '@faker-js/faker/locale/de'; |
| 253 | +import type { Faker } from '@faker-js/faker'; |
| 254 | +``` |
| 255 | + |
| 256 | +```js [config] |
| 257 | +export default { |
| 258 | + input: 'hey-api/backend', // sign up at app.heyapi.dev |
| 259 | + output: 'src/client', |
| 260 | + plugins: [ |
| 261 | + // ...other plugins |
| 262 | + { |
| 263 | + name: '@faker-js/faker', |
| 264 | + locale: 'de', // [!code ++] |
| 265 | + }, |
| 266 | + ], |
| 267 | +}; |
| 268 | +``` |
| 269 | + |
| 270 | +::: |
| 271 | + |
| 272 | +See the [Faker localization guide](https://fakerjs.dev/guide/localization) for available locales. |
| 273 | + |
| 274 | +## Deterministic Results |
| 275 | + |
| 276 | +For snapshot testing or reproducible output, you can seed the faker instance and fix the reference date to ensure deterministic results. |
| 277 | + |
| 278 | +```ts |
| 279 | +import { faker } from '@faker-js/faker'; |
| 280 | + |
| 281 | +faker.seed(42); |
| 282 | +faker.setDefaultRefDate('2026-01-01T00:00:00.000Z'); |
| 283 | + |
| 284 | +const data = fakeFoo(); |
| 285 | +// always returns the same output |
| 286 | +``` |
| 287 | + |
| 288 | +## Custom Faker Instance |
| 289 | + |
| 290 | +You can pass your own [faker instance](https://fakerjs.dev/api/faker.html) via the `faker` option for runtime locale switching or other customizations. |
| 291 | + |
| 292 | +```ts |
| 293 | +import { faker } from '@faker-js/faker/locale/de'; |
| 294 | + |
| 295 | +const data = fakeFoo({ faker }); |
| 296 | +``` |
| 297 | + |
| 298 | +## API |
| 299 | + |
| 300 | +You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/@faker-js/faker/types.ts) interface. |
| 301 | + |
18 | 302 | <!--@include: ../../partials/sponsors.md--> |
0 commit comments