# @exodus/schemasafe-babel-plugin 中文文档

规范地址：https://npmcn.com/package/%40exodus/schemasafe-babel-plugin/

版本：1\.2\.1
npm 元数据更新时间：2026-07-21T14:00:17.525Z
中文译文更新时间：2026-11-14T04:13:51.593Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40exodus/schemasafe-babel-plugin
原文对照：https://npmcn.com/package/%40exodus/schemasafe-babel-plugin/?lang=original
项目主页：https://github.com/ExodusMovement/exodus/tree/master/libs/libraries/schemasafe-babel-plugin
源码仓库：https://github.com/ExodusMovement/exodus

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

- 包体积（解压后）：8.2 kB，6 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- 运行依赖：1 个
- peer 依赖：@exodus/schemasafe ^1\.0\.0
- 首次发布：2023-04-03T11:47:42.147Z
- 最新版本发布：2026-07-21T14:00:16.121Z
- 近 30 天下载量：10666（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：8（近 12 个月 1 个）

## 中文简介

Babel 插件, 用 @ exodus/ schemasafe 编译 JSON 计划

## 安装

```sh
npm install @exodus/schemasafe-babel-plugin
```

也可以使用：`pnpm add @exodus/schemasafe-babel-plugin`、`yarn add @exodus/schemasafe-babel-plugin`、`bun add @exodus/schemasafe-babel-plugin`

## README

# 翻译:

使用 : `@exodus/schemasafe`。 。 。 .

> \[尖叫声\] 这是一个代码库,因此如果使用不可信输入,将带来风险.

# 使用量

### 用法 :

1.  将插件添加到 `babel.config.js`,例如:

```js
module.exports = {
  plugins: [['@exodus/schemasafe-babel-plugin', pluginParams]],
}
```

`pluginParams` 应该是有效的 [斯克马萨德-巴贝尔-普卢金-帕拉姆斯](https://npmcn.com/package/%40exodus/schemasafe-babel-plugin/#readme-api-reference) 对象.

2.  在您的工程中导入 JSON 计划 :

```js
import validateData from './data.schemasafe.json'
import { validateMeta } from './meta.schemasafe.json' // Named imports also work.
```

3.  输入的变量在编译后成为验证器功能

```js
try {
  validateData(data)
} catch (err) {
  console.log(err.message) // 'JSON validation failed for...'
}
```

### API 参考

```ts
/*
  See https://github.com/ExodusMovement/schemasafe#options for all available options.
*/
interface SchemasafeOptions {
  schemas?: JsonSchema[] // Extra schemas to be referenced from a master schema.
  formats?: Object // Extra formats to be used in the supplied schemas.
  removeAdditional?: boolean // Removes non-valid properties from the original object if true.
}

interface SchemasafeBabelPluginParams {
  options: {
    globs: string[] // Global patterns to match against particular schemas.
    schemasafeOptions?: SchemasafeOptions
  }[]
}
```

示例

```js
plugins: [
  [
    '@exodus/schemasafe-babel-plugin',
    {
      options: [
        {
          globs: ['**/src/schemas/*.schema.json'],
          schemasafeOptions: {
            formats: { 'no-foo': (str) => !str.includes('foo') },
          },
        },
        {
          globs: ['**/node_modules/some-package/**/*.schemasafe.json'],
          schemasafeOptions: {
            enableExtra: true,
            removeAdditional: true,
          },
        },
      ],
    },
  ],
]
```

## 常见问题

### @exodus/schemasafe-babel-plugin 是什么？

Babel 插件, 用 @ exodus/ schemasafe 编译 JSON 计划

### 如何安装 @exodus/schemasafe-babel-plugin？

在项目目录运行 npm install @exodus/schemasafe-babel-plugin，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@exodus/schemasafe（^1\.0\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @exodus/schemasafe-babel-plugin 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/exodus\_\_schemasafe-babel-plugin 是否存在。

### @exodus/schemasafe-babel-plugin 支持 ESM 还是 CommonJS？

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

### @exodus/schemasafe-babel-plugin 可以商用吗？

@exodus/schemasafe-babel-plugin 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @exodus/schemasafe-babel-plugin 的下载量有多少？

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

### @exodus/schemasafe-babel-plugin 还在维护吗？

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

