@ bluprynt/ 形式- 核心
JSON驱动的动态形式的框架不可知性引擎. Given a form schema (JSON),引擎通过条件逻辑处理字段能见度,根据特定类型的规则验证用户输入,并生成结构化的形式文档(JSON).
密钥能力
- Schema 汇编 ——通过依赖性分析和循环检测,验证JSON形式定义并将其编成运行时引擎.
- 有条件的能见度 ——以其他字段值为基础动态地显示/hides字段和分段,并带有串连的亲子能见度和地形评价顺序.
- 类型意识验证 ——对照每个字段的规则(要求,分/最大,图案,日期范围,数组限制等)验证形成值,自动跳过隐藏字段.
- 文档兼容性验证 - 核实表格文件的
form.id财务报告和审定财务报表form.version在字段验证前匹配引擎定义. - 窗体定义编辑 ——流利建设API(
FormDefinitionEditor用于方案构建和修改形式方案. - 窗体值编辑 ——流利编辑 API (
FormValuesEditor)用于对定义进行程序读取和书写形式值,同时进行阵列操纵,验证和能见度查询. - 相对日期 ——日期界限如:
"+7d"或者说"-1m"在评估时按所需表格解决submittedAt时间戳,允许动态但可复制的日期限制.
快速启动
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}`)
}
}
}
建立形式定义
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()
编辑窗体值
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 块,绝不影响验证或
可见度. confidence 是一个 0..1 分数 块和三个元数据属性
(单位:千美元)placeholder, (中文(简体) ), answer_guidelines, (中文(简体) ), reference_id)关于字段和部分是可选的,因此存在
定义和文件保持不变.
文档
| 文档 | 说明 |
|---|---|
| 建筑 | 项目结构、模块依赖图、关键设计模式 |
| API 参考 | 完整的公共 API——类,方法,类型,错误处理 |
| 验证条件( C) | 每个字段类型、条件操作符、可见度分辨率的验证规则如何工作 |
| Schema 问题代码 | 全体 DocumentError 错误代码、 验证阶段、 错误处理 |
| 发展指南 | 如何构建、测试、涂料和扩展引擎(例如添加新的字段类型) |
出口类别、类型和公用事业的完整清单见 API 参考。 。 。 .
支持的外地类型
| 类型 | 数值 | 说明 |
|---|---|---|
string | string | 文本输入 |
number | number | 数字输入 |
boolean | boolean | 真假切换 |
date | string (ISO 8601) (中文(简体) ) | 选取日期者 |
select | string | number | 从预定义选项中选择 |
array | T[] | 按顺序排列的平面值列表 |
file | FileValue 对象 | 文件上传元数据( 名称、 mimeType、 大小、 url) |