@ghostry/fabricator-extension-faker-v10

中文文档
0.0.3发布于 2026/11/09MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2044

@faker-js/faker integration for @ghostry/fabricator — faker's data generators, seeded and reproducible through the fabricator instance that calls them.

原始使用文档
文档目录6 节
  1. Install
  2. Example
  3. Every builder is a real kind, never opaque
  4. Three deviations from faker's own API
  5. use — for what the mirror doesn't cover
  6. Notes
fabricator

@ghostry/fabricator-extension-faker-v10

Seeded faker generators for @ghostry/fabricator.

npm npmx jsr github typescript bun node

@faker-js/faker integration for @ghostry/fabricator. Faker's generators draw from the same stream as every other builder in a schema, so T.faker.person.fullName() replays exactly like T.string does — nothing additional to track, no drift between runs.

Targets @faker-js/faker 10.x specifically — hence the -v10 suffix, matching the adapter packages' convention. A future faker major is a sibling package with its own peer range, not a version bump of this one.

Install

npm install @ghostry/fabricator-extension-faker-v10 @ghostry/fabricator @faker-js/faker

@ghostry/fabricator and @faker-js/faker are peer dependencies — bring your own.

Example

fakerExtension returns a registry.extend callback, so faker's builders arrive as T.faker.* on an ordinary extended registry, alongside every core kind:

import { en } from "@faker-js/faker";
import { initialize, registry } from "@ghostry/fabricator";
import { fakerExtension } from "@ghostry/fabricator-extension-faker-v10";

const { T, Fabricator } = initialize({
  types: registry.extend(fakerExtension({ locale: en })),
});

const UserSchema = T.object({
  name: T.faker.person.fullName(),
  email: T.faker.internet.email(),
  joined: T.faker.date.past(),
  id: T.string.whereby({ length: { max: 8 } }),
});

new Fabricator(UserSchema).fabricate();

locale takes faker's own locale definitions rather than a locale name, exactly as new Faker({ locale }) does — importing the ones you use keeps the rest out of your bundle.

Faker's relative-date methods resolve against the same instance clock that T.date.past/T.date.future do — the two never disagree about what "now" is within one schema.

Every builder is a real kind, never opaque

A faker method could have been wrapped as an opaque producer, and that is the one thing this package refuses to do — an opaque schema converts to Type.Unknown() in any adapter and is invisible to combinatorial/coverage. Each builder instead returns the core kind matching the method's own return type:

faker returnsbuilder returnsvia the TypeBox adapter
string (207)T.stringType.String()
Date (7)T.dateType.Date()
a record shape (7)T.object({ ... })Type.Object({ ... })
a literal union (6)T.enum.uniform([...])Type.Union([Type.Literal(), …])
number (6)T.numberType.Number()
boolean (1)T.booleanType.Boolean()
bigint (1)T.bigintType.BigInt()
[number, number] (1)T.tuple([T.number, T.number])Type.Tuple([Type.Number(), Type.Number()])
Date[] (1)T.array(T.date)Type.Array(Type.Date())

Seventeen string methods whose output satisfies a JSON-Schema format regardless of the options passed additionally carry it — internet.email() converts to Type.String({ format: "email" }), database.mongodbObjectId() to a pattern.

The mirror is hand-written, not generated — one declaration per builder and one implementation, kept in agreement by the compiler. A compile-time assertion checks every entry's value type against faker's own declared return type, so a faker release that changes one fails tsc at that entry rather than silently emitting a wrong schema.

Three deviations from faker's own API

The mirror is not 1:1, and each departure is what makes the guarantee above possible.

helpers is absent. It is a utility belt, not a data module, and core already expresses all of it better: arrayElement is T.enum.uniform(...), arrayElements/multiple are T.array(...).whereby({ length }), maybe is T.optional/T.omittable, rangeToNumber is T.number.whereby({ min, max }). Eleven of its eighteen methods are generic and would erase to unknown. Reach the two with no core equivalent — fromRegExp and fake — through use, below.

