@exodus/schemasafe-babel-plugin

中文文档
1.2.1发布于 2026/07/21MITCommonJS近 30 天下载 1.1万

Babel plugin compiling JSON schemas with @exodus/schemasafe

原始使用文档

@exodus/schemasafe-babel-plugin

Compiles imported JSON schemas into validator functions using @exodus/schemasafe.

[!WARNING] This is a codegen library and therefore poses a risk if used with untrusted input.

Usage

Usage:

  1. Add the plugin in babel.config.js, e.g.:
module.exports = {
  plugins: [['@exodus/schemasafe-babel-plugin', pluginParams]],
}

pluginParams should be a valid SchemasafeBabelPluginParams object.

  1. Import a JSON schema in your project:
import validateData from './data.schemasafe.json'
import { validateMeta } from './meta.schemasafe.json' // Named imports also work.
  1. After compilation the imported variables become a validator function so a consumer can use it like a normal JS function:
try {
  validateData(data)
} catch (err) {
  console.log(err.message) // 'JSON validation failed for...'
}

API reference

/*
  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
  }[]
}

Example:

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 共发布 8 个版本,首次发布于 2023/04/03;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.2.1latest2026/07/213 个月前
1.2.02024/11/252 年前
1.1.02024/02/052 年前
1.0.02024/01/242 年前
0.0.42024/01/222 年前
0.0.32023/07/123 年前

关于 @exodus/schemasafe-babel-plugin 的常见问题

@exodus/schemasafe-babel-plugin 是什么?

Babel plugin compiling JSON schemas with @exodus/schemasafe

如何安装 @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 和提交记录判断。