@defra/forms-model

中文文档
4.0.4发布于 2026/11/11OGL-UK-3.0TypeScriptES Module构建来源已验证近 30 天下载 4295

A hapi plugin providing the model for Defra forms

原始使用文档
文档目录15 节
  1. Features
  2. Installation
  3. Usage
  4. Form Definition
  5. Component Usage
  6. Form Validation
  7. Development
  8. Setup
  9. Common commands
  10. Testing changes in a consuming service
  11. Publishing
  12. Test Coverage
  13. JSON Schema Generation and Publishing
  14. License
  15. About the license

Defra forms model

A model and validation library for Defra forms, providing the foundation for form definitions, components, validation, and data handling.

Features

  • Form Definition - Core structures for forms, pages, components, and conditions
  • Component Library - Pre-defined form components with validation
  • JSON Schema - Ready-to-use JSON schema files for cross-platform validation
  • Conditions Engine - Logic for determining form flow and visibility
  • Type Definitions - Complete TypeScript type safety
  • Metadata Handling - Support for form metadata, versioning, and state management

Installation

npm install @defra/forms-model

Usage

Form Definition

import { createForm } from '@defra/forms-model';

const form = createForm({
  name: 'my-form',
  pages: [
    {
      path: '/start',
      title: 'Start page',
      components: [/* components */]
    }
  ],
  sections: [/* sections */]
});

Component Usage

import { ComponentType } from '@defra/forms-model';

const textField = {
  type: ComponentType.TextField,
  name: 'fullName',
  title: 'Full name',
  schema: {
    min: 2,
    max: 100
  }
};

Form Validation

JSON Schema Files

This package includes pre-generated JSON schema files for use with external JSON Schema validators/ IDEs linting etc:

import formSchema from '@defra/forms-model/schemas/form-definition-schema.json';

Available schema files include:

Form Definition Schemas
  • form-definition-schema.json - Full form definition validation
  • form-definition-v2-schema.json - Full form definition V2 validation
Component Schemas
  • component-schema.json - Component validation
  • component-schema-v2.json - Component validation (v2)
Page Schemas
  • page-schema.json - Page validation
  • page-schema-v2.json - Page validation (v2)
List Schemas
  • list-schema.json - List validation
  • list-schema-v2.json - List validation (v2)
Form Metadata Schemas
  • form-metadata-schema.json - Form metadata validation
  • form-metadata-author-schema.json - Form metadata author validation
  • form-metadata-input-schema.json - Form metadata input validation
  • form-metadata-state-schema.json - Form metadata state validation
Form Metadata Field Schemas
  • form-metadata-contact-schema.json - Contact information validation
  • form-metadata-email-schema.json - Email validation
  • form-metadata-online-schema.json - Online form metadata validation
Form Editor Schemas
  • form-editor-input-page-schema.json - Form editor input page validation
  • form-editor-input-check-answers-setting-schema.json - Check answers setting validation
  • form-editor-input-question-schema.json - Question input validation
  • form-editor-input-page-settings-schema.json - Page settings validation
Form Editor Field Schemas
  • page-type-schema.json - Page type validation
  • question-type-schema.json - Question type validation
  • question-type-full-schema.json - Full question type validation
  • written-answer-sub-schema.json - Written answer sub-schema validation
  • date-sub-schema.json - Date sub-schema validation
Form Submission Schemas
  • form-submit-payload-schema.json - Form submission payload validation
  • form-submit-record-schema.json - Form submission record validation
  • form-submit-recordset-schema.json - Form submission recordset validation
Form Manager Schemas
  • patch-page-schema.json - Patch page validation
Section Schemas
  • question-schema.json - Question validation
Validation Schemas
  • min-schema.json - Minimum value validation
  • max-schema.json - Maximum value validation
  • min-length-schema.json - Minimum length validation
  • max-length-schema.json - Maximum length validation
  • max-future-schema.json - Maximum future date validation
  • max-past-schema.json - Maximum past date validation
Common Schemas
  • search-options-schema-ui.json - Search options validation (forms-designer)
  • search-options-schema-manager.json - Search options validation (forms-manager)
  • query-options-schema-ui.json - Query options validation (forms-designer)
  • query-options-schema-manager.json - Query options validation (forms-manager)
  • pagination-options-schema.json - Pagination options validation
  • sorting-options-schema.json - Sorting options validation

Development

This package was previously developed as the model workspace of forms-designer.

Setup

nvm use
npm install
npm run build

Common commands

CommandDescription
npm run buildBuild the type declarations, JavaScript modules and JSON schemas
npm run lintRun EditorConfig, ESLint and TypeScript checks
npm run formatFormat files with Prettier
npm testRun the unit tests with coverage

Testing changes in a consuming service

To test unpublished changes in a consuming service (for example forms-designer), build this package and link it:

# In forms-model
npm run build
npm link

# In the consuming service
npm link @defra/forms-model

Run npm run build in forms-model after each change so that the consuming service picks up the new dist output. Run npm unlink @defra/forms-model && npm install in the consuming service to return to the published version.

Publishing

Merging to main with changes to src/, scripts/, babel.config.* or .browserslistrc runs the Publish workflow, which increments the patch version and publishes the package to npm.

Test Coverage

Unit test coverage threshold, code coverage below which build will fail is set by using jest config, at the moment line coverage threshold is set as 92%, see jest.config.js

JSON Schema Generation and Publishing

This package generates JSON Schema files from Joi validation schemas during the build process. These schemas are included in the published npm package and are available at runtime.

How It Works

When you run npm run build, the script scripts/generate-schemas.js is executed which:

  1. Converts Joi validation schemas to JSON Schema format
  2. Enhances schemas with titles and descriptions
  3. Writes schema files to the schemas/ directory

Known Quirks

  1. First Build Required: After a fresh clone, you must run npm run build before the schema files will be available locally.

License

THIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT LICENCE found at:

http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3

The following attribution statement MUST be cited in your products and applications when using this information. `

Contains public sector information licensed under the Open Government license v3

About the license

The Open Government Licence (OGL) was developed by the Controller of Her Majesty's Stationery Office (HMSO) to enable information providers in the public sector to license the use and re-use of their information under a common open licence.

It is designed to encourage use and re-use of information freely and flexibly, with only a few conditions.

版本与发布记录

@defra/forms-model 共发布 703 个版本,首次发布于 2024/03/18;过去 12 个月发布了 127 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.0.4latest2026/11/11今天
4.0.32026/11/092 天前
4.0.22026/11/047 天前
4.0.12026/10/2220 天前
3.0.7132026/10/1527 天前
3.0.7112026/10/011 个月前

关于 @defra/forms-model 的常见问题

@defra/forms-model 是什么?

A hapi plugin providing the model for Defra forms

如何安装 @defra/forms-model?

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

@defra/forms-model 支持 TypeScript 吗?

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

@defra/forms-model 支持 ESM 还是 CommonJS?

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

@defra/forms-model 需要什么版本的 Node.js?

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

@defra/forms-model 可以商用吗?

@defra/forms-model 使用 OGL-UK-3.0 许可。声明的许可为 OGL-UK-3.0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@defra/forms-model 的下载量有多少?

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

@defra/forms-model 还在维护吗?

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