@dotcom-tool-kit/validated

中文文档
2.0.0发布于 2025/10/31ISCCommonJS近 30 天下载 2605

Types and functions for an errors-as-first-class-values pattern

原始使用文档
文档目录6 节
  1. Rationale
  2. Usage
  3. Creating a Validated
  4. Using Validated values
  5. Extracting values from a Validated
  6. Grouping an array of Validated

@dotcom-tool-kit/validated

Types and functions for an errors-as-first-class-values pattern

Rationale

In Tool Kit, we handle loading many plugins with different types of entry point (for Tasks, Hooks etc) in parallel. There are various points this process could go wrong: importing an entry point, checking version compatibility, and more.

If we naïvely used exception throwing or Promise rejections, Tool Kit would stop at the first problem that happened; the user would fix that only to run into the next error, which would be frustrating. We want to get as far as we can and collect all the errors that happen to present once, as late as possible.

We could still use exceptions/rejections for this, along with Promise.allSettled. However, with that pattern it would be difficult to follow the data flow of the errors, and easy to accidentally forget to collate some errors, falling into the same issue of throwing one error at a time.

Instead, we model these errors using an explicit return value, Validated<T>. This type is a union of Valid<T> which represents a correct value, and Invalid which represents a problem with a list of string reasons. By making this an explicit type, we clearly mark all the functions that can return errors we may want to collate, and make it impossible to use the values without deciding what to do with the errors.

This pattern is modelled after Go's error handling using multiple return values, Rust's Result<T, E> type and Haskell's Either a b type.

Usage

Creating a Validated

@dotcom-tool-kit/validated exports two constructor functions, valid and invalid. Use these to create a wrapper when the status of the value is known, e.g.:

valid({ some: 'object' })

invalid([
    'list of problems'
])

Using Validated values

Validated objects have methods that are similar to the Array or Promise methods, allowing you to do things with the values or error reasons without having to inspect the types:

validated.map(value => {
    /* do something with value */
    // `return` something else. replaces the value with this return value if valid, does nothing if invalid
})

validated.mapError(reasons => {
    /* do something with reasons */
    // `return` something else. replaces the reasons with this return value if invalid, does nothing if valid
})

Extracting values from a Validated

Validated has an .unwrap method that allows you to extract its value if valid, or throws an error with its reasons if invalid. This should be used as late as possible, when you can't get any further without the value, and need to stop if it's invalid.

validated.unwrap('something was invalid!')

Grouping an array of Validated

Similar to Promise.all, if you have multiple Validateds and you need to group their values and reasons (i.e. you have Array<Validated<T>> and you want Validated<Array<T>>), we have reduceValidated:

reduceValidated([
    validated1,
    validated2,
    validated3
])

This is used in Tool Kit when e.g. we load multiple plugins in parallel.

版本与发布记录

@dotcom-tool-kit/validated 共发布 6 个版本,首次发布于 2024/04/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0latest2025/10/311 年前
1.0.32025/05/061.5 年前
1.0.22025/01/031.9 年前
1.0.12024/12/091.9 年前
1.0.02024/09/102 年前
2.0.0-beta.0prerelease2024/04/302 年前

关于 @dotcom-tool-kit/validated 的常见问题

@dotcom-tool-kit/validated 是什么?

Types and functions for an errors-as-first-class-values pattern

如何安装 @dotcom-tool-kit/validated?

在项目目录运行 npm install @dotcom-tool-kit/validated,或使用 pnpm add / yarn add / bun add。

@dotcom-tool-kit/validated 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/dotcom-tool-kit__validated 是否存在。

@dotcom-tool-kit/validated 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@dotcom-tool-kit/validated 需要什么版本的 Node.js?

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

@dotcom-tool-kit/validated 可以商用吗?

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

@dotcom-tool-kit/validated 的下载量有多少?

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

@dotcom-tool-kit/validated 还在维护吗?

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