@diister/valibot-mock

中文文档
0.1.11发布于 2026/10/14MITTypeScriptES Module构建来源已验证近 30 天下载 2631

Generate mock data from Valibot schemas

原始使用文档
文档目录22 节
  1. ✨ Features
  2. 📋 Supported types
  3. Primitive types
  4. Collection types
  5. Choice types
  6. Modifier types
  7. Special types
  8. 🚀 Installation
  9. 💡 Usage
  10. Basic example
  11. Advanced Usage
  12. Custom Data Generation with fake()
  13. Fake Generator Function
  14. Combining with Locale Support
  15. 📖 API
  16. createMockGenerator<T>(schema, options?)
  17. The resolve hook
  18. Semantic field names
  19. fake(generatorFn)
  20. Exported locales
  21. 🔧 Development
  22. 📄 License

@diister/valibot-mock

Mock data generator for Valibot schemas

This package allows you to automatically generate valid test data from Valibot schemas using Faker.js to create realistic values.

✨ Features

  • 🎯 Automatic generation: Creates valid data based on your Valibot schemas
  • 🌍 Multi-language support: Uses Faker.js with locale support
  • 🔧 Configurable: Customizable options (seed, attempts, sizes)
  • 📦 Extensive type support: Wide support for Valibot types (primitives, objects, arrays, unions, etc.)
  • ⚡ Performant: Built-in validation with automatic retry

📋 Supported types

Primitive types

  • string, number, boolean, date, bigint
  • Pipe support: minLength, maxLength, regex, min, max, integer, isoTimestamp

Collection types

  • array, object, record, map, set, tuple

Choice types

  • union, enum, picklist, literal, variant

Modifier types

  • optional, nullable, nullish, nonNullable, nonNullish, nonOptional
  • brand - for branded/tagged types

Special types

  • lazy, intersect, custom, unknown, any
  • null, undefined, void, never
  • file, blob

🚀 Installation

valibot is a peer dependency: the generator reads and validates the very schema objects you hand it, so it has to use your copy of valibot rather than bundle a second one.

# Bun / npm / pnpm / yarn
bun add @diister/valibot-mock valibot

# Deno
deno add jsr:@diister/valibot-mock npm:valibot
import { createMockGenerator } from "@diister/valibot-mock";
import * as v from "valibot";

Published to npm as @diister/valibot-mock and to JSR under the same name; the two carry the same code. Runs on Bun, Deno and Node.js — the Node floor is ^20.19 || ^22.13 || ^23.5 || >=24, inherited from @faker-js/faker.

💡 Usage

Basic example

import * as v from "valibot";
import { createMockGenerator } from "@diister/valibot-mock";

const UserSchema = v.object({
  id: v.pipe(v.string(), v.regex(/^user:[A-Z0-9]{16}$/)),
  email: v.pipe(v.string(), v.email()),
  firstName: v.optional(v.pipe(v.string(), v.minLength(1), v.maxLength(32))),
  lastName: v.optional(v.pipe(v.string(), v.minLength(1), v.maxLength(32))),
  age: v.pipe(v.number(), v.integer(), v.min(18), v.max(120)),
  status: v.enum(["active", "inactive", "pending"]),
  roles: v.array(v.string()),
  createdAt: v.pipe(v.string(), v.isoTimestamp())
});

const generator = createMockGenerator(UserSchema);

// Generate a single user
const mockUser = generator.generate();
console.log(mockUser);

// Generate multiple users
const mockUsers = generator.generateMany(5);
console.log(mockUsers);

Advanced Usage

Custom Data Generation with fake()

For more realistic data generation, you can use the fake() function to provide custom generators:

import * as v from "valibot";
import { createMockGenerator, fake } from "@diister/valibot-mock";

const UserSchema = v.object({
  // Use fake() for custom realistic data generation
  firstName: v.pipe(
    v.string(),
    v.minLength(2),
    fake((faker, context) => {
      console.log(`Generating firstName at ${context.path}`);
      return faker.person.firstName();
    })
  ),
  lastName: v.pipe(
    v.string(),
    fake((faker) => faker.person.lastName())
  ),
  email: v.pipe(
    v.string(),
    v.email(),
    fake((faker) => faker.internet.email())
  ),
  // Fields without fake() use intelligent defaults
  age: v.pipe(
    v.number(),
    v.minValue(18),
    v.maxValue(80)
  ),
  bio: v.optional(v.pipe(
    v.string(),
    v.maxLength(500),
    fake((faker) => faker.person.bio())
  ))
});

const mockGenerator = createMockGenerator(UserSchema);
const userData = mockGenerator.generate();

// Output:
// {
//   firstName: "John",
//   lastName: "Doe", 
//   email: "[email protected]",
//   age: 25.4,
//   bio: "Software engineer and coffee enthusiast"
// }

Fake Generator Function

The fake() function accepts a generator function with two parameters:

fake((faker, context) => {
  // faker: Faker.js instance with full API
  // context: { path: string } - current field path for debugging
  return faker.datatype.uuid();
})

Combining with Locale Support

