@bahmutov/is-my-json-valid

中文文档
2.17.3发布于 2018/05/01MITCommonJS近 30 天下载 1.4万

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

中文使用文档
文档目录9 节
  1. 使用量
  2. 自定义格式
  3. 外部计划
  4. 过滤去除其它属性
  5. Verbose 模式显示更多关于出错源的信息
  6. 贪婪模式尽量验证
  7. 错误消息
  8. 业绩
  9. 许可证

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

临时叉从 是... 我的... json... 验证 以获得我的修补

A 类 JSON计划 使用代码生成的验证符 {\fn黑体\fs22\bord1\shad0\3ahbe\4ah00\fscx67\fscy66\2chffffff\3ch808080}快一点

npm install is-my-json-valid

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

build status

使用量

只要通过一个计划来编译

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)

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

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

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

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

自定义格式

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

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 选项

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 支持过滤去除不在计划内的属性

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失败的密钥数组
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 - 有效的保释 在第一个验证错误,但当贪婪 设定为真实, 尽量验证:

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优化.

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

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

许可证

麻省理工学院

版本与发布记录

@bahmutov/is-my-json-valid 共发布 1 个版本,首次发布于 2018/05/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.17.3latest2018/05/018 年前

关于 @bahmutov/is-my-json-valid 的常见问题

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