@ephox/boulder

中文文档
9.0.0发布于 2026/03/31GPL-2.0-or-laterTypeScriptESM + CJS近 30 天下载 4005

Basic javascript object validation

原始使用文档
文档目录19 节
  1. Install from npm
  2. Running Tests
  3. Boulder APIs
  4. StructureSchema.valueOf(validator)
  5. StructureSchema.arrOf(schema)
  6. StructureSchema.objOf(fieldSchemas)
  7. StructureSchema.setOf(validator, schema)
  8. StructureSchema.thunkOf(description, schemaThunk)
  9. StructureSchema.thunkOf(arguments, schemaBacon)
  10. StructureSchema.asStruct(label, schema, obj)
  11. StructureSchema.asRaw(label, schema, obj)
  12. FieldSchema.field(key, newKey, presence, schema)
  13. FieldSchema.customField(newKey, instantiator)
  14. ValueType.anyValue
  15. ValueType.number
  16. ValueType.string
  17. ValueType.boolean
  18. ValueType.func
  19. ValueType.postMessageable

Description

Boulder is a project designed to provide a nice syntax for validating JavaScript objects. The purpose of it is to provide useful feedback for when a developer has not specified an object correctly. Another purpose of it is to sensibly handle defaulting of arguments and optional arguments.

The API exposed by boulder will be constantly changing, but it should always be based on structure and field schemas. Structure schemas are used to represent an entire structure of fields (e.g. number, array, object etc.). Field schemas are used to represent a single field inside an object (e.g. object.alpha). By combining them, boulder should be able to specify objects of reasonable complexity.

Installation

boulder is available as an npm package. You can install it via the npm package @ephox/boulder

Install from npm

npm install @ephox/boulder

Usage

Running Tests

boulder uses bedrock to run tests. The tests are run through the test script in the project. Specifically,

$ yarn test

Boulder APIs

ephox.boulder.api.StructureSchema

ephox.boulder.api.FieldSchema

ephox.boulder.api.ValueType

Note, there are many other APIs as well, but they tend to be convenience functions built on top of these constructors.

StructureSchema.valueOf(validator)

  • used to provide validation for any value (array, object, number etc.). The argument passed in is a validator, which will take the value as an argument and return Result.value if it should be allowed, and Result.error if it should not. Result is a data type defined by katamari.

StructureSchema.arrOf(schema)

  • used to represent that the value is an array where every item in the array matches schema.

StructureSchema.objOf(fieldSchemas)

  • used to represent an object which has fields that match fieldSchemas. Note, the object can have more fields that those defined in the schema, and if some of the field schemas are defaulted or optional, they may not be necessary.

StructureSchema.setOf(validator, schema)

  • used to represent an object where the fields match some validator, but you don't actually know their exact names. The schema is used to match the value of every field. This is useful for sitautions where a server might be responding with an object where each key matching some id of something else and isn't known in advance.

StructureSchema.thunkOf(description, schemaThunk)

  • used to represent a schema that can be calculated dynamically. This is useful for recursive schemas where a child field needs to be processed in the same way as its parent field (e.g. tree). The description is used to give a simple description of what this schema is representing, because trying to invoke it when calculating the DSL can cause an infinite loop. The schemaThunk is a function that takes no arguments, and returns the StructureSchema to use.

StructureSchema.thunkOf(arguments, schemaBacon)

  • TODO: Bacon ipsum dolor amet filet mignon beef cow shankle ham hock. Ribeye tenderloin leberkas, meatball t-bone boudin bacon doner jowl. Venison sausage tongue doner pastrami. Shankle ribeye alcatra tri-tip landjaeger. T-bone kielbasa pork belly filet mignon jerky meatloaf sirloin ground round corned beef prosciutto chicken pig venison capicola. Pork belly ball tip leberkas doner, kevin jerky turkey chicken ham bacon. Ribeye shankle short loin, pastrami pork chop filet mignon drumstick t-bone picanha.

StructureSchema.asStruct(label, schema, obj)

  • take a schema for an object and an object (obj), and return a [structified]((https://www.npmjs.com/package/@ephox/katamari) version of the object in a Result.value if it matches the schema. If it does not match, returns Result.error with the validation errors. A struct is just an object where every property is wrapped in an accessor function.

StructureSchema.asRaw(label, schema, obj)

  • take a schema for an object and an object (obj), and return a plain version of the object in a Result.value if it matches the schema. If it does not match, returns Result.error with the validation errors. This output will not be structified.

FieldSchema.field(key, newKey, presence, schema)

  • define a field for an object schema. Presence (required | defaulted | asOption | asDefaultedOption) is used to determine how to handle whether the field key is there, and schema defines the schema to match for the field's value. In the output object, newKey will be used as the field name. Note, this method has many convenience methods defined such as FieldSchema.required('key').

FieldSchema.customField(newKey, instantiator)

  • define a generated output field for an object. This has no schema requirements, but can be useful for taking a snapshot of the original object, or creating some state for each extracted version of an object, or doing custom processing.

ValueType.anyValue

  • used to represent any value (array, object, number etc.).

ValueType.number

  • used to represent a number

ValueType.string

  • used to represent a string

ValueType.boolean

  • used to represent a boolean

ValueType.func

  • used to represent a function

ValueType.postMessageable

  • used to represent a value that is structured such that it can be sent via postMessage

版本与发布记录

@ephox/boulder 共发布 79 个版本,首次发布于 2017/09/20;过去 12 个月发布了 1 个版本。

版本发布日期距今
9.0.0latest2026/03/317 个月前
7.1.62024/02/072 年前
7.1.52023/03/153 年前
7.1.32022/11/233 年前
7.1.12022/09/084 年前
7.0.32022/06/294 年前

其他发布通道:rc → 7.0.2-alpha.1。

关于 @ephox/boulder 的常见问题

@ephox/boulder 是什么?

Basic javascript object validation

如何安装 @ephox/boulder?

在项目目录运行 npm install @ephox/boulder,或使用 pnpm add / yarn add / bun add。

@ephox/boulder 支持 TypeScript 吗?

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

@ephox/boulder 支持 ESM 还是 CommonJS?

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

@ephox/boulder 可以商用吗?

@ephox/boulder 使用 GPL-2.0-or-later 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以兼容的 GPL 许可开源。仅在内部使用、不对外分发时不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@ephox/boulder 的下载量有多少?

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

@ephox/boulder 还在维护吗?

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