@bluprynt/forms-core

中文文档
4.2.0发布于 2026/10/21TypeScriptESM + CJS零依赖近 30 天下载 2.1万

Framework-agnostic engine for JSON-driven dynamic forms. Given a form schema (JSON), the engine handles field visibility via conditional logic, validates user input against type-specific rules, and produces structured form documents (JSON).

原始使用文档
文档目录6 节
  1. Key Capabilities
  2. Quick Start
  3. Building a Form Definition
  4. Editing Form Values
  5. Documentation
  6. Supported Field Types

@bluprynt/forms-core

Framework-agnostic engine for JSON-driven dynamic forms. Given a form schema (JSON), the engine handles field visibility via conditional logic, validates user input against type-specific rules, and produces structured form documents (JSON).

Key Capabilities

  • Schema compilation — validates and compiles a JSON form definition into a runtime engine with dependency analysis and cycle detection.
  • Conditional visibility — shows/hides fields and sections dynamically based on other field values, with cascading parent-child visibility and topological evaluation order.
  • Type-aware validation — validates form values against per-field rules (required, min/max, pattern, date ranges, array constraints, etc.), skipping hidden fields automatically.
  • Document compatibility validation — verifies that a form document's form.id and form.version match the engine's definition before field validation.
  • Form definition editing — fluent builder API (FormDefinitionEditor) for programmatically constructing and modifying form schemas.
  • Form values editing — fluent editor API (FormValuesEditor) for programmatically reading and writing form values against a definition, with array manipulation, validation, and visibility queries.
  • Relative dates — date boundaries like "+7d" or "-1m" are resolved at evaluation time against the form's required submittedAt timestamp, enabling dynamic yet reproducible date constraints.

Quick Start

import { FormEngine } from '@bluprynt/forms-core'

// 1. Compile the form definition
const engine = new FormEngine(formDefinition)

// 2. Create a form document (with optional initial values)
const doc = engine.createFormDocument({ "1": "Alice", "2": 30 })

// 3. Compute visibility
const visibility = engine.getVisibilityMap(doc)

// 4. Validate (checks document compatibility + field rules)
const result = engine.validate(doc)
if (!result.valid) {
  result.documentErrors?.forEach(e => console.log(`Document: ${e.message}`))
  for (const [fieldId, errs] of result.fieldErrors) {
    for (const err of errs) {
      console.log(`Field ${fieldId}: ${err.message}`)
    }
  }
}

Building a Form Definition

import { FormDefinitionEditor } from '@bluprynt/forms-core'

const editor = new FormDefinitionEditor({
  id: 'my-form', version: '1.0.0', title: 'My Form', content: [],
})

editor
  .addField({ type: 'string', label: 'Name', validation: { required: true } })
  .addSection({ type: 'section', title: 'Details' })
  .addField({ type: 'number', label: 'Age' }, 2) // into section id=2

const definition = editor.toJSON()

Editing Form Values

import { FormValuesEditor } from '@bluprynt/forms-core'

// 1. Create an editor (optionally pre-populate from an existing document)
const editor = new FormValuesEditor(formDefinition)

// 2. Set field values (fluent chaining)
editor
  .setFieldValue(1, 'Alice')
  .setFieldValue(2, 30)
  .addArrayItem(6, 'TypeScript')
  .addArrayItem(6, 'React')
  .setSubmittedAt('2025-01-01T00:00:00Z')

// 3. Optionally record and resolve machine-proposed answers
editor
  .setSuggestion(1, { value: 'Alice Smith', confidence: 0.8, source: 'Passport MRZ, line 2' })
  .acceptSuggestion(1)   // copies the value across, keeps the suggestion
  .rejectSuggestion(2)   // removes a suggestion, leaves the value untouched

// 4. Validate and extract the document
const result = editor.validate()
const doc = editor.toJSON()

Suggestions live in the document's optional suggestions block and never affect validation or visibility. confidence is a 0..1 fraction. Both the block and the three metadata properties (placeholder, answer_guidelines, reference_id) on fields and sections are optional, so existing definitions and documents keep working unchanged.

Documentation

DocumentDescription
ArchitectureProject structure, module dependency graph, key design patterns
API ReferenceComplete public API — classes, methods, types, error handling
Validation & ConditionsHow validation rules work per field type, condition operators, visibility resolution
Schema Issue CodesAll DocumentError error codes, validation stages, error handling
Development GuideHow to build, test, lint, and extend the engine (e.g., adding new field types)

For the complete list of exported classes, types, and utilities, see API Reference.

Supported Field Types

TypeValueDescription
stringstringFree-text input
numbernumberNumeric input
booleanbooleanTrue/false toggle
datestring (ISO 8601)Date picker
selectstring | numberSingle selection from predefined options
arrayT[]Ordered list of scalar values
fileFileValue objectFile upload metadata (name, mimeType, size, url)

版本与发布记录

@bluprynt/forms-core 共发布 6 个版本,首次发布于 2026/03/20;过去 12 个月发布了 6 个版本。

版本发布日期距今
4.2.0latest2026/10/2117 天前
4.1.02026/09/221 个月前
4.0.12026/06/294 个月前
3.0.02026/03/247 个月前
2.0.02026/03/247 个月前
1.0.02026/03/207 个月前

关于 @bluprynt/forms-core 的常见问题

@bluprynt/forms-core 是什么?

Framework-agnostic engine for JSON-driven dynamic forms. Given a form schema (JSON), the engine handles field visibility via conditional logic, validates user input against type-specific rules, and produces structured form documents (JSON).

如何安装 @bluprynt/forms-core?

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

@bluprynt/forms-core 支持 TypeScript 吗?

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

@bluprynt/forms-core 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bluprynt/forms-core 可以商用吗?

@bluprynt/forms-core 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@bluprynt/forms-core 的下载量有多少?

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

@bluprynt/forms-core 还在维护吗?

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