import { createMockGenerator, locales } from "@diister/valibot-mock";

const generator = createMockGenerator(UserSchema, {
  faker: { 
    locale: [locales.fr], 
    seed: 42 
  },
  maxAttempts: 15, // Number of attempts to generate a valid value
  defaultArrayMaxLength: 5, // Max size for arrays by default
  defaultStringMaxLength: 100 // Max size for strings by default
});

📖 API

createMockGenerator<T>(schema, options?)

Creates a mock generator for a given schema.

Parameters

  • schema: The Valibot schema to use
  • options?: Configuration options

Options

interface MockGeneratorOptions {
  faker?: {
    locale?: LocaleDefinition | LocaleDefinition[];  // use the exported locales
    seed?: number;                   // Seed for reproducible results
    randomizer?: Randomizer;
  };
  maxAttempts?: number;             // Max attempts per value (default: 100)
  maxRegexAttempts?: number;        // Max draws for a regex-backed string (default: 50)
  defaultArrayMaxLength?: number;   // Max array size (default: 10)
  defaultStringMaxLength?: number;  // Max string size (default: 20)

  // Realistic values for recognised field names — see "Semantic field names".
  // `false` disables the tier; a record extends or overrides the built-in table.
  semantics?: Record<string, SemanticGenerator> | false;

  // Caller-side hook consulted at every node — see "The resolve hook".
  resolve?: (node: ResolveNode) => unknown;
}

Returns

A MockGenerator object with methods:

  • generate(): Generate a single value
  • generateMany(count): Generate multiple values

Generation consults three tiers, in this order: resolve, then fake() attached to the schema, then semantics.

Failures

When no valid value can be produced, generate() throws a MockGenerationError (exported) carrying the node's path, its schemaType and, for a regex-backed string, its pattern. Nothing is printed to the console.

A regex-backed string is drawn from the pattern with its quantifiers bounded so that the result fits maxLength (nested repetitions such as /^\/(?:[^/]+\/)*$/ included). Each draw is checked against the pattern; when maxRegexAttempts draws all miss (a lookaround or another construct the generator cannot honour), or when the pattern cannot fit maxLength at all, generation fails at once instead of retrying the whole value. Give such a field a fake() or answer it from resolve.

The resolve hook

resolve is called for every node at any depth, and outranks both fake() and semantics. Return SKIP to decline a node and let normal generation continue; anything else is taken as the value, validated like a generated one, and its subtree is not visited.

import { createMockGenerator, SKIP } from "@diister/valibot-mock";

const gen = createMockGenerator(OrderSchema, {
  resolve: (node) => (node.path === "customer.id" ? "cus_fixed" : SKIP),
});

node.path is dot-separated from the root ("" at the root) and includes array indices, e.g. "items.0.sku".

Semantic field names

Plain strings are generated from their field name when it is recognised, so a firstname entry yields a name rather than random characters. Keys are normalised — first_name, First-Name and firstName all match firstname.

import {
  createMockGenerator,
  DEFAULT_SEMANTICS,
  normalizeSemanticKey,
} from "@diister/valibot-mock";

normalizeSemanticKey("First-Name"); // "firstname"
Object.keys(DEFAULT_SEMANTICS);     // the built-in table

// Extend or override it; pass `false` to switch the tier off entirely.
createMockGenerator(schema, {
  semantics: { sku: (faker) => faker.string.alphanumeric(8).toUpperCase() },
});

fake(generatorFn)

Creates custom data generators for more realistic mock data.

Parameters

  • generatorFn: Function (faker, context) => any
    • faker: Full Faker.js instance
    • context: Object with path property for current field location

Example

fake((faker, context) => {
  // Use any Faker.js method
  return faker.person.firstName();
})

Exported locales

import { locales } from "@diister/valibot-mock";

// Available locales
locales.en    // English
locales.fr    // French
locales.de    // German
locales.es    // Spanish
locales.it    // Italian
locales.ja    // Japanese
locales.ko    // Korean
locales.pt    // Portuguese
locales.ru    // Russian
locales.zh    // Chinese
// ... and many more

🔧 Development

This package is under active development. The API may evolve before version 1.0.

📄 License

MIT

版本与发布记录

@diister/valibot-mock 共发布 4 个版本,首次发布于 2026/09/09;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.11latest2026/10/1429 天前
0.1.102026/10/081 个月前
0.1.92026/09/092 个月前
0.1.82026/09/092 个月前

关于 @diister/valibot-mock 的常见问题

@diister/valibot-mock 是什么?

Generate mock data from Valibot schemas

如何安装 @diister/valibot-mock?

在项目目录运行 npm install @diister/valibot-mock,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:valibot(^1.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@diister/valibot-mock 支持 TypeScript 吗?

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

@diister/valibot-mock 支持 ESM 还是 CommonJS?

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

@diister/valibot-mock 需要什么版本的 Node.js?

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

@diister/valibot-mock 可以商用吗?

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

@diister/valibot-mock 的下载量有多少?

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

@diister/valibot-mock 还在维护吗?

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