# @darkobits/valida 中文文档

规范地址：https://npmcn.com/package/%40darkobits/valida/

版本：0\.1\.6
npm 元数据更新时间：2022-08-17T06:12:45.422Z
中文译文更新时间：2026-11-11T11:56:17.404Z
软件包许可：WTFPL

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40darkobits/valida
原文对照：https://npmcn.com/package/%40darkobits/valida/?lang=original
项目主页：https://github.com/darkobits/valida#readme
源码仓库：https://github.com/darkobits/valida

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：43.4 kB，21 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=14
- 运行依赖：6 个
- 首次发布：2021-08-21T08:16:42.075Z
- 最新版本发布：2022-08-17T06:12:45.305Z
- 近 30 天下载量：5877（2026-10-12 至 2026-11-10，npm 官方下载统计）
- 已发布版本数：7（近 12 个月 0 个）

## 中文简介

类型识别对象验证符 .

## 安装

```sh
npm install @darkobits/valida
```

也可以使用：`pnpm add @darkobits/valida`、`yarn add @darkobits/valida`、`bun add @darkobits/valida`

## README

![](https://user-images.githubusercontent.com/441546/130314776-9eb98ab5-a9f6-4ba0-ae08-4488f98f976f.png)

[![](https://img.shields.io/npm/v/@darkobits/valida.svg?style=flat-square&color=398AFB)](https://www.npmjs.com/package/@darkobits/valida) [![](https://img.shields.io/github/workflow/status/darkobits/valida/ci/master?style=flat-square)](https://github.com/darkobits/valida/actions?query=workflow%3Aci) [![](https://img.shields.io/codecov/c/github/darkobits/valida/master?style=flat-square&color=brightgreen)](https://app.codecov.io/gh/darkobits/valida/branch/master) [![](https://img.shields.io/depfu/darkobits/valida?style=flat-square)](https://depfu.com/github/darkobits/valida) [![](https://img.shields.io/static/v1?label=commits&message=conventional&style=flat-square&color=398AFB)](https://conventionalcommits.org/)

  

使用 [`ow`](https://github.com/sindresorhus/ow)。 。 。 .

# 特征

-   如果使用类型参数,则验证所提供的参数 `ow` 相对于所提供类型,确保 您的运行时间类型检查与您的编译时间类型检查保持同步 .
-   如果使用时没有类型参数,则确保验证值符合所提供的形状 `ow` 谱.
-   使用Levenshtein距离来建议错拼键的正确名称(a-la Jest, Babel).
-   允许指定可选默认,在验证前与用户输入合并.

## 安装

```
npm install @darkobits/valida
```

## 使用

```ts
import createValidator from '@darkobits/valida';

interface Person {
  name: string;
  address: {
    street: string;
    locality: string;
    postalCode: string;
    administrativeArea: string;
    countryCode?: string;
  }
  age: number;
}

/**
 * - Pass a function to `createValidator`. This function will be invoked with a
 *   context object containing an `ow` reference.
 * - By default, `validate` will try to return the right shape based on your
 *   spec object, but if you need to be precise, provide a type argument to
 *   `createValidator` with your expected return type.
 */
const validate = createValidator<Person>(({ ow }) => ({
  /**
   * - Return an object containing a `spec` property and an optional `defaults`
   *   property.
   * - Note: Validation will take place _after_ defaults have been merged with
   *   user-provided data, so any fields for which you provide defaults should
   *   not use the `optional` predicate. You can, however, mark those fields as
   *   optional in your type defs so the user doesn't yelled-at if they omit a
   *   field with a default value.
   */
  spec: {
    name: ow.string,
    address: ow.object.exactShape({
      street: ow.string,
      locality: ow.string,
      postalCode: ow.string.numerical,
      administrativeArea: ow.string,
      countryCode: ow.string.maxLength(2)
    }),
    age: ow.number.positive
  },
  defaults: {
    address: {
      countryCode: 'GB'
    }
  }
}));

// - `person` will be of type `Person`
// - `address.CountryCode` will be merged-in.
const person = validate({
  name: 'Ervin Howell',
  address: {
    street: '692 Victor Plains',
    locality: 'Gwenborough',
    postalCode: '44932',
    administrativeArea: 'Chestertonshire',
  }
});

// This will throw; postalCode is non-numeric.
const person = validate({
  name: 'Ervin Howell',
  address: {
    street: '692 Victor Plains',
    locality: 'Gwenborough',
    postalCode: 'kittens',
    administrativeArea: 'Chestertonshire',
  }
});
```

## 另见

-   [`ow`](https://github.com/sindresorhus/ow)

  
[![](https://user-images.githubusercontent.com/441546/102322726-5e6d4200-3f34-11eb-89f2-c31624ab7488.png)](https://npmcn.com/package/%40darkobits/valida/#readme-top)

## 常见问题

### @darkobits/valida 是什么？

类型识别对象验证符 \.

### 如何安装 @darkobits/valida？

在项目目录运行 npm install @darkobits/valida，或使用 pnpm add / yarn add / bun add。

### @darkobits/valida 支持 TypeScript 吗？

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

### @darkobits/valida 支持 ESM 还是 CommonJS？

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

### @darkobits/valida 需要什么版本的 Node\.js？

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

### @darkobits/valida 可以商用吗？

@darkobits/valida 使用 WTFPL 许可。声明的许可为 WTFPL，请阅读许可原文确认使用条件。以上为概括，不构成法律意见，请以许可原文为准。

### @darkobits/valida 的下载量有多少？

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

### @darkobits/valida 还在维护吗？

最新版本 0\.1\.6 发布于 2022/08/17（4 年前）；至今共发布 7 个版本，其中过去 12 个月发布了 0 个；首次发布于 2021/08/21。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

