# @bahmutov/is-my-json-valid 中文文档

规范地址：https://npmcn.com/package/%40bahmutov/is-my-json-valid/

版本：2\.17\.3
npm 元数据更新时间：2022-06-12T15:01:56.660Z
中文译文更新时间：2026-11-07T15:05:42.650Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40bahmutov/is-my-json-valid
原文对照：https://npmcn.com/package/%40bahmutov/is-my-json-valid/?lang=original
项目主页：https://github.com/mafintosh/is-my-json-valid
源码仓库：https://github.com/mafintosh/is-my-json-valid

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

- 包体积（解压后）：114 kB，46 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- 运行依赖：5 个
- 首次发布：2018-04-30T20:37:29.233Z
- 最新版本发布：2018-04-30T20:37:29.379Z
- 近 30 天下载量：14295（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：1（近 12 个月 0 个）

## 中文简介

一个使用代码生成极快的 JSONSchema 验证器

## 安装

```sh
npm install @bahmutov/is-my-json-valid
```

也可以使用：`pnpm add @bahmutov/is-my-json-valid`、`yarn add @bahmutov/is-my-json-valid`、`bun add @bahmutov/is-my-json-valid`

## README

# 是... 我的... json... 验证

临时叉从 [是... 我的... json... 验证](https://github.com/mafintosh/is-my-json-valid) 以获得我的修补

A 类 [JSON计划](http://json-schema.org/) 使用代码生成的验证符 {\\fn黑体\\fs22\\bord1\\shad0\\3ahbe\\4ah00\\fscx67\\fscy66\\2chffffff\\3ch808080}快一点

```
npm install is-my-json-valid
```

它通过整个 JSONSchema v4测试套房 除了 `remoteRefs` 财务报告和审定财务报表 `maxLength`编号 :`minLength` 当使用 unicode 代对时.

[![build status](http://img.shields.io/travis/mafintosh/is-my-json-valid.svg?style=flat)](http://travis-ci.org/mafintosh/is-my-json-valid)

## 使用量

只要通过一个计划来编译

```js
var validator = require('is-my-json-valid')

var validate = validator({
  required: true,
  type: 'object',
  properties: {
    hello: {
      required: true,
      type: 'string'
    }
  }
})

console.log('should be valid', validate({hello: 'world'}))
console.log('should not be valid', validate({}))

// get the last list of errors by checking validate.errors
// the following will print [{field: 'data.hello', message: 'is required'}]
console.log(validate.errors)
```

也可以通过这个图案作为字符串

```js
var validate = validator('{"type": ... }')
```

可以选择使用所需的子模块从中加载一个 schema `__dirname`

```js
var validator = require('is-my-json-valid/require')
var validate = validator('my-schema.json')
```

## 自定义格式

is-my-json-valid 支持 JSON schema v4(例如日期-时间)中指定的格式. 如果您想要添加自定义格式, 将会随格式选项传入验证器

```js
var validate = validator({
  type: 'string',
  required: true,
  format: 'only-a'
}, {
  formats: {
    'only-a': /^a+$/
  }
})

console.log(validate('aa')) // true
console.log(validate('ab')) // false
```

## 外部计划

你可以通过外部计划,你引用 `$ref` 属性为 `schemas` 选项

```js
var ext = {
  required: true,
  type: 'string'
}

var schema = {
  $ref: '#ext' // references another schema called ext
}

// pass the external schemas as an option
var validate = validator(schema, {schemas: {ext: ext}})

validate('hello') // returns true
validate(42) // return false
```

## 过滤去除其它属性

is-my-json-valid 支持过滤去除不在计划内的属性

```js
var filter = validator.filter({
  required: true,
  type: 'object',
  properties: {
    hello: {type: 'string', required: true}
  },
  additionalProperties: false
})

var doc = {hello: 'world', notInSchema: true}
console.log(filter(doc)) // {hello: 'world'}
```

## Verbose 模式显示更多关于出错源的信息

当 `verbose` 选项设置为 `true`, (中文(简体) ), `is-my-json-valid` 产出:

-   `value`: 导致错误的数据值
-   `schemaPath`: 显示哪个子schema失败的密钥数组

```js
var schema = {
  required: true,
  type: 'object',
  properties: {
    hello: {
      required: true,
      type: 'string'
    }
  }
}
var validate = validator(schema, {
  verbose: true
})

validate({hello: 100});
console.log(validate.errors)
// [ { field: 'data.hello',
//     message: 'is the wrong type',
//     value: 100,
//     type: 'string',
//     schemaPath: [ 'properties', 'hello' ] } ]
```

许多受欢迎的图书馆 都很容易检索失败的规则 `schemaPath`数字 :

```
var schemaPath = validate.errors[0].schemaPath
var R = require('ramda')

console.log( 'All evaluate to the same thing: ', R.equals(
  schema.properties.hello,
  { required: true, type: 'string' },
  R.path(schemaPath, schema),
  require('lodash').get(schema, schemaPath),
  require('jsonpointer').get(schema, [""].concat(schemaPath))
))
// All evaluate to the same thing: true
```

## 贪婪模式尽量验证

默认是... 我的Json - 有效的保释 在第一个验证错误,但当贪婪 设定为真实, 尽量验证:

```js
var validate = validator({
  type: 'object',
  properties: {
    x: {
      type: 'number'
    }
  },
  required: ['x', 'y']
}, {
  greedy: true
});

validate({x: 'string'});
console.log(validate.errors) // [{field: 'data.y', message: 'is required'},
                             //  {field: 'data.x', message: 'is the wrong type'}]
```

## 错误消息

这是一份可能的列表 `message` 错误值 :

-   `is required`
-   `is the wrong type`
-   `has additional items`
-   `must be FORMAT format` (格式为 `format` 从计划属性)
-   `must be unique`
-   `must be an enum value`
-   `dependencies not set`
-   `has additional properties`
-   `referenced schema does not match`
-   `negative schema matches`
-   `pattern mismatch`
-   `no schemas match`
-   `no (or more than one) schemas match`
-   `has a remainder`
-   `has more properties than allowed`
-   `has less properties than allowed`
-   `has more items than allowed`
-   `has less items than allowed`
-   `has longer length than allowed`
-   `has less length than allowed`
-   `is less than minimum`
-   `is more than maximum`

## 业绩

is-my-json-valid 使用代码生成将您的 JSON schema 转换为基本的 Javascript 代码,这些代码很容易被v8优化.

在撰写本报告时,是 **快速验证器** 运行时

-   [json-schema- 基准标记](https://github.com/Muscula/json-schema-benchmark)
-   [宇宙现实.com 基准](http://cosmicrealms.com/blog/2014/08/29/benchmark-of-node-dot-js-json-validation-modules-part-3/)
-   [jsck 基准](https://github.com/pandastrike/jsck/issues/72#issuecomment-70992684)
-   [它们是基准](https://cdn.rawgit.com/playlyfe/themis/master/benchmark/results.html)
-   [z-schema 基准](https://rawgit.com/zaggino/z-schema/master/benchmark/results.html)

如果你知道任何其他相关基准 打开公关,我会添加它们.

## 许可证

麻省理工学院

## 常见问题

### @bahmutov/is-my-json-valid 是什么？

一个使用代码生成极快的 JSONSchema 验证器

### 如何安装 @bahmutov/is-my-json-valid？

在项目目录运行 npm install @bahmutov/is-my-json-valid，或使用 pnpm add / yarn add / bun add。

### @bahmutov/is-my-json-valid 支持 TypeScript 吗？

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

### @bahmutov/is-my-json-valid 支持 ESM 还是 CommonJS？

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

### @bahmutov/is-my-json-valid 可以商用吗？

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

### @bahmutov/is-my-json-valid 的下载量有多少？

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

### @bahmutov/is-my-json-valid 还在维护吗？

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