The seven color.* methods whose return type depends on their arguments are split in two. color.rgb() returns a string or a number[] depending on options.format, which no single kind can honestly describe, so each becomes a namespace of two named builders — and there is deliberately no bare T.faker.color.rgb():

T.faker.color.rgb.text(); // T.string
T.faker.color.rgb.channels(); // T.array(T.number)
T.faker.color.rgb.channels({ includeAlpha: true });

The same applies to cmyk, hsl, hwb, lab, lch, and colorByCSSColorSpace. color's other four methods are ordinary builders.

Where faker's declared return type is narrower than its JS type, the kind narrows with it. A literal union becomes an enum, not a string: person.sexType() gives T.enum.uniform(["female", "generic", "male"]), so it converts to a union of literals and is enumerable by combinatorial/coverage rather than an unconstrained string. A fixed-arity array becomes a tuple, not an array: location.nearbyGPSCoordinate() gives T.tuple([T.number, T.number]), since no option can change its [latitude, longitude] arity — unlike the color channels above, which is why those stay T.array.

use — for what the mirror doesn't cover

use hands you the shared, stream-backed Faker inside a producer, so anything reached through it still draws from the leaf's own seeded stream. It is a plain namespace of kind-tagged forms — you say what shape comes back, and keep a real kind:

T.faker.use.string((f) => f.helpers.fromRegExp("[A-Z]{3}-[0-9]{4}"));
T.faker.use.string((f) =>
  f.helpers.fake("{{person.firstName}} {{person.lastName}}"),
);
T.faker.use.number((f) => f.helpers.rangeToNumber({ min: 1, max: 10 }));
T.faker.use.opaque((f) => f.helpers.arrayElement(["free", "pro"] as const));

use.string, .number, .date, .boolean, and .bigint stay adapter-compatible. use.opaque is the only way to get an opaque schema out of this package — honest, since it is the one case where you have told it nothing about the shape.

Notes

faker.seed(...) is inert here, by design: fabricator's seeding governs, and a second one competing for control of the same output is the bug this package exists to remove. Change the instance's clock — or its salt, if it has one — instead.

A builder called outside fabricate() throws FakerExtensionError.NoActiveScopeError — there is no active fabrication to draw from. FakerExtensionError extends core's FabricatorError, so one catch still covers both packages.

See the faker guide in the docs for the full module list and worked examples.

版本与发布记录

@ghostry/fabricator-extension-faker-v10 共发布 3 个版本,首次发布于 2026/08/24;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.0.3latest2026/11/097 天前
0.0.22026/09/162 个月前
0.0.12026/08/242 个月前

关于 @ghostry/fabricator-extension-faker-v10 的常见问题

@ghostry/fabricator-extension-faker-v10 是什么?

@faker-js/faker integration for @ghostry/fabricator — faker's data generators, seeded and reproducible through the fabricator instance that calls them.

如何安装 @ghostry/fabricator-extension-faker-v10?

在项目目录运行 npm install @ghostry/fabricator-extension-faker-v10,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@faker-js/faker(^10.5.0)、@ghostry/fabricator(>=0.0.14 <0.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ghostry/fabricator-extension-faker-v10 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ghostry/fabricator-extension-faker-v10 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ghostry/fabricator-extension-faker-v10 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ghostry/fabricator-extension-faker-v10 可以商用吗?

@ghostry/fabricator-extension-faker-v10 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ghostry/fabricator-extension-faker-v10 的下载量有多少?

根据 npm 官方下载统计,@ghostry/fabricator-extension-faker-v10 在 2026/10/17 – 2026/11/15 这 30 天内被下载 2,044 次,日均约 68 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ghostry/fabricator-extension-faker-v10 还在维护吗?

最新版本 0.0.3 发布于 2026/11/09(7 天前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/08/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。